def get_todo_by_page(uid, mode=1, page_index=1, page_size=10): user = get_user_by_id(uid) # print(user) if user is None: return False, "user doesn't exist" todo_obj = models.ToDo.query.filter_by(uid=uid) total = len(todo_obj.all()) if mode == 1: todo_list = todo_obj.order_by(models.ToDo.release_time.desc()).paginate(page_index, page_size,False) elif mode == 2: todo_list = todo_obj.order_by(models.ToDo.start_time.desc()).paginate(page_index, page_size, False) else: todo_list = todo_obj.order_by(models.ToDo.deadline_time.desc()).paginate(page_index,page_size,False) ret = [] if todo_list: print(todo_list.items) for i in todo_list.items: ret.append(i.to_dict()) data = {'todo_list': ret} pagination = {'limit': len(ret), 'total': total} return True, {'data': data, 'pagination': pagination}
def get_personal_post_by_page(uid, page_index, page_size, topic_id): user = get_user_by_id(uid) print(user) if user is None: return False, "user doesn't exist" # personal_post_list = [marshal(item, marshal_structure) for item in user.posts if # item.visibility == 1 and item.post_type != 2] # personal_post_list.reverse() # ret = personal_post_list if topic_id == -1: personal_post_sort_obj = Post.query.filter( Post.visibility == 1, Post.post_type != 2, Post.uid == uid).order_by(Post.post_time.desc()) else: personal_post_sort_obj = Post.query.filter( Post.visibility == 1, Post.post_type != 2, Post.uid == uid, Post.topic_id == topic_id).order_by(Post.post_time.desc()) personal_post_sort_list = personal_post_sort_obj.paginate( page_index, page_size, False) marshal_structure = SINGLE_POST_STRUCTURE.copy() ret = marshal(personal_post_sort_list.items, marshal_structure) pagination = { 'limit': len(ret), 'total': len(personal_post_sort_obj.all()) } return True, {'data': ret, 'pagination': pagination}
def comment_like_delete(comment_like_id, uid): user = get_user_by_id(uid) if user is None: return False, 404, "user doesn't exist" result = CommentThumbUp.query.filter_by(id=comment_like_id).first() if result is None: return False, 404, "comment_like_id doesn't exist" if uid != result.uid: return False, 403, "comment_like_id doesn't match with uid " return delete_from_database(result)
def todo_add(uid, label, title, content, start_time, deadline_time, priority, status, img_link=None): user = get_user_by_id(uid) if user is None: return False, "user doesn't exist" new_todo = models.ToDo(label, title, content, start_time, deadline_time, priority, status, img_link) user.todo_list.append(new_todo) ret = insert_to_database(models.ToDo,new_todo) # print(user) # print(new_todo) # print(ret) return ret
def comment_like_add(uid, comment_id): user = get_user_by_id(uid) if user is None: return False, 404, "user doesn't exist" com = query_single_by_id(Comment, comment_id) if com is None: return False, 404, "comment doesn't exist" if has_comment_liked(uid, comment_id): return False, 403, "can't like the same comment more than once" kwargs = {'uid': uid, 'comment_id': comment_id} comment = CommentThumbUp(**kwargs) user.comment_thumb_ups.append(comment) ret = insert_to_database(CommentThumbUp, comment) return ret
def insert_eva(uid, class_id, eva_content, eva_tags, eva_score, eva_status, cid, teacher_id): user = get_user_by_id(uid) tc = get_tc_by_tid_cid(teacher_id, cid) user_class = get_class_by_class_id(class_id) years_semester = user_class.years + "-" + str(user_class.semester) new_eva = Evaluation(eva_content, eva_tags, eva_score, eva_status, years_semester) user_class.evaluations.append(new_eva) user.evaluations.append(new_eva) tc.evaluations.append(new_eva) return insert_to_database(Evaluation, new_eva)
def anonymous_delete(arg_dict): uid = arg_dict['uid'] pid = arg_dict['id'] user = get_user_by_id(uid) if user is None: return False, 404, "user doesn't exist" result = Post.query.filter_by(id=pid, visibility=VISIBILITY_VISIBLE).first() if result is None: return False, 404, "anonymous post id doesn't exist" super_users = User.query.with_entities( User.id).filter_by(level=User.LEVEL_MANAGER).all() super_ids = [user.id for user in super_users] if result.real_uid != uid and uid not in super_ids: return False, 403, "anonymous post id doesn't match with uid " result.visibility = VISIBILITY_INVISIBLE db.session.add(result) db.session.commit() return True, {"status": "deleted"}
def get_personal_eva_by_uid(uid, mode=1, page_index=1, page_size=10): user = get_user_by_id(uid) print(user) if user is None: return False, "user doesn't exist" eva_obj = Evaluation.query.filter_by(uid=uid) total = len(eva_obj.all()) if mode == 1: eva_list = eva_obj.order_by(Evaluation.eva_time.desc()).paginate( page_index, page_size, False) else: eva_list = eva_obj.order_by(Evaluation.eva_score.desc()).paginate( page_index, page_size, False) ret = [] if eva_list: for i in eva_list.items: ret.append(i.to_dict()) data = {'eva_list': ret} pagination = {'limit': len(ret), 'total': total} return True, {'data': data, 'pagination': pagination}