Пример #1
0
    def post(self, request, *args, **kwargs):
        try:
            User.objects.get(username=request.POST["username"])
            username = request.POST["username"] + u"_" + time().__str__()
        except User.DoesNotExist:
            username = request.POST["username"]

        user = User.objects.create_user(username, request.POST["email"], username)
        p = Profile(owner=user, country=Country.objects.get(pk=request.POST["country"]), city=City.objects.get(pk=1))
        p.save()

        return HttpResponseRedirect(reverse("thank_you"))
Пример #2
0
def register_ajax(request):
    if request.method == "POST":
        form = RegisterForm(request.POST or None)
        if form.is_valid():
            redirect_to = request.POST["next"]
            message = u"ok"

            try:
                int(form.cleaned_data["country"])
                int(form.cleaned_data["city"])
            except ValueError:
                message = u"Select your location!"

            try:
                int(form.cleaned_data["activity"])
            except ValueError:
                message = u"Select your field of activity!"

            if message == u"ok":
                u = User.objects.filter(username=form.cleaned_data["username"])
                if u:
                    message = u"Username already in use!"
                u1 = User.objects.filter(email=form.cleaned_data["email"])
                if u1:
                    message = u"Email address already in use!"
                if not u and not u1 and message == "ok":
                    user = User.objects.create_user(
                        form.cleaned_data["username"], form.cleaned_data["email"], form.cleaned_data["password"]
                    )
                    user.first_name = form.cleaned_data["first_name"]
                    user.last_name = form.cleaned_data["last_name"]
                    user.save()

                    status = int(form.cleaned_data["status"])

                    if status == 1:
                        p = Profile(
                            owner=user,
                            title=form.cleaned_data["title"],
                            city=City.objects.get(pk=form.cleaned_data["city"]),
                            country=Country.objects.get(pk=form.cleaned_data["country"]),
                            other_field_of_activity=form.cleaned_data["other"],
                        )
                        p.save()
                        p.field_of_activity.add(Activity.objects.get(id=form.cleaned_data["activity"]))
                    else:
                        o = Organisation(
                            user=user,
                            owner=user,
                            title=form.cleaned_data["title"],
                            city=City.objects.get(pk=form.cleaned_data["city"]),
                            country=Country.objects.get(pk=form.cleaned_data["country"]),
                            other_field_of_activity=form.cleaned_data["other"],
                            email=form.cleaned_data["email"],
                        )
                        o.save()
                        o.field_of_activity.add(Activity.objects.get(id=form.cleaned_data["activity"]))

                    user = authenticate(username=form.cleaned_data["username"], password=form.cleaned_data["password"])
                    login(request, user)

                    send_templated_mail(
                        template_name="welcome",
                        from_email=settings.DEFAULT_FROM_EMAIL,
                        recipient_list=[user.email],
                        context={
                            "username": user.username,
                            "full_name": user.get_full_name(),
                            "signup_date": user.date_joined,
                        },
                        # Optional:
                        # cc=['*****@*****.**'],
                        # bcc=['*****@*****.**'],
                        # headers={'My-Custom-Header':'Custom Value'},
                        # template_prefix="my_emails/",
                        # template_suffix="email",
                    )
            return HttpResponse(
                json.dumps(
                    {
                        "message": message,
                        "redirect": redirect_to.replace("logout", "home"),
                        "element_id": "#register-error-message",
                    }
                ),
                mimetype="application/json",
            )
        else:
            return HttpResponse(json.dumps({"message": "form invalid!"}), mimetype="application/json")
    else:
        return HttpResponseForbidden()