1

Hi Djangonauts, I am new to Django please forgive any silly mistake in logic or code.

Intro: I am building a web app in which members can write posts on a topic and offer courses on that topic. Example A member can write a blog about doing a wheelie on a bicycle and offer courses on that.

What I want: I want members who want to offer courses to be verified. Example: The member has to fill a form with their details like... name, address, and photo ID. Plus pay a charge of $9.99 to get verified. After admin (I in this case) checks if everything is good I will approve them. and then they will be "Verified Members" and be able to offer courses

What I have so far: Right now members can offer courses as there is no verified clause

class Event(models.Model):
    user = models.ForeignKey(User, related_name='seller')
    post = models.ForeignKey(Post, related_name='course')
    price = models.DecimalField(max_digits=6, decimal_places=2)
    stock = models.IntegerField(validators=[MinValueValidator(1), MaxValueValidator(35)])
    date = models.DateField()
    time_from = models.TimeField()
    time_to = models.TimeField()
    event_types = (
        ('1', 'Webinar'),
        ('2', 'Actual Meet'),

    )
    event_choice = models.CharField(max_length=1, choices=event_types)

    def get_absolute_url(self):
        return reverse('posts:single', kwargs={'username': self.user.username,
                                               'slug': self.post.slug})
    def __str__(self):
        return 'Meet for ' + self.post.title

How I plan to do it: I was planning to add a group in Django's admin AUTHENTICATION AND AUTHORIZATION

Home › Authentication and Authorization › Groups › Add group
Name: Verified

Permissions: Chosen permissions 
event| event| Can add event
event| event| Can change event
event| event| Can delete event 

Now what do I do from here?: Have I done things right so far, How do I take it from here. Do I create a model called verified and add forms.py to have members verified. How do permissions come in the picture.

My monkey patch (not a part of the question, for @Ojas Kale )

class Contact(models.Model):
    user_from = models.ForeignKey(User, related_name='supporter')
    user_to = models.ForeignKey(User, related_name='leader')

    def __str__(self):
        return '{} follows {}'.format(self.user_from, self.user_to)


User.add_to_class('following',
                  models.ManyToManyField('self', through=Contact, related_name='followers', symmetrical=False))
Samir Tendulkar
  • 1,151
  • 3
  • 19
  • 47

1 Answers1

2

One way to go about it is adding a is_verified column in the user. There are various ways for doing this. but extending from abstractUser is probably the most straightforward and suitable in your case, since the class django.contrib.auth.models.AbstractUser provides the full implementation of the default User as an abstract model.

in your app_name.models.py create user class like this.

from django.db import models
from django.contrib.auth.models import AbstractUser

class User(AbstractUser):
    is_verified = models.BooleanField(default=False)

in your settingps.py

AUTH_USER_MODEL = 'app_name.User'

notice how app_name is used.

Now you can add as many attributes as you want as well.

By defaul is_verified is set to False, As soon as admin approves (verifies) the user change it to True.

Hope this helps.

Ojas Kale
  • 2,067
  • 2
  • 24
  • 39
  • Just 1 quick question. Will this change items that I have already attached to my User model. Example I have monkey patched my user model when I made follow/ follower model. Plus username, password, email I have all those. I won't have to make them again correct. I am still reading more about AbstractUser – Samir Tendulkar Jun 27 '18 at 20:35
  • AUTH_USER_MODEL = 'app_name.User' This line tells django to use this User always. so your monkey patching would not hold. but you can always add those functionalities here as well/ – Ojas Kale Jun 27 '18 at 20:38
  • I have added my monkey patch in the question above. I would hate to lose that. I have gone too far with that model. Is there a way we can address this with groups and permissions Or Any other way – Samir Tendulkar Jun 27 '18 at 22:13
  • Can I add is_verified as a patch – Samir Tendulkar Jun 27 '18 at 22:16