0

I want to implement the feature where users are not given to signup account directly. A request invitation button will be there. When user clicks for request invitation with an email address, a link will be send with token and when user clicks on that link, the user will be redirected to signup page.

class Invite(models.Model):
    # INVITE_CHOICES = (
    # ('G', 'General'),
    # ('I', 'Invitational'),
    # )
    user = models.OneToOneField(User)
    email = models.EmailField()
    cookie = models.UUIDField(default=uuid.uuid4)
    token = models.UUIDField(default=uuid.uuid4, unique=True, editable=False)
    timestamp = models.DateTimeField(auto_now_add=True)

class InviteForm(forms.ModelForm):
    email = forms.EmailField()
    class Meta:
        model = Invite
        fields = ['email']

    def clean_email(self, *args, **kwargs):
        email = self.cleaned_data.get('email')
        print('###############################')
        print ('email', email)
        email_qs = Invite.objects.filter(email__iexact=email)
        if not email:
            raise forms.validationError('Please! fill in your email address')
        if email_qs.exists():
            print ('sorry the email exists')
            raise forms.validationError('Sorry! This email already exists')
        return email

def invite_user(request):
    form = InviteForm(request.POST or None)
    if request.method == 'POST':
        if form.is_valid():
        # data for sending mail
        form_email = form.cleaned_data.get('email')
        # from_email = settings.EMAIL_HOST_USER
        user = User.objects.create_user(form.cleaned_data.get('email'))
        user.is_active = False
        user.save()
        invite, created = Invite.objects.get_or_create(user=user, email=email)
        if created:
            invite.token = get_token()
            invite.save()
        return HttpResponseRedirect('/')
    context = {"form": form}
    return render(request, "home.html", context)
pythonBeginner
  • 781
  • 2
  • 12
  • 27
  • Is there any `email` field inside your `Invite` model? – nik_m Mar 15 '17 at 08:14
  • no there is no email field. – pythonBeginner Mar 15 '17 at 12:47
  • Just for your information, you have a typo there: `forms.validationError`. It should be `forms.ValidationError` (capital `V`). – nik_m Mar 15 '17 at 12:58
  • Thanks for the information. I forgot i was first using custom form so i used email field over there. Later i changed to modelForm to learn the concept of it too and i forgot to update the models.py with email field. I could store the token but how can i handle the cookie. – pythonBeginner Mar 15 '17 at 13:03
  • You are not asking something specific. Do you get an error or something? And if yes, where? – nik_m Mar 15 '17 at 13:58
  • In addition, there are some errors inside `invite_user` view. For example, if it's a `GET` method, then the context will include an undefined variable `form`.... – nik_m Mar 15 '17 at 14:10

0 Answers0