예제 #1
0
    def process_following(self, request, obj, success_url):
        user = request.user
        kwargs = {}
        if not user == obj:
            is_following = Follow.objects.is_following(user, obj)
            follow_obj = unfollow(user, obj) if is_following else \
                follow(user, obj)
            is_following = not is_following

            if follow_obj.target.__class__ is User:
                if is_following:
                    mail_followee(request, follow_obj.target, user)
                title = user_display(follow_obj.target)
            else:
                title = unicode(follow_obj.target)

            key = "follow" if is_following else "unfollow"
            kwargs.update({"object": title})
        else:
            key = "follow-warning"
        add_message(key, request, **kwargs)

        if request.is_ajax():
            messages = render_messages(request)
            data = {"is_following": is_following, "messages": messages}
            return HttpResponse(json.dumps(data), mimetype="application/json")
        else:
            return HttpResponseRedirect(success_url)
예제 #2
0
    def process_voting(self, request, obj, success_url):
        direction = request.POST["vote-up"] if "vote-up" in request.POST else \
            request.POST["vote-down"]
        kwargs = {}
        if not request.user.has_perm("profiles.can_vote"):
            data = {"ok": False}
            key = "vote-denied"
        elif obj.author == request.user:
            data = {"ok": False}
            key = "vote-warning"
        else:
            vote = dict(VOTE_DIRECTIONS)[direction]
            content_obj = obj.select_parent()
            Vote.objects.record_vote(content_obj, request.user, vote)
            if obj.__class__ is Content:
                obj = obj.select_subclass()
            if obj.__class__ is Question:
                obj.sort_related_answers()
            elif obj.__class__ is Answer:
                obj.question.sort_related_answers()

            kwargs.update({"object": obj})
            key = "vote-{0}".format(direction)
            score = Vote.objects.get_score(content_obj)
            vote = Vote.objects.get_for_user(content_obj, request.user)
            conf = BUTTONS_CONF[vote.vote]
            data = {"conf": conf, "score": score, "ok": True}
        add_message(key, request, **kwargs)
        if request.is_ajax():
            data.update({"messages": render_messages(request)})
            return HttpResponse(json.dumps(data), mimetype="application/json")
        else:
            return HttpResponseRedirect(success_url)
예제 #3
0
 def post(self, request, *args, **kwargs):
     POST = request.POST
     if "question" not in POST:
         return super(QuestionFormMixin, self).post(request, *args,
                                                    **kwargs)
     form_class = PartialQuestionForm
     form = self.get_form(form_class)
     if form.is_valid():
         add_message("object-created", request, model=form._meta.model)
         return self.form_valid(form)
     else:
         add_message("form-invalid", request, model=form._meta.model)
         return self.form_invalid(form)
예제 #4
0
    def toggle_skill(self, request, *args, **kwargs):
        profile = request.user.get_profile()
        if self.tag in profile.skills.all():
            profile.skills.remove(self.tag.name)
            add_message("skill-removed", request, tag=self.tag)
        else:
            profile.skills.add(self.tag)
            add_message("skill-added", request, tag=self.tag)

        if request.is_ajax():
            data = {"messages": render_messages(request)}
            return HttpResponse(json.dumps(data), mimetype="application/json")
        else:
            return HttpResponseRedirect(request.path)
예제 #5
0
    def post(self, request, *args, **kwargs):
        POST = request.POST
        if "answer" not in POST:
            return super(AnswerFormMixin, self).post(request, *args, **kwargs)

        self.status = int(POST.get("answer"))
        form_class = AnswerForm
        form = self.get_form(form_class)
        if form.is_valid():
            add_message("object-created", request, model=form._meta.model)
            return self.form_valid(form)
        else:
            add_message("form-invalid", request, model=form._meta.model)
            del self.status
            return self.form_invalid(form)
