From a98ce7f363b0796bc4dcab2d725952686e8326a4 Mon Sep 17 00:00:00 2001 From: Manan-Arora31 Date: Sun, 29 Oct 2023 15:47:12 +0530 Subject: [PATCH 1/8] Added Username login --- main/views.py | 8 +++++++- templates/main/index.html | 13 ++++++++++++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/main/views.py b/main/views.py index 4fa6276..bcb3df4 100644 --- a/main/views.py +++ b/main/views.py @@ -287,7 +287,13 @@ 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'] + + print(options) + if options == 'username': + user = authenticate(request,first_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..77927ad 100644 --- a/templates/main/index.html +++ b/templates/main/index.html @@ -231,7 +231,18 @@

{% csrf_token %}
- + + + +
+ +
+
From 0196bf56d4326cbc750eecfe840ef47e380ade12 Mon Sep 17 00:00:00 2001 From: Manan-Arora31 Date: Sun, 29 Oct 2023 16:14:28 +0530 Subject: [PATCH 2/8] Fixed the bugs --- main/views.py | 6 +++--- templates/main/index.html | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/main/views.py b/main/views.py index bcb3df4..9cab55c 100644 --- a/main/views.py +++ b/main/views.py @@ -288,10 +288,10 @@ def signin(request): email = request.POST['email'] password = request.POST['pass1'] options = request.POST['options'] - print(options) - if options == 'username': - user = authenticate(request,first_name=email, password=password) + print(email) + if options == 'firstname': + user = authenticate(request,username=email, password=password) else: user = authenticate(request,email=email, password=password) diff --git a/templates/main/index.html b/templates/main/index.html index 77927ad..8d0d621 100644 --- a/templates/main/index.html +++ b/templates/main/index.html @@ -235,14 +235,14 @@

Sign in using:

- +
From c634f7fc58f288362a52d956cec91750c84f468d Mon Sep 17 00:00:00 2001 From: Manan-Arora31 Date: Sun, 29 Oct 2023 16:31:01 +0530 Subject: [PATCH 3/8] Fixed the username field --- main/views.py | 6 ++---- templates/main/index.html | 4 ++-- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/main/views.py b/main/views.py index 9cab55c..c080e10 100644 --- a/main/views.py +++ b/main/views.py @@ -288,10 +288,8 @@ def signin(request): email = request.POST['email'] password = request.POST['pass1'] options = request.POST['options'] - print(options) - print(email) - if options == 'firstname': - user = authenticate(request,username=email, password=password) + if options == 'username': + user = authenticate(request,uname=email, password=password) else: user = authenticate(request,email=email, password=password) diff --git a/templates/main/index.html b/templates/main/index.html index 8d0d621..3b2f29e 100644 --- a/templates/main/index.html +++ b/templates/main/index.html @@ -236,13 +236,13 @@

