def get_share_post_image(post_id): data = GetSharePostImageSchema().fill() path = data.get('path', 'pages/splash/splash') supply = data.get('supply', None) demand = data.get('demand', None) student_id = data.get('student_id', None) post = CoursePost.get(post_id) if not post: raise PostNotFoundError() student = Student.get(post.student_id) if not student: raise UserNotFoundError() supply_course_name = None demand_course_name = None supply_course = Course.get(supply) if supply else None demand_course = Course.get(demand) if demand else None if supply_course: supply_course_name = supply_course.name if demand_course: demand_course_name = demand_course.name img_io = create_share_post_image(student, path, supply_course_name, demand_course_name) img_io.seek(0) UserBehavior.add(student_id or student.id, UserBehaviorType.get_share_course_post_image) return send_file(img_io, mimetype='image/jpeg')
def get_student(student_id): student = Student.get(student_id) if not student: return normal_jsonify({}, 'Student Not Found', 404) UserBehavior.add(g.wechat_user.id, UserBehaviorType.view_other_profile, dict(student_id=student_id)) return jsonify(student.dump())
def edit_post(post_id): data = UpdateCoursePostSchema().fill() post = CoursePost.get(post_id) post.update_self(data) UserBehavior.add(g.wechat_user.id, UserBehaviorType.edit_course_post, dict(post_id=post_id)) return normal_jsonify({'status': 'ok'})
def edit_post_status(post_id): data = UpdateCoursePostStatusSchema().fill() status = PostStatus(data['status']) post = CoursePost.get(post_id) post.update_status(status) UserBehavior.add(g.wechat_user.id, UserBehaviorType.markdone_course_post, dict(post_id=post_id, status=status.value)) return normal_jsonify({'status': 'ok'})
def get_post(post_id): post = CoursePost.get(post_id) student = Student.get(g.wechat_user.id) if student.id != post.student_id: post.pv += 1 has_viewed_contact = True if ViewRecord.gets(student.id, post_id) else False UserBehavior.add(g.wechat_user.id, UserBehaviorType.view_course_post, dict(post_id=post_id)) return normal_jsonify( dict(post=post.dump(), has_viewed_contact=has_viewed_contact))
def update_user(): data = student_schema.UpdateStudentSchema().fill() type_ = StudentType(data['type']) grade = data['grade'] wechat_user = g.wechat_user id_ = wechat_user.id student = Student.get(id_) student = student.update(type_, grade) UserBehavior.add(g.wechat_user.id, UserBehaviorType.edit_profile, dict(type_=type_.value, grade=grade)) return normal_jsonify(student.dump())
def get_share_student_image(student_id): data = GetShareStudentImageSchema().fill() path = data.get('path') or 'pages/index?query=1' student = Student.get(student_id) if not student: raise UserNotFoundError() img_io = create_share_me_image(student, path) img_io.seek(0) UserBehavior.add(student_id, UserBehaviorType.get_share_me_image) return send_file(img_io, mimetype='image/jpeg')
def decr_remaining_viewcount(): student = Student.get(g.wechat_user.id) if not student: return normal_jsonify({}, 'Student Not Found', 404) data = DecrRemainingViewCountSchema().fill() post_id = data['post_id'] student.decr_viewcount() ViewRecord.add(student.id, post_id, PostType.course_post) UserBehavior.add(g.wechat_user.id, UserBehaviorType.view_course_post_contact, dict(post_id=post_id)) return normal_jsonify({})
def callback(): data = request.form key = data.get('key') filesize = data.get('filesize') hash = data.get('hash') photo = FilePhoto.get_by_file_name(key) if not photo: raise photo.update(filesize=filesize, hash=hash) UserBehavior.add(photo.user_id, UserBehaviorType.upload_photo, dict(key=key)) return normal_jsonify()
def create_post(): data = CreateGoodsPostSchema().fill() student_id = data['student_id'] switch = PostMobileSwitch(data['switch']) mobile = data['mobile'] wechat = data['wechat'] message = data['message'] imgs = data['imgs'] post = GoodsPost.add(student_id, switch, mobile, wechat, imgs, message) UserBehavior.add(g.wechat_user.id, UserBehaviorType.create_goods_post, dict(post_id=post.id)) return normal_jsonify(post.dump())
def share_post(): data = SharePostSchema().fill() post_id = data.get('post_id') post_type = PostType(data.get('post_type')) student_id = data.get('student_id', 0) detail = dict(post_id=post_id, student_id=student_id) if post_type is PostType.course_post: UserBehavior.add(student_id, UserBehaviorType.share_course_post, detail) elif post_type is PostType.goods_post: UserBehavior.add(student_id, UserBehaviorType.share_goods_post, detail) return normal_jsonify()
def create_post(): data = CreateCoursePostSchema().fill() student_id = data['student_id'] supply = data['supply'] demand = data['demand'] switch = PostMobileSwitch(data['switch']) mobile = data['mobile'] wechat = data['wechat'] message = data['message'] post = CoursePost.add(student_id, supply, demand, switch, mobile, wechat, message) UserBehavior.add(g.wechat_user.id, UserBehaviorType.create_course_post, dict(post_id=post.id)) return normal_jsonify(post.dump())
def get_posts_by_student(student_id): student = Student.get(student_id) if not student: return normal_jsonify({}, 'Student Not Found', 404) data = student_schema.GetMyCoursePostSchema().fill() start = data.get('start', 0) limit = data.get('limit', 10) order = OrderType(data.get('order', 0)) posts = CoursePost.gets_by_student(student.id, limit=limit, offset=start, order=order) UserBehavior.add(g.wechat_user.id, UserBehaviorType.view_other_posts, dict(student_id=student_id)) return jsonify([post.dump() for post in posts])
def share_student(): data = ShareStudentSchema().fill() student_id = data.get('student_id') UserBehavior.add(student_id, UserBehaviorType.share_me_to_friend) return normal_jsonify()