def post(self, request, contest_id): user = request.user try: contest = Contests.objects.exclude( users__contests__users__exact=user).get(pk=contest_id, is_visible=True) except Contests.DoesNotExist: response = Response(Response.STATUS_FAIL, "Соревнование недоступно для регистрации", {}).get() return JsonResponse(response) if not contest.is_deployed: response = Response(Response.STATUS_FAIL, "Соревнование недоступно для регистрации", {}).get() return JsonResponse(response) provider = ContestRegisterManager(user=user, contest=contest, post_data=request.POST) success = provider.register_user_on_contest() if not success: response = Response(Response.STATUS_FAIL, provider.last_error, {}).get() return JsonResponse(response) data = {'redirect': reverse('profile')} response = Response(Response.STATUS_OK, "", data).get() return JsonResponse(response)
def add_short_hobby(req): try: uid = req.POST.get('uid') tag = req.POST.get('tag') ShortTimeHobby.addHobby(uid, tag) return HttpResponse(Response.response(None)) except Exception, e: return HttpResponse(Response.responseError(NOT_AVAILABLE_REQUEST_PARAMETERS, STRING_NOT_AVAILABLE_REQUEST_PARAMETERS))
def get_news(req): try: uid = req.GET.get('uid', None) page = int(req.GET.get('page')) cat = int(req.GET.get('cat')) news = NewsOperator.get_news(uid, cat, page) return HttpResponse(Response.response({'list': news})) except ValueError, e: return HttpResponse(Response.responseError(NOT_AVAILABLE_REQUEST_PARAMETERS, STRING_NOT_AVAILABLE_REQUEST_PARAMETERS))
def login(req): uid = req.POST.get('uid') token = req.POST.get('token') name = req.POST.get('name') avatar = req.POST.get('avatar') if uid is not None and token is not None and name is not None and avatar is not None: u = UserInfo.login(uid, name, token, avatar) UserInfo.get_tags(u) return HttpResponse(Response.response(None)) else: return HttpResponse(Response.responseError(NOT_AVAILABLE_REQUEST_PARAMETERS, STRING_NOT_AVAILABLE_REQUEST_PARAMETERS))
def pull_collect_list(req): try: uid = req.POST.get('uid') token = req.POST.get('token') page = int(req.POST.get('page')) user = UserInfo.find_user_by_id(uid, token) if user is None: return HttpResponse(Response.responseError(UID_NOT_MATCH_TOKEN, STRING_UID_NOT_MATCH_TOKEN)) result = NewsOperator.get_collected_news(user, page) return HttpResponse(Response.response({'list': result})) except ValueError: return HttpResponse(Response.responseError(NOT_AVAILABLE_REQUEST_PARAMETERS, STRING_NOT_AVAILABLE_REQUEST_PARAMETERS))
def delete_comment(req): try: uid = req.POST.get('uid') cid = req.POST.get('cid') token = req.POST.get('token') if not UserInfo.verify_user(uid, token): return HttpResponse(Response.responseError(UID_NOT_MATCH_TOKEN, STRING_UID_NOT_MATCH_TOKEN)) try: CommentHelper.delete_comment(uid, cid) return HttpResponse(Response.response(None)) except ValueError, e: return HttpResponse(Response.responseError(UID_NOT_MATCH, STRING_UID_NOT_MATCH)) except ObjectDoesNotExist: return HttpResponse(Response.responseError(NO_SUCH_COMMENT, STRING_NO_SUCH_COMMENT))
def pull_comment_list(req): try: news_id = req.POST.get('news_id') uid = req.POST.get('uid') token = req.POST.get('token') if not UserInfo.verify_user(uid, token): return HttpResponse(Response.responseError(UID_NOT_MATCH_TOKEN, STRING_UID_NOT_MATCH_TOKEN)) if NewsOperator.find_news_by_id(news_id) is None: return HttpResponse(Response.responseError(NO_SUCH_NEWS, STRING_NO_SUCH_NEWS)) comment_list = CommentHelper.get_comment_list_by_news_id(uid, token, news_id) return HttpResponse(Response.response({'list': comment_list})) except ValueError, e: return HttpResponse(Response.responseError(NOT_AVAILABLE_REQUEST_PARAMETERS, STRING_NOT_AVAILABLE_REQUEST_PARAMETERS))
def post(self, request): user = request.user provider = ContestCreate(request.POST, user) created_problem = provider.create_contest() if not isinstance(created_problem, Contests): response_status = Response.STATUS_FAIL response_error = provider.last_error response = Response(response_status, response_error, {}).get() return JsonResponse(response) data = {'redirect': reverse('contests_index')} response = Response(Response.STATUS_OK, "", data).get() return JsonResponse(response)
def comment(req): try: uid = req.POST.get('uid') news_id = req.POST.get('news_id') content = req.POST.get('content') token = req.POST.get('token') if not UserInfo.verify_user(uid, token): return HttpResponse(Response.responseError(UID_NOT_MATCH_TOKEN, STRING_UID_NOT_MATCH_TOKEN)) if NewsOperator.find_news_by_id(news_id) is None: return HttpResponse(Response.responseError(NO_SUCH_NEWS, STRING_NO_SUCH_NEWS)) CommentHelper.add_comment(uid, news_id, content) return HttpResponse(Response.response(None)) except Exception, e: return HttpResponse(Response.responseError(NOT_AVAILABLE_REQUEST_PARAMETERS, STRING_NOT_AVAILABLE_REQUEST_PARAMETERS))
def collect(req): try: uid = req.POST.get('uid') token = req.POST.get('token') news_id = req.POST.get('news_id') user = UserInfo.find_user_by_id(uid, token) if user is None: return HttpResponse(Response.responseError(UID_NOT_MATCH_TOKEN, STRING_UID_NOT_MATCH_TOKEN)) news = NewsOperator.find_news_by_id(news_id) if news is None: return HttpResponse(Response.responseError(NO_SUCH_NEWS, STRING_NO_SUCH_NEWS)) NewsOperator.collect_news(user, news) return HttpResponse(Response.response(None)) except ValueError: return HttpResponse(Response.responseError(NOT_AVAILABLE_REQUEST_PARAMETERS, STRING_NOT_AVAILABLE_REQUEST_PARAMETERS))
def post(self, request): response_status = Response.STATUS_OK response_error = '' response_data = {} provider = UserRegistration(request.POST) created_user = provider.register() if not isinstance(created_user, User): response_status = Response.STATUS_FAIL response_error = provider.last_error response = Response(response_status, response_error, {}).get() return JsonResponse(response) login(request, user=created_user) response = Response(response_status, response_error, response_data).get() return JsonResponse(response)
def post(self, request, task_id): if 'get_problem' in request.POST: problem = Problems.objects.get(pk=task_id) problem_dict = ProblemSerializer(problem).get_dict() response = Response(Response.STATUS_OK, "", problem_dict).get() return JsonResponse(response) provider = ProblemUpdate(task_id, request.POST) updated_problem = provider.update_problem() if not isinstance(updated_problem, Problems): response_status = Response.STATUS_FAIL response_error = provider.last_error response = Response(response_status, response_error, {}).get() return JsonResponse(response) data = { 'redirect': reverse('problems_detail', kwargs={'task_id': task_id}) } response = Response(Response.STATUS_OK, "", data).get() return JsonResponse(response)
def post(self, request, contest_id): if 'get_contest' in request.POST: contest = Contests.objects.get(pk=contest_id) contest_dict = ContestSerializer(contest).get_dict() response = Response(Response.STATUS_OK, "", contest_dict).get() return JsonResponse(response) provider = ContestUpdate(contest_id, request.POST) updated_contest = provider.update_contest() if not isinstance(updated_contest, Contests): response_status = Response.STATUS_FAIL response_error = provider.last_error response = Response(response_status, response_error, {}).get() return JsonResponse(response) data = { 'redirect': reverse('contests_detail', kwargs={'contest_id': contest_id}) } response = Response(Response.STATUS_OK, "", data).get() return JsonResponse(response)
def post(self, request): query = request.POST.get('q', '') problems = Problems.objects.filter( Q(title__icontains=query) | Q(pk=query)).only("pk", "title").all() response_data = list() for problem in problems: response_data.append({ 'id': problem.pk, 'title': problem.title, 'text': "ID:{} {}".format(problem.pk, problem.title) }) response = Response(Response.STATUS_OK, "", response_data).get() return JsonResponse(response)
def post(self, request): response_status = Response.STATUS_OK response_error = '' response_data = '' username = request.POST.get('login', '') password = request.POST.get('password', '') user = authenticate(username=username, password=password) if user is not None: if not user.is_active: response_status = Response.STATUS_FAIL response_error = "Аккаунт '{}' отключен. Обратитесь к администратору".format(username) else: login(request, user) else: response_status = Response.STATUS_FAIL response_error = "Неверные логин или пароль" response = Response(response_status, response_error, response_data).get() return JsonResponse(response)
def post(self, request, contest_id): contest = get_object_or_404(Contests, pk=contest_id) response_status = Response.STATUS_OK response_error = '' response_data = {} if 'deploy' in request.POST: success = ContestDeploy(contest).deploy() if 'remove' in request.POST: success = ContestDeploy(contest).remove() if 'reload' in request.POST: provider = ContestDeploy(contest) provider.remove() provider.deploy() if 'status' in request.POST: response_data = ContestDeploy(contest).get_contest_deploy_status() response = Response(response_status, response_error, response_data).get() return JsonResponse(response)
def delete_comment(req): try: uid = req.POST.get('uid') cid = req.POST.get('cid') token = req.POST.get('token') if not UserInfo.verify_user(uid, token): return HttpResponse(Response.responseError(UID_NOT_MATCH_TOKEN, STRING_UID_NOT_MATCH_TOKEN)) try: CommentHelper.delete_comment(uid, cid) return HttpResponse(Response.response(None)) except ValueError, e: return HttpResponse(Response.responseError(UID_NOT_MATCH, STRING_UID_NOT_MATCH)) except ObjectDoesNotExist: return HttpResponse(Response.responseError(NO_SUCH_COMMENT, STRING_NO_SUCH_COMMENT)) except Exception, e: return HttpResponse(Response.responseError(NOT_AVAILABLE_REQUEST_PARAMETERS, STRING_NOT_AVAILABLE_REQUEST_PARAMETERS)) #拉取新闻的评论列表 @csrf_exempt def pull_comment_list(req): try: news_id = req.POST.get('news_id') uid = req.POST.get('uid') token = req.POST.get('token') if not UserInfo.verify_user(uid, token): return HttpResponse(Response.responseError(UID_NOT_MATCH_TOKEN, STRING_UID_NOT_MATCH_TOKEN)) if NewsOperator.find_news_by_id(news_id) is None: return HttpResponse(Response.responseError(NO_SUCH_NEWS, STRING_NO_SUCH_NEWS)) comment_list = CommentHelper.get_comment_list_by_news_id(uid, token, news_id) return HttpResponse(Response.response({'list': comment_list}))
async def requesting(spider, request, max_times=3): print("开始请求:", request.url) method = request.method session = spider.session is_async = spider.is_async Response = spider.Response allow_code = spider.allow_code if request.cookies: if is_async: session.cookie_jar.update_cookies(request.cookies) else: cook = RequestsCookieJar() for k, v in request.cookies.items(): cook.set(k, v) session.cookies.update(cook) if spider.download_delay: time.sleep(spider.download_delay) if spider.timeout: timeout = spider.timeout else: timeout = request.timeout try: if method.upper() == "GET": if is_async: async with session.get( url=request.url, params=request.params, headers=request.headers, proxy=request.proxies, timeout=timeout, allow_redirects=request.allow_redirects) as res: content = await res.read() status_code = res.status charset = res.charset else: res = session.get(request.url, params=request.params, headers=request.headers, proxies=request.proxies, timeout=timeout, allow_redirects=request.allow_redirects) content = res.content status_code = res.status_code charset = res.encoding elif method.upper() == 'POST': if is_async: async with session.post( request.url, data=request.data, headers=request.headers, proxy=request.proxies, timeout=timeout, allow_redirects=request.allow_redirects, json=request.json) as res: content = await res.read() status_code = res.status charset = res.charset else: res = session.post( request.url, data=request.data, headers=request.headers, proxies=request.proxies, timeout=timeout, json=request.json, allow_redirects=request.allow_redirects, ) content = res.content status_code = res.status_code charset = res.encoding else: raise ValueError("method只支持post, get!") except Exception as e: logger.error("请求失败,未返回Response") request.err = e spider.is_invalid = True return request else: if (status_code == 200 and content is not None) or status_code in allow_code: spider.is_invalid = False cookies = res.cookies headers = res.headers return Response(url=request.url, content=content, status_code=status_code, charset=charset, cookies=cookies, headers=headers, callback=request.callback, proxies=request.proxies, method=method, meta=request.meta) else: spider.is_invalid = True logger.error("第%d次请求!状态码为%s" % (abs(max_times - 3), status_code)) return request
def system_status(request): processes_status = EjudgeSystemControl().get_system_processes_status() response = Response(Response.STATUS_OK, "", processes_status).get() return JsonResponse(response)
def system_reload(request): EjudgeSystemControl().reload_system() response = Response(Response.STATUS_OK, "", {}).get() return JsonResponse(response)
def system_start(request): EjudgeSystemControl().start_system() response = Response(Response.STATUS_OK, "", {}).get() return JsonResponse(response)