@ -1,3 +1,8 @@ |
||||
from django.contrib import admin |
||||
|
||||
from .models import BaseUser,Role |
||||
|
||||
# Register your models here. |
||||
|
||||
admin.site.register(BaseUser) |
||||
admin.site.register(Role) |
||||
|
@ -1,2 +0,0 @@ |
||||
app_password ="duse xjaj pxkg wyur" |
||||
app_email = "dmyemail254@gmail.com" |
@ -0,0 +1,45 @@ |
||||
from django import forms |
||||
from django.contrib.auth.forms import UserCreationForm |
||||
from .models import BaseUser |
||||
|
||||
class BaseUserForm(UserCreationForm): |
||||
class Meta: |
||||
model = BaseUser |
||||
fields = [ |
||||
'first_name', |
||||
'last_name', |
||||
'bio', |
||||
'address', |
||||
'contact_number', |
||||
'image', |
||||
'citizenship', |
||||
'certificate', |
||||
'price', |
||||
'role', |
||||
'email', |
||||
] |
||||
password1 = forms.CharField( |
||||
widget=forms.PasswordInput(attrs={'class': 'form-control', 'placeholder': 'Password'}), |
||||
label='Password', |
||||
min_length=8, |
||||
) |
||||
password2 = forms.CharField( |
||||
widget=forms.PasswordInput(attrs={'class': 'form-control', 'placeholder': 'Confirm Password'}), |
||||
label='Confirm Password', |
||||
min_length=8, |
||||
) |
||||
|
||||
def clean_password2(self): |
||||
password1 = self.cleaned_data.get('password1') |
||||
password2 = self.cleaned_data.get('password2') |
||||
|
||||
if password1 and password2 and password1 != password2: |
||||
raise forms.ValidationError("Passwords don't match.") |
||||
return password2 |
||||
|
||||
def save(self, commit=True): |
||||
user = super().save(commit=False) |
||||
user.set_password(self.cleaned_data['password1']) |
||||
if commit: |
||||
user.save() |
||||
return user |
@ -0,0 +1,64 @@ |
||||
# Generated by Django 5.1.4 on 2025-01-11 11:43 |
||||
|
||||
import django.contrib.auth.models |
||||
import django.db.models.deletion |
||||
import django.utils.timezone |
||||
from django.db import migrations, models |
||||
|
||||
|
||||
class Migration(migrations.Migration): |
||||
|
||||
initial = True |
||||
|
||||
dependencies = [ |
||||
('auth', '0012_alter_user_first_name_max_length'), |
||||
] |
||||
|
||||
operations = [ |
||||
migrations.CreateModel( |
||||
name='Role', |
||||
fields=[ |
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), |
||||
('role', models.CharField(max_length=20)), |
||||
('role_desc', models.TextField()), |
||||
], |
||||
), |
||||
migrations.CreateModel( |
||||
name='BaseUser', |
||||
fields=[ |
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), |
||||
('password', models.CharField(max_length=128, verbose_name='password')), |
||||
('last_login', models.DateTimeField(blank=True, null=True, verbose_name='last login')), |
||||
('is_superuser', models.BooleanField(default=False, help_text='Designates that this user has all permissions without explicitly assigning them.', verbose_name='superuser status')), |
||||
('first_name', models.CharField(blank=True, max_length=150, verbose_name='first name')), |
||||
('last_name', models.CharField(blank=True, max_length=150, verbose_name='last name')), |
||||
('is_staff', models.BooleanField(default=False, help_text='Designates whether the user can log into this admin site.', verbose_name='staff status')), |
||||
('is_active', models.BooleanField(default=True, help_text='Designates whether this user should be treated as active. Unselect this instead of deleting accounts.', verbose_name='active')), |
||||
('date_joined', models.DateTimeField(default=django.utils.timezone.now, verbose_name='date joined')), |
||||
('image', models.ImageField(blank=True, null=True, upload_to='')), |
||||
('bio', models.TextField(blank=True, null=True)), |
||||
('address', models.CharField(blank=True, max_length=50, null=True)), |
||||
('contact_number', models.CharField(blank=True, max_length=15, null=True, unique=True)), |
||||
('email', models.EmailField(max_length=254, unique=True)), |
||||
('citizenship', models.FileField(blank=True, null=True, upload_to='uploads/citizenship')), |
||||
('certificate', models.FileField(blank=True, null=True, upload_to='uploads/certificates')), |
||||
('token', models.CharField(blank=True, max_length=64, null=True)), |
||||
('is_verified', models.BooleanField(default=False)), |
||||
('price', models.DecimalField(blank=True, decimal_places=2, max_digits=9999, null=True)), |
||||
('ratings', models.IntegerField(default=1)), |
||||
('member_since', models.DateTimeField(auto_now_add=True)), |
||||
('profile_updated', models.DateTimeField(auto_now=True)), |
||||
('groups', models.ManyToManyField(blank=True, help_text='The groups this user belongs to. A user will get all permissions granted to each of their groups.', related_name='user_set', related_query_name='user', to='auth.group', verbose_name='groups')), |
||||
('user_permissions', models.ManyToManyField(blank=True, help_text='Specific permissions for this user.', related_name='user_set', related_query_name='user', to='auth.permission', verbose_name='user permissions')), |
||||
('role', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='users', to='main.role')), |
||||
], |
||||
options={ |
||||
'verbose_name': 'user', |
||||
'verbose_name_plural': 'users', |
||||
'abstract': False, |
||||
}, |
||||
managers=[ |
||||
('objects', django.contrib.auth.models.UserManager()), |
||||
], |
||||
), |
||||
] |
@ -1,32 +1,43 @@ |
||||
from django.db import models |
||||
from django.contrib.auth.models import User |
||||
from django.contrib.auth.models import AbstractUser |
||||
from django.dispatch import receiver |
||||
from django.db.models.signals import post_save |
||||
|
||||
|
||||
|
||||
# Create your models here. |
||||
|
||||
class Role(models.Model): |
||||
role = models.CharField(max_length=20) |
||||
role_desc = models.TextField() |
||||
|
||||
def __str__(self): |
||||
return self.role |
||||
|
||||
return self.role |
||||
|
||||
class BaseUser(models.Model): |
||||
user = models.OneToOneField(User, on_delete=models.CASCADE, related_name="user") |
||||
image = models.ImageField() |
||||
class BaseUser(AbstractUser): |
||||
username = None |
||||
image = models.ImageField(null=True, blank=True) |
||||
bio = models.TextField(null=True, blank=True) |
||||
address = models.CharField(null=True, blank=True, max_length=50) |
||||
contact_number = models.CharField(unique=True, max_length=15) |
||||
contact_number = models.CharField(unique=True, max_length=15, null=True, blank=True) |
||||
email = models.EmailField(unique=True) |
||||
citizenship = models.FileField(upload_to ='uploads/citizenship') |
||||
certificate = models.FileField(upload_to='uploads/certificates') |
||||
is_activated = models.BooleanField(default=False) |
||||
citizenship = models.FileField(upload_to='uploads/citizenship', null=True, blank=True) |
||||
certificate = models.FileField(upload_to='uploads/certificates', blank=True, null=True) |
||||
#is_activated = models.BooleanField(default=False) |
||||
token = models.CharField(max_length=64,blank=True,null=True) |
||||
is_verified = models.BooleanField(default=False) |
||||
price = models.DecimalField(max_digits=9999,decimal_places=2) |
||||
ratings = models.PositiveIntegerField() |
||||
role = models.ForeignKey(Role, on_delete=models.CASCADE, related_name="user_role") |
||||
def __str__(self): |
||||
return self.user.username |
||||
USERNAME_FIELD = "email" |
||||
price = models.DecimalField(max_digits=9999, decimal_places=2, null=True, blank=True) |
||||
ratings = models.IntegerField(default=1) |
||||
role = models.ForeignKey('Role', on_delete=models.SET_NULL, related_name="users", null=True, blank=True) |
||||
member_since = models.DateTimeField(auto_now_add=True) |
||||
profile_updated = models.DateTimeField(auto_now=True) |
||||
|
||||
USERNAME_FIELD = "email" |
||||
REQUIRED_FIELDS = ["image", "contact_number", "bio", "address", "citizenship", "certificate", "price", "password"] |
||||
|
||||
def __str__(self): |
||||
return self.email |
||||
|
||||
@receiver(post_save, sender=AbstractUser) |
||||
def user_to_inactive(sender, instance, created, update_fields, **kwargs): |
||||
if created: |
||||
instance.is_active = False |
||||
|
@ -0,0 +1,17 @@ |
||||
<!DOCTYPE html> |
||||
<html lang="en"> |
||||
<head> |
||||
<meta charset="UTF-8"> |
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> |
||||
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css"> |
||||
<title>Document</title> |
||||
</head> |
||||
<body> |
||||
|
||||
<form method="post"> |
||||
{% csrf_token %} |
||||
{{ form.as_p }} |
||||
<button type="submit">Create</button> |
||||
</form> |
||||
</body> |
||||
</html> |
@ -0,0 +1,5 @@ |
||||
<form method="post"> |
||||
{% csrf_token %} |
||||
<p>Are you sure you want to delete {{ object }}?</p> |
||||
<button type="submit">Delete</button> |
||||
</form> |
@ -0,0 +1,65 @@ |
||||
{% load static %} |
||||
|
||||
<!DOCTYPE html> |
||||
<html lang="en"> |
||||
<head> |
||||
<meta charset="UTF-8"> |
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> |
||||
<title>Welcome to Sahara</title> |
||||
<style> |
||||
body { |
||||
font-family: Arial, sans-serif; |
||||
color: #333; |
||||
background-color: #f7f7f7; |
||||
padding: 30px; |
||||
} |
||||
|
||||
.email-container { |
||||
background-color: #ffffff; |
||||
padding: 20px; |
||||
border-radius: 8px; |
||||
max-width: 600px; |
||||
margin: 0 auto; |
||||
} |
||||
|
||||
h1 { |
||||
color: #2c3e50; |
||||
} |
||||
|
||||
.company-logo { |
||||
max-width: 150px; |
||||
margin-bottom: 20px; |
||||
} |
||||
|
||||
.cta-button { |
||||
background-color: #3498db; |
||||
color: #ffffff; |
||||
padding: 10px 20px; |
||||
border-radius: 5px; |
||||
text-decoration: none; |
||||
display: inline-block; |
||||
font-size: 16px; |
||||
} |
||||
|
||||
.cta-button:hover { |
||||
background-color: #2980b9; |
||||
} |
||||
|
||||
p { |
||||
font-size: 16px; |
||||
} |
||||
</style> |
||||
</head> |
||||
<body> |
||||
<div class="email-container"> |
||||
<h1>Welcome to Sahara!</h1> |
||||
<img src="{% static 'images/sahara_logo.png' %}" alt="Sahara" class="company-logo"> |
||||
<p>Hello {{ user.first_name }},</p> |
||||
<p>Thank you for signing up with Sahara! We're excited to have you on board.</p> |
||||
<p>Please click the button below to activate your account:</p> |
||||
<a href="{{ activation_link }}" class="cta-button">Activate Your Account</a> |
||||
<p>If you did not sign up for this account, please ignore this email.</p> |
||||
<p>Best regards,<br>Sahara Team</p> |
||||
</div> |
||||
</body> |
||||
</html> |
@ -0,0 +1,14 @@ |
||||
Welcome to Sahara! |
||||
|
||||
Hello {{ user.first_name }}, |
||||
|
||||
Thank you for signing up with Sahara! We're excited to have you on board. |
||||
|
||||
Please click the link below to activate your account: |
||||
|
||||
{{ activation_link }} |
||||
|
||||
If you did not sign up for this account, please ignore this email. |
||||
|
||||
Best regards, |
||||
Sahara Team |
@ -0,0 +1,5 @@ |
||||
<form method="post"> |
||||
{% csrf_token %} |
||||
{{ form.as_p }} |
||||
<button type="submit">Update</button> |
||||
</form> |
@ -0,0 +1,5 @@ |
||||
|
||||
{% for user in users %} |
||||
<p><a href="{% url 'update_user' pk=user.pk %}">{{ user }}</a> |
||||
<a href="{% url 'delete_user' pk=user.pk %}">(delete)</a></p> |
||||
{% endfor %} |
@ -1,7 +1,9 @@ |
||||
from django.contrib import admin |
||||
from django.urls import path, include |
||||
from .views import homeView |
||||
from django.urls import path |
||||
from .views import homeView,send_mail_page,createUserView,activate_account |
||||
|
||||
urlpatterns = [ |
||||
path('',homeView, name="home"), |
||||
path('send-email/',send_mail_page,name="sendmail"), |
||||
path('user/register/',createUserView,name="register"), |
||||
path('activate/<str:token>/', activate_account, name='activate'), |
||||
] |
||||
|
@ -0,0 +1,26 @@ |
||||
from django.core.mail import send_mail |
||||
from django.template.loader import render_to_string |
||||
from django.utils.html import strip_tags |
||||
from django.conf import settings |
||||
from django.urls import reverse |
||||
from django.contrib.sites.shortcuts import get_current_site |
||||
from django.contrib.auth.tokens import default_token_generator |
||||
|
||||
def send_welcome_email(user, request): |
||||
token = 'generated_token_here' |
||||
activation_link = f"{get_current_site(request).domain}{reverse('account:activate', args=[token])}" |
||||
subject = "Welcome to Sahara - Please Activate Your Account" |
||||
|
||||
html_message = render_to_string('welcome_email.html', { |
||||
'user': user, |
||||
'activation_link': activation_link, |
||||
}) |
||||
plain_message = strip_tags(html_message) |
||||
|
||||
send_mail( |
||||
subject, |
||||
plain_message, |
||||
settings.DEFAULT_FROM_EMAIL, |
||||
[user.email], |
||||
html_message=html_message, |
||||
) |
@ -1,7 +1,91 @@ |
||||
from django.shortcuts import render,HttpResponse |
||||
|
||||
|
||||
from django.shortcuts import render,redirect,HttpResponse |
||||
from django.core.mail import send_mail |
||||
from django.conf import settings |
||||
from django.template.loader import render_to_string |
||||
from django.utils.html import strip_tags |
||||
from django.conf import settings |
||||
from django.urls import reverse |
||||
from django.contrib.sites.shortcuts import get_current_site |
||||
from . forms import BaseUserForm |
||||
from django.contrib import messages |
||||
from django.contrib.auth.tokens import default_token_generator |
||||
# Create your views here. |
||||
|
||||
from .models import BaseUser |
||||
|
||||
def homeView(request): |
||||
return HttpResponse("<h1>Home Page</h1>") |
||||
return HttpResponse("home") |
||||
|
||||
def createUserView(request): |
||||
if request.method == 'POST': |
||||
form = BaseUserForm(request.POST, request.FILES) |
||||
print(form) |
||||
if form.is_valid(): |
||||
form.save() |
||||
form.is_a |
||||
#return redirect('home') |
||||
return HttpResponse("<h1>User Created</h1>") |
||||
else: |
||||
form = BaseUserForm() |
||||
return render(request, 'main/create_user.html', { |
||||
'form': form |
||||
}) |
||||
|
||||
|
||||
def send_mail_page(): |
||||
address = "sajeshmaan@gmail.com" |
||||
subject = "Congrts Team Flash" |
||||
message = "Congrts Team Flash hackx committe has announced your team as a winner" |
||||
try: |
||||
send_mail(subject, message, settings.EMAIL_HOST_USER, [address,'rajakiaryal0@gmail.com']) |
||||
result= 'Email sent successfully' |
||||
except Exception as e: |
||||
result = f'Error sending email: {e}' |
||||
|
||||
|
||||
def send_welcome_email(user, request): |
||||
# Generate activation link |
||||
user = BaseUser.objects.get(email="user@example.com") # Replace with your user object |
||||
token = default_token_generator.make_token(user) |
||||
token = 'generated_token_here' # This token will be generated based on your activation logic |
||||
activation_link = f"{get_current_site(request).domain}{reverse('account:activate', args=[token])}" |
||||
|
||||
# Prepare email subject and message |
||||
subject = "Welcome to Sahara - Please Activate Your Account" |
||||
|
||||
# Render the HTML email |
||||
html_message = render_to_string('welcome_email.html', { |
||||
'user': user, |
||||
'activation_link': activation_link, |
||||
}) |
||||
|
||||
# Render the plain-text version of the email |
||||
plain_message = strip_tags(html_message) |
||||
|
||||
# Send the email |
||||
send_mail( |
||||
subject, |
||||
plain_message, |
||||
settings.DEFAULT_FROM_EMAIL, |
||||
[user.email], |
||||
html_message=html_message, |
||||
) |
||||
|
||||
|
||||
|
||||
|
||||
def activate_account(request, token): |
||||
try: |
||||
# Decode token and retrieve user |
||||
user = User.objects.get(email__iexact=user_email_from_token) |
||||
if user: |
||||
# Activate user account |
||||
user.is_active = True |
||||
user.save() |
||||
messages.success(request, "Account activated successfully!") |
||||
return redirect('login') # Redirect to the login page |
||||
else: |
||||
messages.error(request, "Invalid activation link.") |
||||
except Exception as e: |
||||
messages.error(request, str(e)) |
||||
return redirect('home') # Redirect to homepage in case of error |
||||
|
After Width: | Height: | Size: 273 KiB |
After Width: | Height: | Size: 273 KiB |
After Width: | Height: | Size: 273 KiB |
After Width: | Height: | Size: 273 KiB |
After Width: | Height: | Size: 28 KiB |
After Width: | Height: | Size: 28 KiB |
After Width: | Height: | Size: 273 KiB |
@ -1,23 +1,12 @@ |
||||
""" |
||||
URL configuration for sahara project. |
||||
|
||||
The `urlpatterns` list routes URLs to views. For more information please see: |
||||
https://docs.djangoproject.com/en/5.1/topics/http/urls/ |
||||
Examples: |
||||
Function views |
||||
1. Add an import: from my_app import views |
||||
2. Add a URL to urlpatterns: path('', views.home, name='home') |
||||
Class-based views |
||||
1. Add an import: from other_app.views import Home |
||||
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') |
||||
Including another URLconf |
||||
1. Import the include() function: from django.urls import include, path |
||||
2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) |
||||
""" |
||||
from django.contrib import admin |
||||
from django.urls import path, include |
||||
from django.urls import path,include |
||||
from django.conf import settings |
||||
from django.conf.urls.static import static |
||||
|
||||
urlpatterns = [ |
||||
path('admin/', admin.site.urls), |
||||
path('',include("main.urls")) |
||||
] |
||||
|
||||
if settings.DEBUG: |
||||
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) |