def login(request): """ login :author: lishanZheng :date: 2019/12/31 """ if request.method == 'POST' and request.POST: account = request.POST.get('account') password = request.POST.get('password') correct_password = '' admin = '' try: admin = Administrator.objects.get(account=account) correct_password = admin.password except Administrator.DoesNotExist: return result_util.error(error_code=code.ADMIN_NOT_EXIST, message='管理员不存在') if admin.state == 0: return result_util.error(error_code=code.ADMIN_NOT_EXIST, message='管理员不存在') if compare(password, correct_password): admin.password = '' request.session['is_login'] = True admin = AdministratorSerializer(admin).data request.session['admin'] = admin return result_util.success(admin) return result_util.error(error_code=code.INCORRECT_PASSWORD, message='密码错误') return result_util.error(error_code=code.EMPTY_REQUEST, message='请求体空')
def post(self, request): """ add administrator :author: lishanZheng :date: 2020/01/06 """ account = request.data.get('account') admin = Administrator.objects.filter(account=account) if admin.count() > 0: admin_old = Administrator.objects.get(account=account) if admin_old.state == 0: admin_old.delete() else: return result_util.error(error_code=code.ADMIN_EXIST, message='此管理员账户已经存在') privilege_data = request.data.get('privilege') self.privilege = Privilege.objects.create(**privilege_data) data = request.data data['password'] = encrypt(data.get('password')) administrator = self.get_serializer(data=data) if administrator.is_valid(): administrator.save() admin = administrator.data admin['password'] = '' return result_util.success(admin)
def post(self, request): """ create clazz student :author: gexuewen :date: 2020/01/08 """ data = request.data.copy() student_id = data.get('student_id') clazz_id = data.get('clazz_id') recommendation_peoples = data.get('recommendation_people') clazz_student = ClazzStudent.objects.filter(clazz_id=clazz_id, student_id=student_id) if clazz_student.count() > 0: return result_util.error(ALREADY_APPLY, '已经参加过') self.evaluation = Evaluation.objects.create() apply_data = data.get('application') self.apply = ApplicationInformation.objects.create(**apply_data) if recommendation_peoples: for recommendation_people in recommendation_peoples: RecommendationPeople.objects.create(information=self.apply, **recommendation_people) self.student = Student.objects.filter(id=student_id).first() self.clazz = Clazz.objects.filter(id=clazz_id).first() data.update({'state': WAIT_FOR_AUDIT}) clazz_student_serializer = self.get_serializer(data=data) clazz_student_serializer.is_valid(raise_exception=True) clazz_student_serializer.save() self.clazz.current_people_number = self.clazz.current_people_number + 1 self.clazz.save() return result_util.success_empty()
def logout(request): """ logout :author: lishanZheng :date: 2019/12/31 """ if request.session.get('is_login'): request.session['is_login'] = None request.session['admin'] = None return result_util.success_empty() return result_util.error(error_code=code.NOT_LOGIN, message='未登录')
def login(request): """ student login :author: gexuewen :date: 2020/01/06 """ code = request.POST.get('code') avatar_url = request.POST.get('avatar_url') gender = request.POST.get('gender') gender = get_gender(gender) if gender not in [gender_choice.MALE, gender_choice.FEMALE]: gender = gender_choice.MALE if (not avatar_url) or (not avatar_url.startswith('http')): return result_util.error(INVALID_AVATAR_URL, '头像链接错误') result = send_login_request(code) if 'errcode' in result: return result_util.error(INVALID_JS_CODE, result.get('errmsg')) open_id = result.get('openid') session_key = result.get('session_key') session_id = get_session_id(open_id) wechat_student = WechatStudent.objects.filter(open_id=open_id).first() if wechat_student is None: student = Student.objects.create(avatar_url=avatar_url, gender=gender) wechat_student = WechatStudent.objects.create(open_id=open_id, session_key=session_key, session_id=session_id, student=student) else: wechat_student.session_key = session_key wechat_student.save() wechat_student.student.avatar_url = avatar_url wechat_student.student.gender = gender wechat_student.student.save() student_serializer = StudentSerializer(wechat_student.student) data = { 'session_id': wechat_student.session_id, 'student': student_serializer.data } return result_util.success(data)
def get(self, request, activity_id, param): """ get activity student record :author: gexuewen :date: 2020/01/11 """ student_id = int(param) record = ActivityStudent.objects.filter(activity_id=activity_id, student_id=student_id) record = record.first() if record: activity_student_serializer = ActivityStudentSerializer(record) return result_util.success(activity_student_serializer.data) return result_util.error(ACTIVITY_STUDENT_NOT_FOUND, '未参与此活动')
def put(self, request, primary_key): """ update semester :author: lishanZheng :date: 2020/01/03 """ key = primary_key state = request.data.get('state') if state is not None: state = int(state) if state == CLOSED: clazz_list = Clazz.objects.filter(semester_id=key) count = len(clazz_list) clazz_closed = Clazz.objects.filter(semester_id=key, state=clazz_state.CLOSED) count_closed = len(clazz_closed) if count != count_closed: return result_util.error(CLAZZ_NOT_CLOSE, '存在课程没有结束') res = self.partial_update(request, key) return result_util.success(res.data)
def post(self, request): """ create activity clazz :author: gexuewen :date: 2020/01/03 """ activity_id = request.data.get('activity_id') clazz_id = request.data.get('clazz_id') self.activity = Activity.objects.filter(id=activity_id).first() self.clazz = Clazz.objects.filter(id=clazz_id).first() activity_clazz = ActivityClazz.objects.filter(activity_id=activity_id, clazz_id=clazz_id) if activity_clazz.count() > 0: return result_util.error(CLAZZ_ALREADY_IN, '此班级已经在活动列表中') data = {'activity_id': activity_id, 'clazz_id': clazz_id} activity_clazz = ActivityClazz.objects.create(**data) activity_clazz_serializer = self.get_serializer( instance=activity_clazz) return result_util.success(activity_clazz_serializer.data)
def post(self, request): """ create activity student :author: gexuewen :date: 2020/01/03 """ activity_id = request.data.get('activity_id') student_id = request.data.get('student_id') self.activity = Activity.objects.filter(id=activity_id).first() self.student = Student.objects.filter(id=student_id).first() activity_student = ActivityStudent.objects.filter( activity_id=activity_id, student_id=student_id) if activity_student.count() > 0: return result_util.error(ALREADY_JOIN, '已经参加过') data = {'state': WAIT_FOR_PAY} activity_student_serializer = self.get_serializer(data=data) activity_student_serializer.is_valid(raise_exception=True) activity_student_serializer.save() return result_util.success(activity_student_serializer.data)
def upload(request): """ upload :author: lishanZheng :date: 2020/01/09 """ file = request.FILES.get("file", None) # 获取上传的文件,如果没有文件,则默认为None if not file: return error(MISS_FILE, '没有选择文件') time_str = str(time.time()).replace('.', '') file_name = 'media/{0}{1}'.format(time_str, file.name) path = os.path.join(os.path.join(settings.FILE_ROOT_DIR, file_name)) # 根据路径打开指定的文件(以二进制读写方式打开) destination = open(path, 'wb+') # 分块写入文件 for chunk in file.chunks(): destination.write(chunk) destination.close() url = settings.FILE_HOST + '/' + file_name res = {'url': url} return success(res)