adding empty cache folders and other things

p2p
quadrismegistus 4 years ago
parent a3a314669f
commit f460c64ab4

2
.gitignore vendored

@ -4,5 +4,5 @@ client/komrade.json
uploads
uploads/*
uploads/*/*
client/cache
client/log.txt

@ -0,0 +1,4 @@
# Ignore everything in this directory
*
# Except this file
!.gitignore

@ -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__':

@ -38,6 +38,7 @@
<PostImage>:
# height: '25'
# size: self.norm_image_size
id: post_image
allow_stretch: True
# keep_ratio: True

@ -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
#####

@ -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

Loading…
Cancel
Save