def set_rating(request, obj_type, obj_id, vote_type): error = "" if obj_type in RATINGS_CONFIG: model_name = RATINGS_CONFIG[obj_type]['model'] model = str_to_class(model_name) try: obj = model.objects.get(pk=obj_id) if obj.rating == None: rating = Rating.objects.create() rating.save() obj.rating = rating obj.save() vote, is_new = RatingVote.objects.get_or_create(user=request.user, rating=obj.rating) if is_new: vote.vote_type = vote_type vote.save() if vote_type == VOTE_LIKE: obj.rating.likes += 1 else: obj.rating.dislikes += 1 obj.rating.save() tasks.task_compute_object_rating(obj_type, obj) else: error = _("You already voted") except model.DoesNotExist: error = _("You can't vote for this object") else: error = _("You can't vote for this object") if request.is_ajax(): if error: return json_response({'status': "error", 'text': error}) else: return json_response({'status': "ok", 'text': _("Your vote successfully counted")}) return redirect(request.META.get('HTTP_REFERER', '/'))
def toggle_guides(request, status): if request.user.is_authenticated(): if status == 'disable': UserGuide.objects.filter(user=request.user, guide__in=request.GET.getlist('guide_list[]'))\ .update(views_count=(settings.GUIDE_MIN_VIEWS_COUNT + 1)) else: UserGuide.objects.filter(user=request.user, guide__in=request.GET.getlist('guide_list[]'))\ .update(views_count=1) request.session['guide_disabled'] = (status == 'disable') return json_response({'status': 'ok'})
def wrapped_func(request, *args, **kwargs): func_response = func(request, *args, **kwargs) if not request.is_ajax(): return func_response if isinstance(func_response, HttpResponseRedirect): return json_response({'status': 'ok', 'action': 'redirect', 'url': func_response._headers['location'][1]}) if isinstance(func_response, TemplateResponse): template_name_splitted = os.path.splitext(func_response.template_name) template_name = template_name_splitted[0] + '_ajax' + template_name_splitted[1] return JSONTemplateResponse(request, template_name, func_response.context_data, data={'status': 'ok'}) return func_response
def upload(request, *args, **kwargs): if request.method != 'POST': raise Http404 if 'sessionid' not in request.POST: raise Http404 request.user = user_from_session_key(request.POST['sessionid']) if not request.user.is_authenticated(): raise Http404 sender = request.POST.get('sender', 'uploadify') filename = request.POST.get('fileDataName', 'Filename') if request.FILES: received_list = upload_received.send(sender=sender, request=request, data=request.FILES[filename]) for received, response in received_list: if response is not None: return json_response(response) raise Http404