def post(self): user = users.get_current_user() date = self.request.get('date') user_list = UserList.all().filter('google_account =', user).filter('status =', 'stable').get() if user_list is None: return self.error(401) archive_list_query = ArchiveList().all().filter( 'account =', user_list.key()) if date != '': archive_list_query.filter( 'created_at <', datetime.datetime.strptime(date, '%Y-%m-%d %H:%M:%S')) archive_list_query.order('-created_at') archive_list = archive_list_query.fetch(IMAGE_FETCH_COUNT) data = [] for image in archive_list: data.append({ 'image_key': image.image_key, 'created_at': image.created_at.strftime('%Y-%m-%dT%H:%M:%S+0000') }) date = image.created_at.strftime('%Y-%m-%d %H:%M:%S') #logging.info('date: %s' % date) archive_list_query = ArchiveList().all().filter( 'account =', user_list.key()) archive_list_query.filter( 'created_at <', datetime.datetime.strptime(date, '%Y-%m-%d %H:%M:%S')) archive_list_query.order('-created_at') archive_list = archive_list_query.fetch(IMAGE_FETCH_COUNT) if len(archive_list) <= 0: date = False data = {'data': data, 'date': date} json_data = json.dumps(data, ensure_ascii=False) self.response.content_type = 'application/json' self.response.out.write(json_data)
def post(self, image_key): if image_key == '' or len(image_key) != 6: return self.error(404) archive_list_query = ArchiveList().all() archive_list_query.filter('image_key =', image_key) archive_list_query.filter('delete_flg =', False) archive_list = archive_list_query.get() if archive_list is None: return self.error(404) mode = self.request.get('mode') user = users.get_current_user() if user: if archive_list.account.google_account == user: if mode == 'delete': archive_list.delete_flg = True archive_list.updated_at = datetime.datetime.now() archive_list.put() mask_image_key = hashlib.md5( '%s-%s' % (SECRET_MASK_KEY, image_key)).hexdigest() original_image_key = hashlib.md5( '%s-%s' % (SECRET_IMAGE_KEY, image_key)).hexdigest() memcache.delete('cached_mask_%s' % mask_image_key) memcache.delete('cached_original_%s' % original_image_key) memcache.delete('cached_image_%s' % image_key) memcache.delete('archive_%s' % image_key) for style in ('icon48', 'icon120', 'size240', 'size300'): memcache.delete('cached_image_%s_%s' % (image_key, style)) data = {'status': True} json_data = json.dumps(data, ensure_ascii=False) self.response.content_type = 'application/json' self.response.out.write(json_data)
def get(self): image_key = self.request.get('id') user_id = self.request.get('user_id') if image_key == '': return self.error(404) archive_list_query = ArchiveList().all() archive_list_query.filter('image_key =', image_key) archive_list_query.filter('user_id =', user_id) archive_list = archive_list_query.get() if archive_list is None: return self.error(404) mask_image_key = hashlib.md5('%s-%s' % (SECRET_MASK_KEY, image_key)).hexdigest() mask_image_query = MaskImage().all() mask_image_query.filter('access_key =', mask_image_key) mask_image = mask_image_query.get() mask_image.delete() original_image_key = hashlib.md5( '%s-%s' % (SECRET_IMAGE_KEY, image_key)).hexdigest() original_image_query = OriginalImage().all() original_image_query.filter('access_key =', original_image_key) original_image = original_image_query.get() original_image.delete() archive_list.delete() memcache.delete('cached_mask_%s' % mask_image_key) memcache.delete('cached_original_%s' % original_image_key) memcache.delete('cached_image_%s' % image_key) data = {'state': True} json_data = json.dumps(data, ensure_ascii=False) self.response.content_type = 'application/json' self.response.out.write(json_data)
def get(self): user_id = self.request.get('id') archive_list_query = ArchiveList().all() archive_list_query.filter('user_id =', user_id) archive_list_query.order('-created_at') archive_list = archive_list_query.fetch(25) data = [] for image in archive_list: data.append({ 'image_key': image.image_key, 'created_at': image.created_at.strftime('%Y-%m-%dT%H:%M:%S+0000') }) json_data = json.dumps(data, ensure_ascii=False) self.response.content_type = 'application/json' self.response.out.write(json_data)
def get(self, version='v1'): user = users.get_current_user() date = self.request.get('date') action = self.request.get('action') if action not in ('login'): action = None if version not in ('v1', 'v2'): return self.error(501) if version == 'v1': actionButton = False else: actionButton = True if user is None: return self.redirect('/page/%s/welcome' % version) user_list = UserList.all().filter('google_account =', user).filter('status =', 'stable').get() if user_list is None: return self.redirect('/page/%s/welcome?action=logout' % version) archive_list_query = ArchiveList().all().filter( 'account =', user_list.key()).filter('delete_flg =', False) if date != '': archive_list_query.filter( 'created_at <', datetime.datetime.strptime(date, '%Y-%m-%d %H:%M:%S')) archive_list_query.order('-created_at') archive_list = archive_list_query.fetch(IMAGE_FETCH_COUNT) if len(archive_list) <= 0: return self.redirect('/page/%s/start?action=login' % version) data = [] for image in archive_list: data.append({ 'image_key': image.image_key, 'created_at': image.created_at.strftime('%Y-%m-%dT%H:%M:%S+0000') }) date = image.created_at.strftime('%Y-%m-%d %H:%M:%S') logging.info('date: %s' % date) how_to_use_it = False if len(archive_list) < 5: how_to_use_it = True load_more_hide = False if date != '': archive_list_query.filter( 'created_at <', datetime.datetime.strptime(date, '%Y-%m-%d %H:%M:%S')) archive_list = archive_list_query.fetch(IMAGE_FETCH_COUNT) if len(archive_list) <= 0: load_more_hide = True account = user.email() logout_url = users.create_logout_url('/page/%s/welcome?action=logout' % version) template_values = { 'archive_list': data, 'load_more_hide': load_more_hide, 'date': date, 'account': account, 'logout_url': logout_url, 'action': action, 'how_to_use_it': how_to_use_it, 'version': version, 'actionButton': actionButton } self.render_template('page/archives.html', template_values)
def post(self, version='v1'): user = users.get_current_user() if version not in ('v1', 'v2'): return self.error(501) data = {} tmp_image_key = self.request.get('tmp_image_key') if tmp_image_key: logging.info('tmp_image_key: %s' % tmp_image_key) tmp_org_image = memcache.get('tmp_org_%s' % tmp_image_key) tmp_msk_image = memcache.get('tmp_msk_%s' % tmp_image_key) if tmp_org_image and tmp_msk_image: org_image = tmp_org_image['org_image'] msk_image = tmp_msk_image['msk_image'] mask_mode = tmp_msk_image['mask_mode'] mask_type = tmp_msk_image['mask_type'] access_code = tmp_msk_image['access_code'] else: return self.error(500) else: org_image = self.request.get('original_image') msk_image = self.request.get('mask_image') mask_mode = self.request.get('mask_mode') mask_type = self.request.get('mask_type') access_code = self.request.get('access_code') comment = self.request.get('comment') logging.info('original_image: %d' % len(org_image)) logging.info('mask_image: %d' % len(msk_image)) logging.info('User: %s' % user.email()) logging.info('Comment: %s', comment) logging.info('MaskType: %s', mask_type) user_list = UserList.all().filter('google_account =', user).filter('status =', 'stable').get() if user_list is None: return self.error(401) if len(mask_mode) == 0: mask_mode = 'scratch' mask_mode_list = [ 'scratch', 'accelerometer1', 'accelerometer2', 'sound_level', 'barcode' ] mask_type_list = ['black', 'mosaic', 'caution', 'zebra', 'note'] if mask_type not in mask_type_list: mask_type = 'black' if org_image and msk_image and mask_mode in mask_mode_list: #image_key = hashlib.md5('%s' % uuid.uuid4()).hexdigest()[0:6] image_key = gen_imagekey() # workaround tmp_archive = ArchiveList.all().filter('image_key =', image_key).get() if tmp_archive is not None: return self.error(500) archive_list = ArchiveList() archive_list.image_key = image_key archive_list.comment = comment archive_list.account = user_list.key() archive_list.delete_flg = False archive_list.put() original_image = OriginalImage() original_image.archive_list_key = archive_list.key() original_image.image = db.Blob(org_image) original_image.access_key = hashlib.md5( '%s-%s' % (SECRET_IMAGE_KEY, image_key)).hexdigest() original_image.put() mask_image = MaskImage() mask_image.mask_mode = mask_mode mask_image.archive_list_key = archive_list.key() mask_image.image = db.Blob(msk_image) mask_image.access_key = hashlib.md5( '%s-%s' % (SECRET_MASK_KEY, image_key)).hexdigest() mask_image.read_count = 0 mask_image.access_code = access_code mask_image.mask_type = mask_type mask_image.put() data = {'image_key': image_key} else: data = {'image_key': False} json_data = json.dumps(data, ensure_ascii=False) self.response.content_type = 'application/json' self.response.out.write(json_data)
def get(self): image_key = self.request.get('id') if image_key == '': return self.error(404) style = self.request.get('style') if style in ('icon48', 'icon120', 'size240', 'size300'): cache_key = 'cached_image_%s_%s' % (image_key, style) else: style = None cache_key = 'cached_image_%s' % image_key logging.info('memcache_key: %s' % cache_key) thumbnail = memcache.get(cache_key) if thumbnail is None: archive_list_query = ArchiveList().all() archive_list_query.filter('image_key =', image_key) archive_list_query.filter('delete_flg =', False) archive_list = archive_list_query.get() if archive_list is None: memcache.add(cache_key, 404, 3600) return self.error(404) mask_image_key = hashlib.md5( '%s-%s' % (SECRET_MASK_KEY, image_key)).hexdigest() mask_image_query = MaskImage().all() mask_image_query.filter('access_key =', mask_image_key) mask_image = mask_image_query.get() original_image_key = hashlib.md5( '%s-%s' % (SECRET_IMAGE_KEY, image_key)).hexdigest() original_image_query = OriginalImage().all() original_image_query.filter('access_key =', original_image_key) original_image = original_image_query.get() if mask_image is None or original_image is None: return self.error(404) mask_image = images.Image(mask_image.image) mask_image.resize(width=640) #mask_image.im_feeling_lucky() new_mask_image = mask_image.execute_transforms( output_encoding=images.PNG) new_mask_image = convert_square(new_mask_image, 640, 640) original_image = images.Image(original_image.image) original_image.resize(width=640) #original_image.im_feeling_lucky() new_original_image = original_image.execute_transforms( output_encoding=images.PNG) new_original_image = convert_square(new_original_image, 640, 640) all_images = [] all_images.append((new_original_image, 0, 0, 1.0, images.TOP_LEFT)) all_images.append((new_mask_image, 0, 0, 1.0, images.TOP_LEFT)) image_c = images.composite(all_images, 640, 640, 0, images.PNG, 100) img = images.Image(image_c) width = 320 if style is not None: if style == 'size240': width = 240 elif style == 'size300': width = 300 img.resize(width=width) #img.im_feeling_lucky() thumbnail = img.execute_transforms(output_encoding=images.JPEG) if style is not None: if style == 'icon48': thumbnail = convert_square(thumbnail, 48, 48) elif style == 'icon120': thumbnail = convert_square(thumbnail, 120, 120) memcache.add(cache_key, thumbnail, 3600) logging.info('Image from DB. image_key: %s' % image_key) else: logging.info('Image from memcache. image_key: %s' % image_key) if thumbnail == 404: return self.error(404) self.response.headers['Content-Type'] = 'image/jpeg' self.response.out.write(thumbnail)
def get(self, image_key): if image_key == '' or len(image_key) != 6: return self.error(404) archive_list = memcache.get('archive_%s' % image_key) if archive_list is None: archive_list_query = ArchiveList().all() archive_list_query.filter('image_key =', image_key) archive_list = archive_list_query.get() if archive_list is None or archive_list.delete_flg: memcache.add('archive_%s' % image_key, 404, 3600) return self.error(404) memcache.add('archive_%s' % image_key, archive_list, 3600) logging.info('Archive from datastore.') else: if archive_list == 404: return self.error(404) logging.info('Archive from memcache.') if archive_list is None: return self.error(404) actionButton = self.request.get('actionButton') if actionButton == 'True': actionButton = True else: actionButton = False mask_image_key = hashlib.md5('%s-%s' % (SECRET_MASK_KEY, image_key)).hexdigest() read_count = memcache.get('count_mask_%s' % mask_image_key) if read_count is None: mask_image_query = MaskImage().all() mask_image_query.filter('access_key =', mask_image_key) mask_image = mask_image_query.get() if mask_image is not None: read_count = mask_image.read_count memcache.add('count_mask_%s' % mask_image_key, mask_image.read_count, 3600) logging.info('Count mask add. id: %s' % image_key) else: read_count = 0 mask_type = memcache.get('mask_type_%s' % mask_image_key) if mask_type is None: mask_image_query = MaskImage().all() mask_image_query.filter('access_key =', mask_image_key) mask_image = mask_image_query.get() try: mask_type = mask_image.mask_type except: mask_type = 'black' memcache.add('mask_type_%s' % mask_image_key, mask_image.mask_type, 3600) logging.info('Mask type: %s id: %s' % (image_key, mask_type)) is_owner = False user = users.get_current_user() if user: if archive_list.account.google_account == user: is_owner = True page_title = None try: comment = archive_list.comment if comment == '' or comment == None: comment = 'no comment.' else: page_title = comment except: comment = 'no comment.' template_values = { 'image_key': image_key, 'comment': comment, 'read_count': read_count, 'is_owner': is_owner, 'page_title': page_title, 'actionButton': actionButton, 'mask_type': mask_type } user_agent = self.request.headers.get('user_agent') logging.info('UserAgent: %s' % user_agent) if 'Mobile' in user_agent and ('Safari' in user_agent or 'AppleWebKit' in user_agent): if 'Safari' in user_agent: safari = True else: safari = False if 'AppleWebKit' in user_agent: webkit = True else: webkit = False template_values['safari'] = safari template_values['webkit'] = webkit if 'iPad' in user_agent: path = 'view/image.html' else: path = 'view/image_ios.html' else: path = 'view/image.html' self.render_template(path, template_values)