M
M
maestro072017-04-02 21:43:33
Django
maestro07, 2017-04-02 21:43:33

How to do registration through django activation?

views.py

def registration(request):
    template = "main/registration.html"
    params = dict()
    if request.method == "POST":
        email = request.POST.get("email", "").lower()
        password = request.POST.get("password", "")
        password2 = request.POST.get("password2", "")

        try:
            validate_email(email)
        except Exception as error:
            return render(request, template, params)        

        try:
            len(password) >= 5
        except Exception as error:
            messages.add_message(request, messages.ERROR, PASSWORD_ERROR)
            return render(request, template, params)        

        if password != password2:
           messages.add_message(request, messages.ERROR, DIFFERENT_PASSWORD)

        if User.objects.filter(username=email, is_active=True).exists():
            messages.add_message(request, messages.ERROR, EXIST_USER)
            return render(request, template, params)

        new_user = User.objects.create_user(username=email, password=password)
        activation_key = Activation.objects.generate(user=new_user)

        messages.add_message(request, messages.ERROR, SUCCESS)
        send_mail('activation JustSolve.kz', "hello world", settings.EMAIL_HOST_USER, ['[email protected]'])
        # TODO: add celery
        # TODO: send verification email
    return render(request, template, params)

settings.py
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = '[email protected]'
EMAIL_HOST_PASSWORD = 'mypassword'
EMAIL_PORT = 587
EMAIL_USE_TLS = True

models.py
class ActivationManager(models.Manager):

    def generate(self, user):

        activation = Activation()
        activation.user = user
        activation.key = uuid.uuid4()
        activation.save()

        return activation.key


class Activation(models.Model):

    user = models.ForeignKey('MainUser')
    key = models.CharField(max_length=100, blank=False)

    create_time = models.DateTimeField(auto_now=True)

    is_active = models.BooleanField(default=True)

    objects = ActivationManager()

    def __unicode__(self):
        return u"user={0} key={1}".format(self.user, self.key)

    class Meta:
        verbose_name=u""

how to make an activation by sending an activation link to email. I want to make the is active field in the admin panel true. I think that the idea is this: After registration, I generate a code and send a link to email ../activate/code. After opening this link, I make the field is active true. how to implement it?
send_mail('activation JustSolve.kz', "hello world", settings.EMAIL_HOST_USER, ['[email protected]'])

and how to make to send a letter not only to gmail

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Pavel Aksenov, 2017-04-03
@hellmin

I used the password recovery mechanism to activate my account. It turned out, approximately, so.
views.py

...
        user.is_active = False
        user.save()
        context = {'user': user}

        current_site = get_current_site(request)
        site_name = current_site.name
        domain = current_site.domain

        use_https = request.is_secure()
        token_generator = user_is_active_token_generator
        extra_context = {
            'domain': domain,
            'site_name': site_name,
            'uid': urlsafe_base64_encode(force_bytes(user.pk)),
            'user': user,
            'token': token_generator.make_token(user),
            'protocol': 'https' if use_https else 'http',
        }
        context.update(extra_context)
        mail_message_template = 'путь до registration_email_user_not_is_active.html'
        mail_message = render_to_string(mail_message_template, context)

        send_mail('activation JustSolve.kz', mail_message, settings.EMAIL_HOST_USER, ['[email protected]'])

tokens.py (it's pretty much the same as django.contrib.auth.tokens)
from django.conf import settings
from django.contrib.auth.tokens import PasswordResetTokenGenerator
from django.utils import six
from django.utils.crypto import constant_time_compare, salted_hmac
from django.utils.http import base36_to_int, int_to_base36


class UserIsActiveTokenGenerator(PasswordResetTokenGenerator):

    def check_token(self, user, token):
        """
        Check that a password reset token is correct for a given user.
        """
        # Parse the token
        try:
            ts_b36, hash = token.split("-")
        except ValueError:
            return False

        try:
            ts = base36_to_int(ts_b36)
        except ValueError:
            return False

        # Check that the timestamp/uid has not been tampered with
        if not constant_time_compare(self._make_token_with_timestamp(user, ts), token):
            return False

        return True

    def _make_token_with_timestamp(self, user, timestamp):
        # timestamp is number of days since 2001-1-1.  Converted to
        # base 36, this gives us a 3 digit string until about 2121
        ts_b36 = int_to_base36(timestamp)

        # By hashing on the internal state of the user and using state
        # that is sure to change (the password salt will change as soon as
        # the password is set, at least for current Django auth, and
        # last_login will also change), we produce a hash that will be
        # invalid as soon as it is used.
        # We limit the hash to 20 chars to keep URL short
        key_salt = "django.contrib.auth.tokens.PasswordResetTokenGenerator"

        # Ensure results are consistent across DB backends
        joined_timestamp = '' if user.date_joined is None else user.date_joined.replace(microsecond=0, tzinfo=None)

        value = (six.text_type(user.pk) +
                 six.text_type(joined_timestamp) + six.text_type(timestamp))
        hash = salted_hmac(key_salt, value).hexdigest()[::2]
        return "%s-%s" % (ts_b36, hash)

user_is_active_token_generator = UserIsActiveTokenGenerator()

registration_email_user_not_is_active.html
Dear, {{ user.username }}!<br>
You join to {{ domain }}!<br>

url to activate <a href="{{ protocol }}://{{ domain }}/activate/{{ uid }}/{{ token }}/">{{ protocol }}://{{ domain }}/activate/{{ uid }}/{{ token }}/</a>

welcome!

urls.py
...
    url(r'^activate/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$',
        views.activate, name='activate')
...

views.py
def activate(request, uidb64=None, token=None,
             token_generator=user_is_active_token_generator,
             template_name='common/activate.html'):

    UserModel = get_user_model()
    assert uidb64 is not None and token is not None  # checked by URLconf

    try:
        # urlsafe_base64_decode() decodes to bytestring on Python 3
        uid = force_text(urlsafe_base64_decode(uidb64))
        user = UserModel._default_manager.get(pk=uid)
    except (TypeError, ValueError, OverflowError, UserModel.DoesNotExist):
        user = None

    if user is not None and user.is_active:
        validlink = True
        title = _('Link no longer relevant')
    elif user is not None and token_generator.check_token(user, token):
        user.is_active = True
        user.save()

        validlink = True
        title = _('Account activated')
    else:
        validlink = False
        title = _('Link not correct')
    context = {
        'title': title,
        'validlink': validlink,
    }

    return TemplateResponse(request, template_name, context)

With this approach, you will not need an additional Activation model or any fields in the database.
# Проверки должны находиться в django forms, а не во вьюхе.
validate_email(email)
len(password) >= 5
password != password2
User.objects.filter(username=email, is_active=True).exists()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question