- +
From c17932f083859585afb11d90298975a22530a48a Mon Sep 17 00:00:00 2001 From: Manan-Arora31 Date: Sun, 29 Oct 2023 23:07:18 +0530 Subject: [PATCH 4/8] Changed the models --- main/models.py | 12 ++++++------ main/views.py | 3 ++- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/main/models.py b/main/models.py index f0a9d2c..a0fd8b3 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,username, 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(username = username,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, email,username, 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,username, 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 + username = 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 = ['username'] objects = UserManager() diff --git a/main/views.py b/main/views.py index c080e10..2c956bf 100644 --- a/main/views.py +++ b/main/views.py @@ -273,10 +273,11 @@ def signup(request): email = request.POST['email'] password1= request.POST['pass1'] password2= request.POST['pass2'] + username= request.POST['uname'] 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( email=email,username=username,first_name=fname,last_name=lname,password=password1,role=role) user.save() messages.success(request, "User registered successfully") return redirect('signin') From 2bc68d51a04a49f0a4bf53ca2cf23a2f3a6c8b7f Mon Sep 17 00:00:00 2001 From: Manan-Arora31 Date: Mon, 30 Oct 2023 15:53:58 +0530 Subject: [PATCH 5/8] Revert "Changed the models" This reverts commit c17932f083859585afb11d90298975a22530a48a. --- main/models.py | 12 ++++++------ main/views.py | 3 +-- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/main/models.py b/main/models.py index a0fd8b3..f0a9d2c 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,username, password, **extra_fields): + def _create_user(self, email, 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(username = username,email=email, **extra_fields) + user = self.model(email=email, **extra_fields) user.set_password(password) user.save(using=self._db) return user - def create_user(self, email,username, password=None, **extra_fields): + def create_user(self, 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,username, password, **extra_fields) + return self._create_user(email, 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 = models.CharField(max_length=20,unique=True) + username = None email = models.EmailField(_('email address'), unique=True) role=models.CharField(choices=ROLE,max_length=10) USERNAME_FIELD = 'email' - REQUIRED_FIELDS = ['username'] + REQUIRED_FIELDS = [] objects = UserManager() diff --git a/main/views.py b/main/views.py index 2c956bf..c080e10 100644 --- a/main/views.py +++ b/main/views.py @@ -273,11 +273,10 @@ def signup(request): email = request.POST['email'] password1= request.POST['pass1'] password2= request.POST['pass2'] - username= request.POST['uname'] role = request.POST['role'] if password1 == password2: # Create a new user - user = User.objects.create_user( email=email,username=username,first_name=fname,last_name=lname,password=password1,role=role) + user = User.objects.create_user( email=email,first_name=fname,last_name=lname,password=password1,role=role) user.save() messages.success(request, "User registered successfully") return redirect('signin') From 51e2ca9f67ad9de5bdde0e4d331f0418f7d935db Mon Sep 17 00:00:00 2001 From: Manan-Arora31 Date: Mon, 30 Oct 2023 23:06:34 +0530 Subject: [PATCH 6/8] Completed the functionality --- main/authentication.py | 32 ++++++++++++++++++++++++++++++++ main/views.py | 4 +++- templates/main/index.html | 6 ++---- youdemy/settings.py | 5 +++++ 4 files changed, 42 insertions(+), 5 deletions(-) create mode 100644 main/authentication.py diff --git a/main/authentication.py b/main/authentication.py new file mode 100644 index 0000000..c38cbc6 --- /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, first_name=None, password=None): + """ + Overrides the authenticate method + """ + try: + if email == None: + user = User.objects.get(first_name=first_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/views.py b/main/views.py index c080e10..f84e66f 100644 --- a/main/views.py +++ b/main/views.py @@ -282,14 +282,16 @@ 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'] options = request.POST['options'] + if options == 'username': - user = authenticate(request,uname=email, password=password) + user = authenticate(request,first_name=email, password=password) else: user = authenticate(request,email=email, password=password) diff --git a/templates/main/index.html b/templates/main/index.html index 3b2f29e..52e4f6c 100644 --- a/templates/main/index.html +++ b/templates/main/index.html @@ -235,14 +235,12 @@

Sign in using: -

-
- +
diff --git a/youdemy/settings.py b/youdemy/settings.py index 7a885b4..f522110 100644 --- a/youdemy/settings.py +++ b/youdemy/settings.py @@ -129,3 +129,8 @@ # https://docs.djangoproject.com/en/4.2/ref/settings/#default-auto-field DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' + +AUTHENTICATION_BACKENDS = [ + 'django.contrib.auth.backends.ModelBackend', # This is the default that allows us to log in via username + 'main.authentication.NameAuthBackend' +] \ No newline at end of file From 14c9478d85370f4a7d37e00d2f006bd456ba7e71 Mon Sep 17 00:00:00 2001 From: Manan-Arora31 Date: Tue, 31 Oct 2023 01:35:01 +0530 Subject: [PATCH 7/8] Changed from firstname to username login --- main/authentication.py | 4 ++-- main/models.py | 20 ++++++++++---------- main/views.py | 5 +++-- templates/main/index.html | 4 ++-- 4 files changed, 17 insertions(+), 16 deletions(-) diff --git a/main/authentication.py b/main/authentication.py index c38cbc6..73d5883 100644 --- a/main/authentication.py +++ b/main/authentication.py @@ -7,13 +7,13 @@ class NameAuthBackend: Allows users to log in using their first name. """ - def authenticate(self, request, email=None, first_name=None, password=None): + def authenticate(self, request, email=None, name=None, password=None): """ Overrides the authenticate method """ try: if email == None: - user = User.objects.get(first_name=first_name) + user = User.objects.get(name=name) else: user = User.objects.get(email=email) if user.check_password(password): diff --git a/main/models.py b/main/models.py index f0a9d2c..dc03512 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,12 +74,12 @@ 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 \ No newline at end of file diff --git a/main/views.py b/main/views.py index f84e66f..7f6a206 100644 --- a/main/views.py +++ b/main/views.py @@ -268,6 +268,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'] @@ -276,7 +277,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) user.save() messages.success(request, "User registered successfully") return redirect('signin') @@ -291,7 +292,7 @@ def signin(request): options = request.POST['options'] if options == 'username': - user = authenticate(request,first_name=email, password=password) + user = authenticate(request,name=email, password=password) else: user = authenticate(request,email=email, password=password) diff --git a/templates/main/index.html b/templates/main/index.html index 52e4f6c..52136e6 100644 --- a/templates/main/index.html +++ b/templates/main/index.html @@ -235,12 +235,12 @@

Sign in using:

- +
From 346884a0cab3b5e9452a6ed80334ff9e07c6cf27 Mon Sep 17 00:00:00 2001 From: Manan-Arora31 Date: Tue, 31 Oct 2023 16:16:07 +0530 Subject: [PATCH 8/8] Fixed errors --- main/models.py | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/main/models.py b/main/models.py index a7ebf89..da84a57 100644 --- a/main/models.py +++ b/main/models.py @@ -83,3 +83,57 @@ class Questions(models.Model): answer=models.TextField() def _str_(self): return self.question + +class Tag(models.Model): + name = models.CharField(max_length=50, unique=True) + def __str__(self): + return self.name +class Blogs(models.Model): + author=models.ForeignKey(User, on_delete=models.CASCADE) + title=models.CharField(max_length=100,blank=False) + intro=models.TextField() + description=models.TextField() + conclusion=models.TextField() + blog_picture=models.ImageField(null=True,upload_to='image/blog/') + tags=models.ManyToManyField(Tag,related_name='Blogs',blank=True) + created_at=models.DateTimeField(default=timezone.now) + def get_comments(self): + return self.comments.filter(parent=None) + def save(self, *args, **kwargs): + # Set the 'created_at' field to the current local time + if not self.created_at: + self.created_at = timezone.now() + super(Blogs, self).save(*args, **kwargs) + def __str__(self): + return self.title + +class BlogLike(models.Model): + blog=models.ForeignKey(Blogs,on_delete=models.CASCADE) + user=models.ForeignKey(User, on_delete=models.CASCADE) + like=models.BooleanField(default=None) + + class Meta: + unique_together = ['blog','user'] + def __str__(self): + return f'{self.user} {"liked" if self.like else "disliked"} {self.blog}' + +class Comment(models.Model): + user=models.ForeignKey(User, on_delete=models.CASCADE) + blog = models.ForeignKey(Blogs, on_delete=models.CASCADE,related_name="comments") # Add this field + parent = models.ForeignKey('self', null=True, blank=True, on_delete=models.CASCADE) + message = models.TextField() + timestamp = models.DateTimeField(auto_now_add=True) + class Meta: + ordering = ('timestamp',) + def get_comments(self): + return Comment.objects.filter(parent=self) + def __str__(self): + return f'Comment by {self.user.email} on {self.timestamp}' + +class UserProfile(models.Model): + user=models.ForeignKey(User,on_delete=models.CASCADE,related_name='userprofiles') + profile_picture=models.ImageField(null=True,default='defaultpic.jpg',upload_to='image/profile') + bio=models.TextField(default='I am passionate about learning!') + + def __str__(self): + return f'{self.user.first_name}' \ No newline at end of file