diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index c5bb637..2385cfa 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -3,4 +3,5 @@ Add your name in the given format. - [Kunal Katiyar](https://github.com/kunal2812/) -- [Eleena Sarah Mathew](https://github.com/eleensmathew/) \ No newline at end of file +- [Eleena Sarah Mathew](https://github.com/eleensmathew/) +- [Manan Arora](https://github.com/Manan-Arora31) \ No newline at end of file diff --git a/main/authentication.py b/main/authentication.py new file mode 100644 index 0000000..73d5883 --- /dev/null +++ b/main/authentication.py @@ -0,0 +1,32 @@ +from django.contrib.auth.models import User +from main.models import User +class NameAuthBackend: + """ + Custom authentication backend. + + Allows users to log in using their first name. + """ + + def authenticate(self, request, email=None, name=None, password=None): + """ + Overrides the authenticate method + """ + try: + if email == None: + user = User.objects.get(name=name) + else: + user = User.objects.get(email=email) + if user.check_password(password): + return user + return None + except User.DoesNotExist: + return None + + def get_user(self, user_id): + """ + Overrides the get_user method + """ + try: + return User.objects.get(pk=user_id) + except User.DoesNotExist: + return None \ No newline at end of file diff --git a/main/models.py b/main/models.py index 15fd47a..da84a57 100644 --- a/main/models.py +++ b/main/models.py @@ -8,21 +8,21 @@ class UserManager(BaseUserManager): use_in_migrations = True - def _create_user(self, email, password, **extra_fields): + def _create_user(self, email, name, password, **extra_fields): """Create and save a User with the given email and password.""" if not email: raise ValueError('The given email must be set') email = self.normalize_email(email) - user = self.model(email=email, **extra_fields) + user = self.model(name=name, email=email, **extra_fields) user.set_password(password) user.save(using=self._db) return user - def create_user(self, email, password=None, **extra_fields): + def create_user(self, name, email, password=None, **extra_fields): """Create and save a regular User with the given email and password.""" extra_fields.setdefault('is_staff', False) extra_fields.setdefault('is_superuser', False) - return self._create_user(email, password, **extra_fields) + return self._create_user(email, name, password, **extra_fields) def create_superuser(self, email, password, **extra_fields): """Create and save a SuperUser with the given email and password.""" @@ -39,11 +39,11 @@ def create_superuser(self, email, password, **extra_fields): class User(AbstractUser): """User model.""" ROLE=(('Student','STUDENT'),('Mentor','MENTOR')) - username = None + name = models.CharField(max_length=20, unique=True) email = models.EmailField(_('email address'), unique=True) role=models.CharField(choices=ROLE,max_length=10) USERNAME_FIELD = 'email' - REQUIRED_FIELDS = [] + REQUIRED_FIELDS = ['name'] objects = UserManager() @@ -55,7 +55,7 @@ class Courses(models.Model): course_language=models.CharField(max_length=50) editing_status=models.BooleanField(default=True,blank=False); - def __str__(self): + def _str_(self): return self.course_name class Chapters(models.Model): @@ -66,7 +66,7 @@ class Chapters(models.Model): order = models.FloatField() - def __str__(self): + def _str_(self): return self.chapter_name class Titles(models.Model): title_name=models.CharField(max_length=100,blank=False) @@ -74,16 +74,16 @@ class Titles(models.Model): description=models.TextField() order = models.FloatField() - def __str__(self): + def _str_(self): return self.title_name class Questions(models.Model): chapter=models.ForeignKey(Chapters, on_delete=models.CASCADE) question=models.TextField() answer=models.TextField() - def __str__(self): + def _str_(self): return self.question - + class Tag(models.Model): name = models.CharField(max_length=50, unique=True) def __str__(self): @@ -136,5 +136,4 @@ class UserProfile(models.Model): bio=models.TextField(default='I am passionate about learning!') def __str__(self): - return f'{self.user.first_name}' - \ No newline at end of file + return f'{self.user.first_name}' \ No newline at end of file diff --git a/main/views.py b/main/views.py index d9af2bf..b4b748c 100644 --- a/main/views.py +++ b/main/views.py @@ -347,6 +347,7 @@ def contact(request): def signup(request): if request.method == 'POST': + uname = request.POST['uname'] fname = request.POST['fname'] lname = request.POST['lname'] email = request.POST['email'] @@ -355,7 +356,7 @@ def signup(request): role = request.POST['role'] if password1 == password2: # Create a new user - user = User.objects.create_user( email=email,first_name=fname,last_name=lname,password=password1,role=role) + user = User.objects.create_user(name=uname,email=email,first_name=fname,last_name=lname,password=password1,role=role) userp=UserProfile.objects.create(user=user) userp.save() user.save() @@ -363,12 +364,18 @@ def signup(request): return redirect('signin') else: messages.error(request, "Passwords do not match") + return render(request, 'main/index.html') def signin(request): if request.method == 'POST': email = request.POST['email'] password = request.POST['pass1'] - user = authenticate(request,email=email, password=password) + options = request.POST['options'] + + if options == 'username': + user = authenticate(request,name=email, password=password) + else: + user = authenticate(request,email=email, password=password) if user is not None: login(request, user) diff --git a/templates/main/index.html b/templates/main/index.html index 8b12491..52136e6 100644 --- a/templates/main/index.html +++ b/templates/main/index.html @@ -231,7 +231,16 @@

{% csrf_token %}
- + + +
+
+
diff --git a/youdemy/settings.py b/youdemy/settings.py index 2ed91b4..d142aec 100644 --- a/youdemy/settings.py +++ b/youdemy/settings.py @@ -129,4 +129,9 @@ # https://docs.djangoproject.com/en/4.2/ref/settings/#default-auto-field DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' -TIME_ZONE = 'Asia/Kolkata' \ No newline at end of file + +AUTHENTICATION_BACKENDS = [ + 'django.contrib.auth.backends.ModelBackend', # This is the default that allows us to log in via username + 'main.authentication.NameAuthBackend' +] +TIME_ZONE = 'Asia/Kolkata'