def text(self): user = models.User().find(self.operator_id) text = '' if self.action == ACTIVITY_ACTION['PHOTO_LIKE']: photo = models.Photo().find(self.target_id) text = u'<a href="/user/{user.username}">{user.fullname}</a> 喜欢照片 <a href="/photo/{photo_id}">{photo_title}</a>'.format( user=user, photo_id=photo.id, photo_title=photo.title or u"无标题", ) elif self.action == ACTIVITY_ACTION['PHOTO_COMMENT_ADD']: photo = models.Photo().find(self.context_id) text = u'<a href="/user/{user.username}">{user.fullname}</a> 评论了照片 <a href="/photo/{photo_id}">{photo_title}</a>'.format( user=user, photo_id=photo.id, photo_title=photo.title or u"无标题", ) elif self.action == ACTIVITY_ACTION['BLOG_COMMENT_ADD']: post = models.Blog().find(self.context_id) title = u'评论了你在博文' url = '/blog/post/{post.id}' if post.status == 1: title = u'评论了你在反馈' url = '/feedback' text = u'<a href="/user/{user.username}">{user.fullname}</a> {title} <a href="{url}">{post.title}</a> 中的评论'.format( user=user, title=title, post=post, url=url, ) elif self.action == ACTIVITY_ACTION['USER_FOLLOW']: text = u'<a href="/user/{user.username}">{user.fullname}</a> 关注了你'.format( user=user) return text
def upload_photo(): """ Upload a photo, bypassing the API for cleaner invocation. """ data = request.form # Create a new photo. photo = { 'credit': data.get('credit', ''), 'caption': data.get('caption', ''), 'file_name': data.get('file_name', ''), } photo = models.Photo(**photo) # Get the data. if data.get('file_string', None): photo.write_photo(data['file_string']) # Do the save. photo.save() # Return data. serializer = Serializer() data = serializer.serialize_object(photo) return jsonify(data)
def _photo_like(photo_like): """ re calculate user level here """ user = models.User().find(models.Photo().find( photo_like.photo_id).user_id) redis_client = get_redis_client() redis_client.hincrby(REDIS_KEY['USER_LIKED_COUNT'], user.id, 1) current_likes_count = redis_client.hget(REDIS_KEY['USER_LIKES_COUNT'], photo_like.user_id) or 0 redis_client.hset(REDIS_KEY['USER_LIKES_COUNT'], photo_like.user_id, int(current_likes_count) + 1) calculated_user_level = calculate_user_level(user) if calculated_user_level > user.level: redis_key = REDIS_KEY['USER_MESSAGE'].format(user_id=user.id) models.Invite_Key().gen_by_level(user, calculated_user_level, user.level) msg = u"{0}|恭喜你,成功升级到{1}".format( 'info', USER_LEVEL_CN[calculated_user_level]) redis_client.lpush(redis_key, msg) user.level = calculated_user_level user.save()
def _render(self, kind, photo = None, tag_name = None): template_prefix = 'partial/' if self.is_ajax_request else '' offset = (int(self.get_argument('page', 1)) - 1) * PHOTOS_PER_PAGE if kind == 'hot': page_path = '/photos/hot' photos_title = u'热门照片' photos_type = 'hot' photos = (models.Photo().order_by('-karma') .findall_by_status(0, limit = PHOTOS_PER_PAGE, offset = offset)) total_items = models.Photo().count_by_status(0) if kind == 'latest': page_path = '/photos/latest' photos_title = u'最新照片' photos_type = 'latest' photos = models.Photo().findall_by_status(0, limit = PHOTOS_PER_PAGE, offset = offset) total_items = models.Photo().count_by_status(0) elif kind == 'mine_upload': page_path = '/mine/photos' photos_title = u'我添加的照片' photos_type = 'user' photos = models.Photo().findall_by_user_id_and_status( self.current_user.id, 0, limit = PHOTOS_PER_PAGE, offset = offset) total_items = models.Photo().count_by_user_id_and_status(self.current_user.id, 0) elif kind == 'mine_likes': page_path = '/mine/likes_photos' photos_title = u'我喜欢的照片' photos_type = 'user' photo_ids = models.Photo_Like().findall_by_user_id( self.current_user.id, limit = PHOTOS_PER_PAGE, offset = offset)\ .get_field('photo_id') photos = [] for photo_id in photo_ids: photos.append(models.Photo().find(photo_id)) total_items = models.Photo_Like().count_by_user_id(self.current_user.id) elif kind == 'tag': page_path = u'/tag/{0}'.format(tag_name) photos_title = u'带有"{0}"标签的照片'.format(tag_name) photos_type = u'tag/{0}'.format(tag_name) photo_ids = models.Photo_Tag().findall_by_tag( tag_name, limit = PHOTOS_PER_PAGE, offset = offset)\ .get_field('photo_id') photos = [] for photo_id in photo_ids: photos.append(models.Photo().find(photo_id)) total_items = models.Photo_Tag().count_by_tag(tag_name) return self.render('{0}photos.html'.format(template_prefix), photos_title = photos_title, photos_type = photos_type, photos = photos, total_items = total_items, page_path = page_path, current_photo = photo, )
def get(self, photo_id): photo = models.Photo().find(photo_id) if not photo or photo.status != 0: return self.render('error/photo_not_exists.html') # used in BaseUserPhotosHandler self.user = models.User().find(photo.user_id) return self._render('photos', photo=photo)
def post(self, photo_id): photo = models.Photo().find(photo_id) if not photo: return self.render('error/photo_not_exists.html') if not self.current_user.has_liked_photo(photo): models.Photo_Like().like(self.current_user.id, photo_id) else: models.Photo_Like().unlike(self.current_user.id, photo_id) return self.send_success_json()
def insert_test_data(): models.Photo.objects.all().delete() models.Event.objects.all().delete() e = models.Event(name='Событие 1', date='2010-02-05', description=u'Описание события') e.save() models.Photo(event=e, description=u'описание', photo='uploads/test.jpg').save()
def wrapper(self, photo_id, *args, **kwargs): photo = models.Photo().find(photo_id) if not photo or photo.status != 0: return self.send_error_json({'message': 'photo not exists'}) if photo.user_id != self.current_user.id: return self.send_error_json({'message': 'permission denied'}) self.photo = photo return func(self, photo_id, *args, **kwargs)
def left_upload_count(self): if self.is_admin: return 100 init_count = USER_LEVEL_PHOTOS_PER_WEEK[self.level] now = datetime.datetime.now() dt = now - datetime.timedelta(days=datetime.datetime.weekday(now)) start_week_timestamp = time.mktime( datetime.datetime.date(dt).timetuple()) created_count = models.Photo().where('created', '>', start_week_timestamp)\ .where('status', '=', 0)\ .where('user_id', '=', self.id)\ .count() return init_count - created_count
def get(self, photo_id): photo = models.Photo().find(photo_id) if not photo or photo.status != 0: return self.render('error/photo_not_exists.html') self._incr_view_counts(photo) return self.render( 'partial/photo.html', photo=photo, photo_exif=models.Photo_Exif().get(photo_id), photo_tags=models.Photo_Tag().findall_by_photo_id(photo_id), )
def _photo_unlike(photo_like): """ re calculate user level here """ user = models.User().find(models.Photo().find( photo_like.photo_id).user_id) redis_client = get_redis_client() redis_client.hincrby(REDIS_KEY['USER_LIKED_COUNT'], user.id, -1) current_likes_count = redis_client.hget(REDIS_KEY['USER_LIKES_COUNT'], photo_like.user_id) or 0 redis_client.hset(REDIS_KEY['USER_LIKES_COUNT'], photo_like.user_id, int(current_likes_count) - 1)
def _render(self, kind, photo = None): template_prefix = 'partial/' if self.is_ajax_request else 'user_' offset = (int(self.get_argument('page', 1)) - 1) * PHOTOS_PER_PAGE fullname = self.user.fullname if self.current_user and self.user.id == self.current_user.id: fullname = u'我' if kind == 'photos': page_path = '/user/{0}/photos'.format(self.user.username) photos_title = u'{0}的照片'.format(fullname) photos_type = 'user' photos = models.Photo().findall_by_user_id_and_status( self.user.id, 0, limit = PHOTOS_PER_PAGE, offset = offset) total_items = models.Photo().count_by_user_id_and_status(self.user.id, 0) return self.render('{0}photos.html'.format(template_prefix), photos_title = photos_title, photos_type = photos_type, photos = photos, total_items = total_items, page_path = page_path, current_photo = photo, )
def submit(request): # Submission to gallery page ctx = {'title':'Submit Contest Entry', 'form':None} my_photo = models.Photo(author=request.user) if request.method == "POST": form = forms.PhotoForm(request.POST, request.FILES, instance=my_photo) if form.is_valid(): photo = form.save() ctx['form'] = form ctx['submission_url'] = settings.SITE_ROOT + reverse(photo_detail, args=[photo.id]) return render_to_response('submit_done.html', ctx, context_instance=RequestContext(request)) else: form = forms.PhotoForm(instance=my_photo) ctx['form'] = form return render_to_response('submit.html', ctx, context_instance=RequestContext(request))
def _photo_like(photo_like): receiver_id = models.Photo().find(photo_like.photo_id).user_id current_notification = Notification().where('receiver_id', '=', receiver_id)\ .where('operator_id', '=', photo_like.user_id)\ .where('action', '=', ACTIVITY_ACTION['PHOTO_LIKE'])\ .find() if not current_notification: Notification( operator_id=photo_like.user_id, receiver_id=receiver_id, action=ACTIVITY_ACTION['PHOTO_LIKE'], created=time.time(), target_id=photo_like.photo_id, is_new=1, ).save() else: current_notification.is_new = 1 current_notification.created = time.time() current_notification.save()
def _photo_comment_add(photo_comment): receiver_ids = set( [models.Photo().find(photo_comment.photo_id).user_id]) mention_users = re.findall(r'@[^\(]+\((.*?)\)', photo_comment.content) if mention_users: for username in mention_users: user_id = models.User().find_by_username(username).id receiver_ids.add(user_id) for receiver_id in receiver_ids: if photo_comment.user_id != receiver_id: Notification( operator_id=photo_comment.user_id, receiver_id=receiver_id, action=ACTIVITY_ACTION['PHOTO_COMMENT_ADD'], created=photo_comment.created, target_id=photo_comment.id, context_id=photo_comment.photo_id, is_new=1, ).save()
def _create_photo(path): file_path, file_name = os.path.split(path) file_name, file_extension = os.path.splitext(file_name) image = { 'file_name': '%s%s' % (file_name, file_extension), 'caption': 'TKTK', 'credit': 'TKTK' } image = models.Photo(**image) with open(path.replace('/musicgame', 'www'), 'rb') as readfile: file_string = ';,%s' % base64.b64encode(readfile.read()) image.write_photo(file_string) image.save() print "Saved image: %s" % image return image
def post(self): photo_id = self.get_argument('photo_id', 0) photo = models.Photo().find(photo_id) if not photo or photo.status != 0: return self.send_error_json({'message': 'photo not exists'}) content = self.get_argument('content', '').strip() if not content: return self.send_error_json({'message': 'empty content'}) comment = models.Photo_Comment( user_id = self.current_user.id, photo_id = photo.id, content = content, ) comment.create() comment_content = self.render_string( 'partial/comment.html', comment = comment, photo = photo, ) return self.send_success_json(content = comment_content)
def post(self): photo = models.Photo( title=self.get_argument('title', ''), content=self.get_argument('content', ''), user_id=self.current_user.id, page_source=self.get_argument('page_source', ''), photo_source=self.get_argument('photo_source', ''), ) photo_id = photo.create() if photo_id: photo.width, photo.height = get_photo_width_height( self.current_user.id, photo.hash) photo.save() tags = self.get_argument('tag', '') if tags: for tag in tags.split(' '): models.Photo_Tag(photo_id=photo_id, tag=tag).save() if self.get_argument('exif_Model', ''): for item in ('Model', 'FocalLength', 'FNumber', 'ExposureTime', 'ISOSpeedRatings', 'Lens'): value = self.get_argument('exif_{0}'.format(item), '') models.Photo_Exif(photo_id=photo_id, key=item, value=value).save() else: exif = get_photo_exif(photo.hash, self.current_user.id) for key, value in exif.items(): models.Photo_Exif(photo_id=photo_id, key=key, value=value).save() self.send_success_json( location='/photo/{0}/via/mine'.format(photo_id)) else: self.send_error_json(photo.errors)
def text(self): user = models.User().find(self.user_id) text = '' if self.action == ACTIVITY_ACTION['PHOTO_LIKE']: photo = models.Photo().find(self.target_id) photo_url = get_photo_url(self, photo, 's') text = u'<a href="/user/{user.username}">{user.fullname}</a> 喜欢照片 <a href="/photo/{photo_id}"><img src="{photo_url}" /></a>'.format( user = user, photo_id = photo.id, photo_url = photo_url, ) if self.action == ACTIVITY_ACTION['PHOTO_UNLIKE']: photo = models.Photo().find(self.target_id) photo_url = get_photo_url(self, photo, 's') text = u'<a href="/user/{user.username}">{user.fullname}</a> 不喜欢照片 <a href="/photo/{photo_id}"><img src="{photo_url}" /></a>'.format( user = user, photo_id = photo.id, photo_url = photo_url, ) elif self.action == ACTIVITY_ACTION['PHOTO_COMMENT_ADD']: photo = models.Photo().find(self.context_id) photo_url = get_photo_url(self, photo, 's') text = u'<a href="/user/{user.username}">{user.fullname}</a> 评论了照片 <a href="/photo/{photo_id}"><img src="{photo_url}" /></a>'.format( user = user, photo_id = photo.id, photo_url = photo_url, ) elif self.action == ACTIVITY_ACTION['BLOG_COMMENT_ADD']: post = models.Blog().find(self.context_id) title = u'评论了博文' url = '/blog/post/{post.id}' if post.status == 1: title = u'评论了反馈' url = '/feedback' text = u'<a href="/user/{user.username}">{user.fullname}</a> {title} <a href="{url}">{post.title}</a> 中的评论'.format( user = user, title = title, post = post, url = url, ) elif self.action == ACTIVITY_ACTION['USER_FOLLOW']: dest_user = models.User().find(self.target_id) text = u'<a href="/user/{user.username}">{user.fullname}</a> 关注了 <a href="/user/{user.username}">{dest_user.fullname}</a>'.format(user = user, dest_user = dest_user) elif self.action == ACTIVITY_ACTION['USER_UNFOLLOW']: dest_user = models.User().find(self.target_id) text = u'<a href="/user/{user.username}">{user.fullname}</a> 取消关注了 <a href="/user/{user.username}">{dest_user.fullname}</a>'.format(user = user, dest_user = dest_user) elif self.action == ACTIVITY_ACTION['USER_CREATE']: text = u'<a href="/user/{user.username}">{user.fullname}</a> 创建了账号' elif self.action == ACTIVITY_ACTION['PHOTO_CREATE']: photo = models.Photo().find(self.target_id) photo_url = get_photo_url(self, photo, 's') text = u'<a href="/user/{user.username}">{user.fullname}</a> 发布了照片 <a href="/photo/{photo.id}"><img src="{photo_url}" /></a>'.format(user = user, photo = photo, photo_url = photo_url) elif self.action == ACTIVITY_ACTION['BLOG_EDIT']: post = models.Blog().find(self.target_id) text = u'<a href="/user/{user.username}">{user.fullname}</a> 编辑了博文 <a href="/blog/post/{post.id}">{post.title}</a>'.format(user = user, post = post) elif self.action == ACTIVITY_ACTION['BLOG_ADD']: post = models.Blog().find(self.target_id) text = u'<a href="/user/{user.username}">{user.fullname}</a> 发布了博文 <a href="/blog/post/{post.id}">{post.title}</a>'.format(user = user, post = post) elif self.action == ACTIVITY_ACTION['BLOG_DELETE']: post = models.Blog().find(self.target_id) text = u'<a href="/user/{user.username}">{user.fullname}</a> 删除了博文 <a href="/blog/post/{post.id}">{post.title}</a>'.format(user = user, post = post) return text
def get(self, photo_id): photo = models.Photo().find(photo_id) if not photo or photo.status != 0: return self.render('error/photo_not_exists.html') kind = 'mine_upload' if self.current_user else 'hot' return self._render(kind, photo=photo)
def get(self, photo_id, tag_name): photo = models.Photo().find(photo_id) if not photo or photo.status != 0: return self.render('error/photo_not_exists.html') return self._render('tag', photo=photo, tag_name=tag_name)