def update_user_rates(request): form = UserRatesForm(request.POST) if form.is_valid(): try: User = get_user_model() user = User.objects.get(username=form.cleaned_data['username']) except User.DoesNotExist: error_text = _("User %s not found", form.cleaned_data['username']) return JsonResponseNotFound({'msg': error_text}) user.currency = form.cleaned_data['currency'] user.rate = form.cleaned_data['rate'] user.review_rate = form.cleaned_data['review_rate'] user.hourly_rate = form.cleaned_data['hourly_rate'] scorelog_filter = {'user': user} paid_task_filter = scorelog_filter.copy() if form.cleaned_data['effective_from'] is not None: effective_from = form.cleaned_data['effective_from'] scorelog_filter.update({ 'creation_time__gte': effective_from }) paid_task_filter.update({ 'datetime__gte': effective_from }) scorelog_query = ScoreLog.objects.filter(**scorelog_filter) scorelog_count = scorelog_query.count() paid_task_query = PaidTask.objects.filter(**paid_task_filter) paid_task_count = paid_task_query.count() scorelog_query.update(rate=user.rate, review_rate=user.review_rate) def get_task_rate_for(user, task_type): return { PaidTaskTypes.TRANSLATION: user.rate, PaidTaskTypes.REVIEW: user.review_rate, PaidTaskTypes.HOURLY_WORK: user.hourly_rate, PaidTaskTypes.CORRECTION: 1, }.get(task_type, 0) for task in paid_task_query: task.rate = get_task_rate_for(user, task.task_type) task.save() user.save() return JsonResponse({ 'scorelog_count': scorelog_count, 'paid_task_count': paid_task_count, }) return JsonResponseBadRequest({'errors': form.errors})
def handle_exception(self, exc): """Handles response exceptions.""" if isinstance(exc, Http404): return JsonResponseNotFound({"msg": "Not found"}) if isinstance(exc, PermissionDenied): return JsonResponseForbidden({"msg": "Permission denied."}) if isinstance(exc, JSONDecodeError): return JsonResponseBadRequest({"msg": "Invalid JSON data"}) raise
def remove_paid_task(request, task_id=None): if request.method == "DELETE": try: obj = PaidTask.objects.get(id=task_id) string = "%s\t%s\t%s" % (request.user.username, PAID_TASK_DELETED, obj) obj.delete() log(string) return JsonResponse({"removed": 1}) except PaidTask.DoesNotExist: return JsonResponseNotFound({}) return JsonResponseBadRequest({"error": _("Invalid request method")})
def remove_paid_task(request, task_id=None): if request.method == 'DELETE': try: obj = PaidTask.objects.get(id=task_id) str = '%s\t%s\t%s' % (request.user.username, PAID_TASK_DELETED, obj) obj.delete() log(str) return JsonResponse({'removed': 1}) except PaidTask.DoesNotExist: return JsonResponseNotFound({}) return JsonResponseBadRequest({'error': _('Invalid request method')})
def process_exception(self, request, exception): msg = force_str(exception) if isinstance(exception, Http404): if request.is_ajax(): return JsonResponseNotFound({"msg": msg}) elif isinstance(exception, Http400): if request.is_ajax(): return JsonResponseBadRequest({"msg": msg}) elif isinstance(exception, PermissionDenied): if request.is_ajax(): return JsonResponseForbidden({"msg": msg}) ctx = { "permission_error": msg, } if not request.user.is_authenticated: msg_args = { "login_link": reverse("account_login"), } login_msg = _( 'You need to <a class="js-login" ' 'href="%(login_link)s">login</a> to access this page.' % msg_args) ctx["login_message"] = login_msg return HttpResponseForbidden( render_to_string("errors/403.html", context=ctx, request=request)) elif exception.__class__.__name__ in ( "OperationalError", "ProgrammingError", "DatabaseError", ): # HACKISH: Since exceptions thrown by different databases do not # share the same class heirarchy (DBAPI2 sucks) we have to check # the class name instead. Since python uses duck typing I will call # this poking-the-duck-until-it-quacks-like-a-duck-test return handle_exception(request, exception, "errors/db.html") else: return handle_exception(request, exception, "errors/500.html")
def process_exception(self, request, exception): msg = force_unicode(exception) if isinstance(exception, Http404): if request.is_ajax(): return JsonResponseNotFound({'msg': msg}) elif isinstance(exception, Http400): if request.is_ajax(): return JsonResponseBadRequest({'msg': msg}) elif isinstance(exception, PermissionDenied): if request.is_ajax(): return JsonResponseForbidden({'msg': msg}) ctx = { 'permission_error': msg, } if not request.user.is_authenticated(): msg_args = { 'login_link': reverse('account_login'), } login_msg = _( 'You need to <a class="js-login" href="%(login_link)s">login</a> ' 'to access this page.', msg_args) ctx["login_message"] = login_msg return HttpResponseForbidden( render_to_string('errors/403.html', ctx, RequestContext(request))) elif (exception.__class__.__name__ in ('OperationalError', 'ProgrammingError', 'DatabaseError')): # HACKISH: Since exceptions thrown by different databases do # not share the same class heirarchy (DBAPI2 sucks) we have to # check the class name instead. Since python uses duck typing # I will call this # poking-the-duck-until-it-quacks-like-a-duck-test return handle_exception(request, exception, 'errors/db.html') else: return handle_exception(request, exception, 'errors/500.html')