def notice(request): data = request.REQUEST size = int(data.get("size", 50)) timestamp = int(data.get("time")) if not timestamp: return json_response_error(PARAM_REQUIRED, "time") time_type = data.get("type", "after") if time_type not in ["before", "after"]: return json_response_error(PARAM_REQUIRED, "type") size = min(size, 100) user = request.META["user"] if time_type == "before": notices = ( Notice.objects.filter(receive_user_id=user.id) .filter(status=0) .filter(create_time__lt=timestamp_to_datetime(timestamp)) .order_by("-id") .all()[:size] ) else: notices = ( Notice.objects.filter(receive_user_id=user.id) .filter(status=0) .filter(create_time__gte=timestamp_to_datetime(timestamp)) .order_by("-id") .all()[:size] ) result = {"notices": []} for notice in notices: try: reply_id = notice.comment_id comment = Comment.objects.get(id=reply_id) secret = Secret.objects.get(id=comment.secret_id) r = { "id": notice.id, "reply_id": reply_id, "secret": { "id": secret.id, "group_id": secret.group_id, "content": secret.content, "time": datetime_to_timestamp(secret.create_time), }, "reply_time": datetime_to_timestamp(comment.create_time), "reply_content": comment.content, } if comment.reply_to_id: replied_content = Comment.objects.get(id=comment.reply_to_id).content r["replied_content"] = replied_content result["notices"].append(r) except Exception as e: return json_response_error(DATA_ERROR, e) return json_response_ok(result)
def comments(request, secret_id): data = request.REQUEST size = int(data.get("size", 50)) timestamp = int(data.get("time")) if not timestamp: return json_response_error(PARAM_REQUIRED, "time") time_type = data.get("type", "after") if time_type not in ["before", "after"]: return json_response_error(PARAM_REQUIRED, "type") size = min(size, 100) if time_type == "before": comments = ( Comment.objects.filter(secret_id=secret_id) .filter(create_time__lt=timestamp_to_datetime(timestamp)) .order_by("-id") .all()[:size] ) else: comments = ( Comment.objects.filter(secret_id=secret_id) .filter(create_time__gte=timestamp_to_datetime(timestamp)) .order_by("-id") .all()[:size] ) result = {"comments": []} for comment in comments: if comment.reply_to_id: replied_floor = Comment.objects.get(id=comment.reply_to_id).floor result["comments"].append( { "id": comment.id, "content": comment.content, "time": datetime_to_timestamp(comment.create_time), "floor": comment.floor, "replied_floor": replied_floor, } ) else: result["comments"].append( { "id": comment.id, "content": comment.content, "time": datetime_to_timestamp(comment.create_time), "floor": comment.floor, } ) return json_response_ok(result)
def add_comments(request, secret_id): data = request.POST content = data.get("content") if not content: return json_response_error(PARAM_REQUIRED, "content") reply_to = data.get("reply_to") user = request.META["user"] new_comment = Comment.objects.filter(secret_id=secret_id).order_by("-floor").all() if new_comment: floor = new_comment[0].floor + 1 else: floor = 1 try: if reply_to: reply_to = int(reply_to) replied_comment = Comment.objects.get(id=reply_to) receive_user_id = replied_comment.author_id comment = Comment._add_comment(content, user.id, secret_id, reply_to, floor) else: replied_secret = Secret.objects.get(id=secret_id) receive_user_id = replied_secret.author_id comment = Comment._add_comment(content, user.id, secret_id, reply_to, floor) Notice._add_notice(receive_user_id, comment) except Exception as e: return json_response_error(DATA_ERROR, e) result = {"id": comment.id, "time": datetime_to_timestamp(comment.create_time), "floor": floor} return json_response_ok(result)
def secrets(request, group_id): data = request.POST size = int(data.get("size", 50)) timestamp = int(data.get("time")) if not timestamp: return json_response_error(PARAM_REQUIRED, "time") time_type = data.get("type") if time_type not in ["before", "after"]: return json_response_error(PARAM_REQUIRED, "type") size = min(size, 100) if time_type == "before": secrets = ( Secret.objects.filter(create_time__lt=timestamp_to_datetime(timestamp), group_id=group_id) .order_by("-id") .all()[:size] ) else: secrets = ( Secret.objects.filter(create_time__gte=timestamp_to_datetime(timestamp), group_id=group_id) .order_by("-id") .all()[:size] ) result = {"secrets": []} for secret in secrets: hot = Comment.objects.filter(secret_id=secret.id).count() result["secrets"].append( {"id": secret.id, "content": secret.content, "hot": hot, "time": datetime_to_timestamp(secret.create_time)} ) return json_response_ok(result)
def add_secrets(request, group_id): data = request.POST content = data.get("content") if not content: return json_response_error(PARAM_REQUIRED, "content") user = request.META["user"] try: secret = Secret._add_secret(user, group_id, content) except Exception as e: return json_response_error(DATA_ERROR, e) result = {"id": secret.id, "time": datetime_to_timestamp(secret.create_time)} return json_response_ok(result)
def sample(request): size = 20 secrets = Secret.objects.order_by("-id").all()[:size] result = {"secrets": []} for secret in secrets: hot = Comment.objects.filter(secret_id=secret.id).count() result["secrets"].append( { "id": secret.id, "content": secret.content, "group_id": secret.group_id, "hot": hot, "time": datetime_to_timestamp(secret.create_time), } ) return json_response_ok(result)
def mine(request): data = request.REQUEST size = int(data.get("size", 50)) timestamp = int(data.get("time")) if not timestamp: return json_response_error(PARAM_REQUIRED, "time") time_type = data.get("type", "after") if time_type not in ["before", "after"]: return json_response_error(PARAM_REQUIRED, "type") size = min(size, 100) user = request.META["user"] if time_type == "before": secrets = ( Secret.objects.filter(author_id=user.id) .filter(create_time__lt=timestamp_to_datetime(timestamp)) .order_by("-id") .all()[:size] ) else: secrets = ( Secret.objects.filter(author_id=user.id) .filter(create_time__gte=timestamp_to_datetime(timestamp)) .order_by("-id") .all()[:size] ) result = {"secrets": []} for secret in secrets: result["secrets"].append( { "id": secret.id, "time": datetime_to_timestamp(secret.create_time), "content": secret.content, "group_id": secret.group_id, } ) return json_response_ok(result)
def group_list(request): user = request.META["user"] user_group_list = GroupUserRelation.objects.filter(user_id=user.id, status=0).all() group_id_list = [relation.group_id for relation in user_group_list] relation_time_dict = dict([(relation.group_id, relation.update_time) for relation in user_group_list]) group_list = Group.objects.filter(id__in=group_id_list).all() result = [] for group in group_list: result.append( { "id": group.id, "name": group.name, "pinyin": group.pinyin, "py_first": group.py_first, "time": datetime_to_timestamp(relation_time_dict[group.id]), } ) return json_response_ok(result)
secrets = ( Secret.objects.filter(create_time__gte=timestamp_to_datetime(timestamp)).order_by("-id").all()[:size] ) except Exception, e: return json_response_error(DATA_ERROR, e) result = {"secrets": []} for secret in secrets: hot = Comment.objects.filter(secret_id=secret.id).count() result["secrets"].append( { "id": secret.id, "content": secret.content, "group_id": secret.group_id, "hot": hot, "time": datetime_to_timestamp(secret.create_time), } ) return json_response_ok(result) @user_auth def my_secrets(request): data = request.POST size = data.get("size", 50) timestamp = data.get("time") if not timestamp: return json_response_error(PARAM_REQUIRED, "time") time_type = data.get("type", "after") if time_type not in ["before", "after"]: