def store(description, image): def _strconv(msg): htmlentities = list() for c in msg: if ord(c) in codepoint2name: htmlentities.append('&%s;' % codepoint2name[ord(c)]) else: htmlentities.append(c) return ''.join(htmlentities) plus = { 'timestamp': time.time(), 'data': { 'user': current_user.name, 'description': _strconv(description).encode('utf-8'), 'image': image, }, } data = readjson(taskjson) if data is None: data = [] data.append(plus) writejson(taskjson, data) logger.info('new entry from %s: %s image: %s' %(current_user.name, description, image))
def index(): taskform = TaskForm() if taskform.validate_on_submit(): description = None filename = None if taskform.description.data: description = taskform.description.data if taskform.image.data: filename = secure_filename(taskform.image.data.filename) # avoid overwriting existing files while path.exists(path.join(taskattachdir, filename)): fname, fext = path.splitext(filename) filename = '%s_%s' %(fname, fext) image_path = path.join(taskattachdir, filename) taskform.image.data.save(image_path) if AUTO_ROTATE: try: angle = fix_orientation(image_path, save_over=True) logger.info('image %s rotated by %s degrees' %(filename, angle)) except ValueError as e: logger.warn('image %s has no EXIF data: %s' %(filename, e)) store(description, filename) recent = readjson(taskjson) if recent is not None: recent = sorted(recent, key=lambda r: r['timestamp'], reverse=True) return render_template('main.html', title='Today I ...', taskform=taskform, recent=recent)
def backup(): recent = readjson(taskjson) if recent is not None: if not os.path.exists(archivedir): os.makedirs(archivedir) writejson(os.path.join(archivedir, taskarchivejson_name), recent) logger.info('backup: %s' %(taskjson)) for element in recent: if element['data']['image'] is not None: copy(os.path.join(taskattachdir, element['data']['image']), archivedir) logger.info('backup: %s' %(element['data']['image'])) else: logger.info('nothing to backup')
def push(): recent = readjson(os.path.join(archivedir, taskarchivejson_name)) if recent is not None: content = u'<dl>\n' wp = Client(WPxmlrpc, WPuser, WPpass) recent = sorted(recent, key=lambda r: r['timestamp']) for element in recent: if element['data']['image'] is not None: filename = os.path.join(archivedir, element['data']['image']) data = { 'name': element['data']['image'], 'type': guess_type(filename)[0] } with open(filename, 'rb') as img: data['bits'] = xmlrpc_client.Binary(img.read()) response = wp.call(media.UploadFile(data)) content += pskel.format(user=element['data']['user'], image=iskel.format(imageurl=response['url'], imagealt=element['data']['description']), description=element['data']['description']) else: content += pskel.format(user=element['data']['user'], image='', description=element['data']['description']) content += u'</dl>\n' post = WordPressPost() post.title = '%s %s' %(report_headline, week_timestamp()) post.content = content post.terms_names = { 'post_tag': post_tag, 'category': category, } wp.call(NewPost(post)) else: logger.info('nothing to push')