def test_add_studnet(self): name = 'mew' gender = Gender.male grade = '2014' password = '******' mobile = '13000000000' id_ = Student.add(name, gender, grade, StudentType.double_major, password, mobile, AccountStatus.need_verify) student = Student.get(id_) assert student assert student.id account = Account.get(id_) assert account assert account.id == id_ assert student.password != password assert student.need_verify() assert student.mobile == mobile student.to_normal() student = Student.get(id_) assert student.is_normal()
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 get_current_student(): wechat_user = g.wechat_user id_ = wechat_user.id student = Student.get(id_) if not student: return normal_jsonify({}, 'Student Not Found', 404) return jsonify(student.dump())
def create_user(): wechat_user = g.wechat_user open_id = wechat_user.open_id data = student_schema.CreateStudentSchema().fill() mobile = data['mobile'] validator.validate_phone(mobile) verify_code = data['verify_code'] try: r = SMSVerify.verify(mobile, verify_code, SMSVerifyType.register) except AtemptTooManyTimesError as e: return normal_jsonify({}, e.message, e.http_status_code) if not r: raise InvalidSMSVerifyCodeError() type_ = StudentType(data['type']) grade = data['grade'] id_ = Student.add(wechat_user.id, mobile, open_id, type_, grade, AccountStatus.need_verify) student = Student.get(id_) SMS.reg_complete.delay(mobile=mobile) Student.cache_avatar.delay(student.id, student.avatar_url) return normal_jsonify(student.dump())
def get_share_post_image(post_id): data = GetSharePostImageSchema().fill() path = data.get('path', 'pages/common/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) return send_file(img_io, mimetype='image/jpeg')
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) return send_file(img_io, mimetype='image/jpeg')
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_token(): student = Student.get(g.wechat_user.id) if not student: raise UserNotFoundError() data = GetUploadTokenSchema().fill() ext = data.get('ext') now = datetime.now().strftime('%y%m%d%H%M%S') file_name = 'bm-post-pic-s%s-t%s.%s' % (student.id, now, ext) id_ = FilePhoto.add(student.id, qiniu_client.bucket_name, file_name) token = qiniu_client.get_token(file_name=file_name) return normal_jsonify(dict(id=id_, token=token))
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 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 get_my_post(): student = Student.get(g.wechat_user.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) return jsonify([post.dump() for post in posts])
def test_password(self): name = 'seeyoon' gender = Gender.male grade = '2014' password = '******' mobile = '13000000001' id_ = Student.add(name, gender, grade, StudentType.double_major, password, mobile, AccountStatus.need_verify) student = Student.get(id_) assert student.verify_password(password) assert not student.verify_password('wrongpasswd')
def test_change_mobile(self): name = 'tim' gender = Gender.male grade = '2014' password = '******' mobile = '13000000002' id_ = Student.add(name, gender, grade, StudentType.double_major, password, mobile, AccountStatus.need_verify) student = Student.get(id_) new_mobile = '15600000000' student.change_mobile(new_mobile) assert student.mobile == new_mobile
def _add_student(): import random from black_market.model.user.student import Student from black_market.model.user.consts import Gender, StudentType, AccountStatus name = 'mew' + str(random.randint(0, 100)) + str(random.randint(0, 100)) gender = Gender.male grade = '2014' password = '******' mobile = '1300000' + str(random.randint(0, 9)) + str(random.randint(0, 9)) + \ str(random.randint(0, 9)) + str(random.randint(0, 9)) id_ = Student.add( name, gender, grade, StudentType.double_major, password, mobile, AccountStatus.need_verify) return Student.get(id_)
def test_change_password(self): name = 'zhengnan' gender = Gender.male grade = '2014' password = '******' mobile = '13000000001' id_ = Student.add(name, gender, grade, StudentType.double_major, password, mobile, AccountStatus.need_verify) student = Student.get(id_) salt = student.salt new_password = '******' student.change_password(new_password) assert student.verify_password(new_password) assert not student.verify_password(password) assert salt != student.salt
def clear_user(id_): data = request.args pwd = data.get('pwd') if pwd != RAW_SALT: return normal_jsonify({'status': 'failed'}) from black_market.model.user.student import Student from black_market.model.wechat.session import WechatSession from black_market.model.post.course import CoursePost student = Student.get(id_) name = student.username wechat_user = student.wechat_user wechat_session = WechatSession.get_by_open_id(wechat_user.open_id) posts = CoursePost.gets_by_student(student.id, limit=100, offset=0) for post in posts: post.delete() student.delete() wechat_user.delete() wechat_session.delete() return normal_jsonify({'status': 'Student %s has been removed.' % name})
def get_student_share_profile(student_id): student = Student.get(student_id) if not student: return normal_jsonify({}, 'Student Not Found', 404) return jsonify(student.share_dump())
def get_remaining_viewcount(): student = Student.get(g.wechat_user.id) if not student: return normal_jsonify({}, 'Student Not Found', 404) viewcount = student.remaining_viewcount return normal_jsonify(dict(viewcount=viewcount))
def student(self): return Student.get(self.student_id)