From f460c64ab483a5d3f170b380fd4cdce1edfca096 Mon Sep 17 00:00:00 2001 From: quadrismegistus Date: Tue, 11 Aug 2020 14:42:55 +0100 Subject: [PATCH] adding empty cache folders and other things --- .gitignore | 2 +- client/cache/.gitignore | 4 ++++ client/main.py | 33 ++++++++++++++++++--------------- client/screens/feed/feed.kv | 1 + client/screens/feed/feed.py | 25 ++++++++++++++++++++++--- client/screens/post/post.py | 15 ++++++++++----- 6 files changed, 56 insertions(+), 24 deletions(-) create mode 100644 client/cache/.gitignore diff --git a/.gitignore b/.gitignore index 1e381ae..337b7d7 100644 --- a/.gitignore +++ b/.gitignore @@ -4,5 +4,5 @@ client/komrade.json uploads uploads/* uploads/*/* -client/cache + client/log.txt diff --git a/client/cache/.gitignore b/client/cache/.gitignore new file mode 100644 index 0000000..86d0cb2 --- /dev/null +++ b/client/cache/.gitignore @@ -0,0 +1,4 @@ +# Ignore everything in this directory +* +# Except this file +!.gitignore \ No newline at end of file diff --git a/client/main.py b/client/main.py index c355ddc..7dc1047 100644 --- a/client/main.py +++ b/client/main.py @@ -115,7 +115,7 @@ class MainApp(MDApp): if not self.is_logged_in(): self.root.change_screen('login') else: - self.root.post_id=179 + self.root.post_id=190 self.root.change_screen('view') return self.root @@ -209,22 +209,25 @@ class MainApp(MDApp): with open(ofn_json,'w') as of: json.dump(jsond, of) + return jsond + + + def get_image(self, img_src): # is there an image? - img_src = jsond.get('img_src','') - if img_src: - # is it cached? - ofn_image = os.path.join('cache','img',img_src) - if not os.path.exists(ofn_image): - # create dir? - ofn_image_dir = os.path.split(ofn_image)[0] - if not os.path.exists(ofn_image_dir): os.makedirs(ofn_image_dir) - log('getting image!') - with self.get_session() as sess: - with sess.get(self.api+'/download/'+img_src,stream=True) as r: - with open(ofn_image,'wb') as of: - shutil.copyfileobj(r.raw, of) + if not img_src: return + # is it cached? + ofn_image = os.path.join('cache','img',img_src) + if not os.path.exists(ofn_image): + # create dir? + ofn_image_dir = os.path.split(ofn_image)[0] + if not os.path.exists(ofn_image_dir): os.makedirs(ofn_image_dir) + log('getting image!') + with self.get_session() as sess: + with sess.get(self.api+'/download/'+img_src,stream=True) as r: + with open(ofn_image,'wb') as of: + shutil.copyfileobj(r.raw, of) + return ofn_image - return jsond if __name__ == '__main__': diff --git a/client/screens/feed/feed.kv b/client/screens/feed/feed.kv index 2ab51ec..5c5031e 100644 --- a/client/screens/feed/feed.kv +++ b/client/screens/feed/feed.kv @@ -38,6 +38,7 @@ : # height: '25' # size: self.norm_image_size + id: post_image allow_stretch: True # keep_ratio: True diff --git a/client/screens/feed/feed.py b/client/screens/feed/feed.py index a597d87..89dd4ed 100644 --- a/client/screens/feed/feed.py +++ b/client/screens/feed/feed.py @@ -6,6 +6,9 @@ from kivymd.uix.card import MDCard, MDSeparator from kivy.uix.scrollview import ScrollView from screens.base import ProtectedScreen from main import log +import os +from kivy.app import App + ### POST CODE class PostTitle(MDLabel): pass @@ -42,7 +45,9 @@ class PostCard(MDCard): super().__init__() self.author = author self.title = title - self.img_src = img_src + self.img_src = img_src if img_src else '' + self.cache_img_src = os.path.join('cache','img',img_src) if img_src else '' + self.img_loaded = os.path.exists(self.cache_img_src) self.content = content self.bind(minimum_height=self.setter('height')) @@ -56,9 +61,9 @@ class PostCard(MDCard): # author_section_layout.add_widget(author_avatar) # self.add_widget(author_section_layout) - if self.img_src: + if self.cache_img_src: image_layout = PostImageLayout() - image = PostImage(source=self.img_src) + self.image = image = PostImage(source=self.cache_img_src) image.height = '300dp' image_layout.add_widget(image) image_layout.height='300dp' @@ -98,6 +103,20 @@ class PostCard(MDCard): self.add_widget(scroller) # self.add_widget(post_layout) + @property + def app(self): + return App.get_running_app() + + def load_image(self): + if not self.img_src: return + if self.img_loaded: return + + # otherwise load image... + self.app.get_image(self.img_src) + log('done getting image!') + self.image.reload() + self.img_loaded=True + ##### diff --git a/client/screens/post/post.py b/client/screens/post/post.py index 4184326..049df28 100644 --- a/client/screens/post/post.py +++ b/client/screens/post/post.py @@ -49,21 +49,26 @@ class ViewPostScreen(ProtectedScreen): log('child: '+str(child)) self.remove_widget(child) - post = self.app.get_post(self.root.post_id) - log(post) - img_src=os.path.join('cache','img',post['img_src']) if post['img_src'] else '' + post_json = self.app.get_post(self.root.post_id) + log(post_json) + img_src = post_json.get('img_src','') + content = post_json.get('content','') + cache_img_src = os.path.join('cache','img',img_src) if img_src else '' kwargs = dict(author='Marx Zuckerberg', title='', img_src=img_src, - content=post['content']) + content=content) log(kwargs) + post = PostCard(**kwargs) print(post) self.add_widget(post) + + def on_enter(self): - pass + for child in self.children: child.load_image() pass