예제 #6
0
    def post(self, request, *args, **kwargs):
        POST = request.POST
        if self.model is Question and "add_question" in POST:
            form_class = self.get_form_class()
            form = self.get_form(form_class)
            cb_answer = POST.get("cb_answer", None)
            ctx = {"cb_answer": cb_answer}
            if form.is_valid():
                self.object = form.save(commit=False)
                kwargs = {"request": request, "status": self.object.status}
                if cb_answer:
                    kwargs.update({"empty_permitted": False})
                formset = QAFormSet(data=POST, instance=self.object, **kwargs)
                if formset.is_valid():
                    add_message("object-created", request, model=self.model)
                    self.object.save()
                    form.save_m2m()
                    formset.save()
                    return HttpResponseRedirect(self.get_success_url())
                else:
                    add_message("form-invalid", request, model=formset.model)
                ctx.update({"formset": formset})
            else:
                add_message("form-invalid", request, model=self.model)
            ctx.update({"form": form})
            return self.render_to_response(self.get_context_data(**ctx))

        return super(ContentCreateView, self).post(request, *args, **kwargs)
예제 #7
0
 def post(self, request, *args, **kwargs):
     self.object = self.get_object()
     self.experts = self.get_experts()
     self.items = [self.object]
     POST = request.POST
     if request.is_ajax() and "about" in POST:
         about = POST["about"]
         ## DJANGO 1.5: use defer and save only about field
         profile = request.user.get_profile()
         toggle = bool(profile.about) != bool(about)
         profile.about = about
         profile.save()
         add_message("about", request)
         messages = render_messages(request)
         data = {
             "about": about,
             "messages": messages,
             "result": "success",
             "toggle": toggle
         }
         return HttpResponse(json.dumps(data), mimetype="application/json")
     else:
         return super(ContentDetailView, self).post(request, *args,
                                                    **kwargs)
예제 #8
0
    def form_valid(self, form):
        if "ask" not in self.request.POST:
            return super(AskForHelpMixin, self).form_valid(form)

        receiver_profiles = []
        request, user = [self.request, self.request.user]
        question = Question.objects.get(id=form.question_id)
        username = user_display(user)

        for key, value in form.cleaned_data.items():
            if key in ["experts", "users"] and value:
                profile_ids = map(int, re.split("\D+", value))
                receiver_profiles.extend(Profile.objects.filter(
                    id__in=profile_ids).select_related("user"))
            elif key == "email" and value:
                m = EMAIL_PATTERN.match(request.POST["email"])
                if m:
                    receiver_profile = Profile(user=User(email=m.group()),
                                               name=m.group("username"))
                    receiver_profile.user.account = Account()
                    receiver_profile.user.get_absolute_url = void
                    receiver_profiles.append(receiver_profile)
                else:
                    kwargs = {"user": username, "email": request.POST["email"]}
                    add_message("email-error", request, **kwargs)

        for receiver_profile in receiver_profiles:
            receiver = receiver_profile.user

            receiver_name = user_display(receiver_profile)

            if receiver != user:
                mail_helper(request, receiver, user, question, receiver_name,
                            username)
                kwargs = {"user": username, "receiver": receiver_name}
                add_message("email-sent", request, **kwargs)
            else:
                add_message("ask-warning", request, user=username)

        return HttpResponseRedirect(request.path)
예제 #9
0
    def post(self, request, *args, **kwargs):
        form_class = self.get_form_class()
        form = self.get_form(form_class)
        POST = request.POST

        if self.model == Item and ("add_links" in POST or "del_links" in POST
                                   or "store_search" in POST):
            if "add_links" in POST and self.object:
                form.link_aff(self.object)
            if "del_links" in POST and self.object:
                form.unlink_aff(self.object)
            if "store_search" in POST:
                form.stores_search()
            return self.render_to_response(self.get_context_data(form=form))
        elif request.is_ajax() and "add_item" in POST:
            form = ItemForm(data=POST, request=request, prefix="item")
            if form.is_valid():
                item = form.save()
                args = [item._meta.module_name, item.id]
                add_message("object-created", request, model=form._meta.model)
                messages = render_messages(request)
                data = {
                    "id": item.id,
                    "name": item.name,
                    "messages": messages,
                    "next": reverse("item_edit", args=args)
                }
            else:
                data = "invalid form"
            return HttpResponse(json.dumps(data), mimetype="application/json")
        elif form.is_valid():
            key = "object-updated" if self.object else "object-created"
            add_message(key, request, model=self.model)
            return self.form_valid(form)
        else:
            add_message("form-invalid", request, model=self.model)
            return self.form_invalid(form)