Module apps.registration.forms

Expand source code
from django.contrib.auth.forms import UserCreationForm
from django import forms
from django.contrib.auth.models import User, Permission
from django.contrib.contenttypes.models import ContentType
from django.apps import apps


class RegistrationForm(UserCreationForm):
    """
    This class will create a form for registration
    """
    email = forms.EmailField()

    class Meta:
        """
        This class will define the meta data for RegistrationForm
        """
        model = User
        fields = ["username", "email", "password1", "password2"]

    def save(self, commit=True):
        """
        While saving the user we need to make sure that the registered user should have viewing access
        of our models and it should be a staff user so that it can access admin page.
        Arguments:
            commit {bool} -- True
        Returns:
            object -- User object
        """
        user = super(RegistrationForm, self).save(commit=False)
        user.is_staff = True
        if commit:
            user.save()
            app_models = apps.get_app_config("email_scheduler").get_models()
            for model in app_models:
                permission_codename = "view_" + model._meta.model_name
                content_type = ContentType.objects.get_for_model(model)
                permission = Permission.objects.get(
                    codename=permission_codename, content_type=content_type)
                user.user_permissions.add(permission)
        return user

Classes

class RegistrationForm (*args, **kwargs)

This class will create a form for registration

Expand source code
class RegistrationForm(UserCreationForm):
    """
    This class will create a form for registration
    """
    email = forms.EmailField()

    class Meta:
        """
        This class will define the meta data for RegistrationForm
        """
        model = User
        fields = ["username", "email", "password1", "password2"]

    def save(self, commit=True):
        """
        While saving the user we need to make sure that the registered user should have viewing access
        of our models and it should be a staff user so that it can access admin page.
        Arguments:
            commit {bool} -- True
        Returns:
            object -- User object
        """
        user = super(RegistrationForm, self).save(commit=False)
        user.is_staff = True
        if commit:
            user.save()
            app_models = apps.get_app_config("email_scheduler").get_models()
            for model in app_models:
                permission_codename = "view_" + model._meta.model_name
                content_type = ContentType.objects.get_for_model(model)
                permission = Permission.objects.get(
                    codename=permission_codename, content_type=content_type)
                user.user_permissions.add(permission)
        return user

Ancestors

  • django.contrib.auth.forms.UserCreationForm
  • django.forms.models.ModelForm
  • django.forms.models.BaseModelForm
  • django.forms.forms.BaseForm

Class variables

var Meta

This class will define the meta data for RegistrationForm

var base_fields
var declared_fields

Instance variables

var media
Expand source code
def _media(self):
    # Get the media property of the superclass, if it exists
    sup_cls = super(cls, self)
    try:
        base = sup_cls.media
    except AttributeError:
        base = Media()

    # Get the media definition for this class
    definition = getattr(cls, 'Media', None)
    if definition:
        extend = getattr(definition, 'extend', True)
        if extend:
            if extend is True:
                m = base
            else:
                m = Media()
                for medium in extend:
                    m = m + base[medium]
            return m + Media(definition)
        return Media(definition)
    return base

Methods

def save(self, commit=True)

While saving the user we need to make sure that the registered user should have viewing access of our models and it should be a staff user so that it can access admin page.

Arguments

commit {bool} – True

Returns

object – User object

Expand source code
def save(self, commit=True):
    """
    While saving the user we need to make sure that the registered user should have viewing access
    of our models and it should be a staff user so that it can access admin page.
    Arguments:
        commit {bool} -- True
    Returns:
        object -- User object
    """
    user = super(RegistrationForm, self).save(commit=False)
    user.is_staff = True
    if commit:
        user.save()
        app_models = apps.get_app_config("email_scheduler").get_models()
        for model in app_models:
            permission_codename = "view_" + model._meta.model_name
            content_type = ContentType.objects.get_for_model(model)
            permission = Permission.objects.get(
                codename=permission_codename, content_type=content_type)
            user.user_permissions.add(permission)
    return user