def update(self, _id): try: # get all parameters data = json.loads(self.request.body) # source ip ip = self.request.remote_addr credits = data.get('credits', '') tags = data.get('tags', []) lang = data.get('lang', 'en') audience = int(data.get('audience', 1)) email = data.get('email', None) tags_list = [] # if there are new tags create them for tag in tags: tag = Tag.get_or_create(tag, source='MASK', lang=lang) tags_list.append(tag.key.id()) # get the existing photo mask = Mask.get_by_id(_id) if mask is None: self.response.headers['Access-Control-Allow-Origin'] = "*" self.response.set_status('404') self.response.write("Not Found") mask.audience = audience mask.is_safe = True if int(audience) <= 2 else False mask.tags.extend(tags_list) mask.tags = list(set(mask.tags)) mask.email = email mask.lang = lang mask.credits = credits mask.put() # memcache memcache.set(_id, Mask.to_json_object(mask)) response_data = { "id": _id, "mask": "/mask/%s" % _id, "url": "/m/%s" % _id } self.response.headers['Access-Control-Allow-Origin'] = "*" self.response.headers['Content-Type'] = 'application/json' self.response.write(json.dumps(response_data)) except Exception, e: self.response.headers['Access-Control-Allow-Origin'] = "*" self.response.set_status('400') self.response.write(e.message)
def mask(self, _id): self.response.headers['Access-Control-Allow-Origin'] = "*" mask_json = memcache.get(_id) if mask_json is None: mask = Mask.get_by_id(_id) if mask is None: self.response.set_status('404') return mask_json = Mask.to_json_string(mask) memcache.set(_id, mask_json) self.response.headers['Content-Type'] = 'application/json' self.response.write(mask_json)
def masks(self): id_2_delete = self.request.get('delete', None) if id_2_delete is not None: print 'deleting mask %s' % id_2_delete mask = Mask.get_by_id(id_2_delete) mask.key.delete() template_values = { 'page': 'masks', 'link': 'photos', 'items': Mask.query().fetch() } template = JINJA_ENVIRONMENT.get_template('admin_masks.html') self.response.write(template.render(template_values))
def vote(self, _id, action): if self.request.method == 'OPTIONS': self.options() return self.response.headers['Access-Control-Allow-Origin'] = "*" mask = Mask.get_by_id(_id) if mask is None: self.response.set_status('404') return if action == 'up': mask.up_vote += 1 mask.put() # memcache memcache.set(_id, Mask.to_json_object(mask)) self.response.headers['Content-Type'] = 'application/json' self.response.write( json.dumps({ 'up': mask.up_vote, 'dw': mask.dwn_vote })) return if action == 'dw': mask.dwn_vote += 1 mask.put() # memcache memcache.set(_id, Mask.to_json_object(mask)) self.response.headers['Content-Type'] = 'application/json' self.response.write( json.dumps({ 'up': mask.up_vote, 'dw': mask.dwn_vote })) return self.response.set_status('400')
def post(self): try: #image_file = self.request.get('image', default_value=None) data = json.loads(self.request.body) ext = data.get('type') # mime type must be present and must be png or gif if ext is None or ext not in ['png', 'gif']: raise Exception('invalid format') # image must be present if data.get('image', None) is None: raise Exception('missing image') image = base64.b64decode( data.get('image').replace('data:image/%s;base64,' % ext, '')) ip = self.request.remote_addr credits = data.get('credits', '') tags = data.get('tags', []) lang = data.get('lang', 'en') audience = int(data.get('audience', 1)) is_safe = True if audience <= 2 else False email = data.get('email', None) # flip image horizontally # image_object = images.Image(image) # image_object.horizontal_flip() # image = image_object.execute_transforms() # caclulate mask id mask_id = hashlib.sha1(image).hexdigest() print mask_id # check if mask exists if Mask.get_by_id(mask_id) is not None: self.response.headers['Access-Control-Allow-Origin'] = "*" self.response.set_status('304') return # if len(tags) == 0: # raise Exception('missing tags') tags_list = [] # if there are new tags create them for tag in tags: tag = Tag.get_or_create(tag, source='MASK', lang=lang) tags_list.append(str(tag.key.id())) # save it on cloud storage bucket_name = os.environ.get( 'BUCKET_NAME', app_identity.get_default_gcs_bucket_name()) filename = '%s.%s' % (mask_id, ext) gs_filename = '/%s/%s/%s' % (bucket_name, FOLDER_MASKS, filename) gcs_file = gcs.open(gs_filename, 'w', content_type='image/%s' % ext, options={ 'x-goog-meta-foo': 'foo', 'x-goog-meta-bar': 'bar' }) gcs_file.write(image) gcs_file.close() mask = Mask(id=mask_id) mask.populate(filename=filename, ip=ip, audience=audience, email=email, tags=tags_list, credits=credits, is_safe=is_safe) mask.put() # memcache memcache.set(mask_id, Mask.to_json_object(mask)) response_data = { "id": mask_id, "mask": "/masks/%s" % mask_id, "url": "/m/%s" % filename } self.response.headers['Access-Control-Allow-Origin'] = "*" self.response.headers['Content-Type'] = 'application/json' self.response.write(json.dumps(response_data)) except Exception, e: self.response.headers['Access-Control-Allow-Origin'] = "*" self.response.set_status('400') self.response.write(e.message)
def post(self): try: # get all parameters data = json.loads(self.request.body) ext = data.get('type') # mime type must be present and must be png or gif if ext is None or ext not in ['png', 'gif']: raise Exception('invalid format') # image must be present image = data.get('image', None) if image is None: raise Exception('missing image') # parse image image = base64.b64decode( image.replace('data:image/%s;base64,' % ext, '')) # source ip ip = self.request.remote_addr email = data.get('email', '') tags = data.get('tags', []) lang = data.get('lang', 'en') audience = int(data.get('audience', 1)) masks = [str(x) for x in data.get('masks', [])] # mask must be present if len(masks) == 0: raise Exception('masks is empty') # caclulate photo id photo_id = hashlib.sha1(image).hexdigest() photo = Photo.get_by_id(photo_id) # check if phot exists if photo is not None: self.response.headers['Access-Control-Allow-Origin'] = "*" self.response.set_status('304') return tags_list = [] # if there are new tags create them for tag in tags: tag = Tag.get_or_create(tag, source='USER', lang=lang) tags_list.append(tag.key.id()) # find the other tags in the masks for mask_id in masks: if mask_id == 0: # test mask continue mask = Mask.get_by_id(mask_id) if mask is not None: tags_list.extend(mask.tags) mask.photo_count += 1 mask.put() # set the minumum audience to the maximum of the mask audience if mask.audience > audience: audience = mask.audience # now understand if it is safe or not is_safe = True if int(audience) <= 2 else False tags_list = list(set(tags_list)) # save it on cloud storage bucket_name = os.environ.get( 'BUCKET_NAME', app_identity.get_default_gcs_bucket_name()) filename = '%s.%s' % (photo_id, ext) gs_filename = '/%s/%s/%s' % (bucket_name, FOLDER_PHOTOS, filename) gcs_file = gcs.open(gs_filename, 'w', content_type='image/%s' % ext, options={ 'x-goog-acl': 'public-read', 'x-goog-meta-foo': 'foo' }) gcs_file.write(image) gcs_file.close() # create a key for the stored file gs_key = blobstore.create_gs_key('/gs' + gs_filename) # get the cache url for the image serving_url = images.get_serving_url(gs_key) photo = Photo(id=photo_id) photo.populate(filename=filename, ip=ip, audience=audience, masks=masks, tags=tags_list, email=email, ext=ext, thumb=serving_url, is_safe=is_safe) photo.put() response_data = { "id": photo_id, "blowout": "/blowout/%s" % photo_id, "image": "/p/%s" % photo.filename, "thumb": photo.thumb } # memcache json_photo = Photo.to_json_object(photo) memcache.set(photo.key.id(), json_photo) self.response.headers['Access-Control-Allow-Origin'] = "*" self.response.headers['Content-Type'] = 'application/json' self.response.write(json.dumps(response_data)) except Exception, e: logging.info(e) self.response.headers['Access-Control-Allow-Origin'] = "*" self.response.set_status('400') self.response.write(e.message)