Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- youtube link
- https://www.youtube.com/watch?v=Ae7nc1EGv-A&t=246s&ab_channel=VeryAcademy
- from django.db import models
- from django.db.models.deletion import CASCADE, DO_NOTHING
- from django.contrib.auth.models import AbstractBaseUser,PermissionsMixin,BaseUserManager
- from django.utils.translation import gettext_lazy as _
- class CustomAccountManager(BaseUserManager):
- def create_superuser(self, email, user_name, first_name,last_name,phone_number, password, **other_fields):
- other_fields.setdefault('is_staff', True)
- other_fields.setdefault('is_superuser', True)
- other_fields.setdefault('verified_email', True)
- if other_fields.get('is_staff') is not True:
- raise ValueError(
- 'Superuser must be assigned to is_staff=True.')
- if other_fields.get('is_superuser') is not True:
- raise ValueError(
- 'Superuser must be assigned to is_superuser=True.')
- return self.create_user(email, user_name, first_name,last_name,phone_number, password, **other_fields)
- def create_user(self, email, user_name, first_name,last_name,phone_number, password, **other_fields):
- if not email:
- raise ValueError(_('You must provide an email address'))
- email = self.normalize_email(email)
- user = self.model(email=email, user_name=user_name,
- first_name=first_name,last_name=last_name,phone_number=phone_number **other_fields)
- user.set_password(password)
- user.save()
- return user
- class lecture(models.Model):
- name = models.CharField(max_length=50)
- video = models.FileField(upload_to='videos/')
- class course_section(models.Model):
- name = models.CharField(max_length=50)
- description = models.CharField(max_length=50)
- lectures = models.ForeignKey(lecture,on_delete=models.SET_NULL,related_name="course_section",null=True)
- class course(models.Model):
- name = models.CharField(max_length=50)
- description = models.CharField(max_length=500)
- price = models.IntegerField()
- course_img = models.ImageField(upload_to="images")
- course_sections = models.ManyToManyField(course_section)
- def __str__(self) -> str:
- return f"{self.name} - {self.price}$"
- class semester(models.Model):
- name = models.CharField(max_length=50)
- semester_courses = models.ForeignKey(course,on_delete=models.SET_NULL,related_name="semester",null=True)
- class degree(models.Model):
- name = models.CharField(max_length=50)
- semesters = models.ForeignKey(semester,on_delete=models.SET_NULL,related_name="degree",null=True)
- university = models.CharField(max_length=50)
- class NewUser(AbstractBaseUser, PermissionsMixin):
- email = models.EmailField(_('email address'), unique=True)
- user_name = models.CharField(max_length=150, unique=True)
- first_name = models.CharField(max_length=150, blank=True)
- verified_email = models.BooleanField(default=False)
- last_name = models.CharField(max_length=50)
- user_image = models.ImageField(upload_to="images",null=True)
- phone_number = models.CharField(max_length=12)
- user_degree = models.ForeignKey(degree,on_delete=models.SET_NULL,related_name="users",null=True)
- writen_courses = models.ForeignKey(course,related_name='author',on_delete=models.SET_NULL,null=True)
- purchased_courses = models.ManyToManyField(course,related_name='course_users')
- is_staff = models.BooleanField(default=False)
- objects = CustomAccountManager()
- USERNAME_FIELD = user_name
- REQUIRED_FIELDS = ['user_name', 'first_name']
- def __str__(self):
- return self.user_name
- from django.contrib import admin
- # Register your models here.
- from.models import NewUser,degree,semester,course_section,course,lecture
- from django.contrib import admin
- from .models import NewUser
- from django.contrib.auth.admin import UserAdmin
- from django.forms import TextInput, Textarea
- admin.site.register(degree)
- admin.site.register(semester)
- admin.site.register(course_section)
- admin.site.register(course)
- admin.site.register(lecture)
- class UserAdminConfig(UserAdmin):
- ordering = ('email')
- model = NewUser
- search_fields = ('email', 'user_name',)
- list_filter = ('email', 'verified_email', 'is_staff')
- list_display = ('email', 'user_name', 'phone_number',
- 'verified_email')
- fieldsets = (
- (None, {'fields': ('email', 'user_name', 'first_name','last_name','phone_number')}),
- ('Permissions', {'fields': ('is_staff', 'verified_email')}),
- )
- add_fieldsets = (
- (None, {
- 'classes': ('wide',),
- 'fields': ('email', 'user_name', 'first_name', 'password1', 'password2', 'verified_email', 'is_staff','last_name','phone_number','user_degree')}
- ),
- )
- admin.site.register(NewUser, UserAdminConfig)
- """
- Django settings for ben_gurion_bkal project.
- Generated by 'django-admin startproject' using Django 3.2.6.
- For more information on this file, see
- https://docs.djangoproject.com/en/3.2/topics/settings/
- For the full list of settings and their values, see
- https://docs.djangoproject.com/en/3.2/ref/settings/
- """
- from pathlib import Path
- # Build paths inside the project like this: BASE_DIR / 'subdir'.
- BASE_DIR = Path(__file__).resolve().parent.parent
- # Quick-start development settings - unsuitable for production
- # See https://docs.djangoproject.com/en/3.2/howto/deployment/checklist/
- # SECURITY WARNING: keep the secret key used in production secret!
- SECRET_KEY = 'django-insecure-k@q#fs40xdi%cmy5dg*4_27&56#42u=ag&a4ws13o((6@80w2l'
- # SECURITY WARNING: don't run with debug turned on in production!
- DEBUG = True
- ALLOWED_HOSTS = []
- # Application definition
- INSTALLED_APPS = [
- 'main_website',
- 'django.contrib.admin',
- 'django.contrib.auth',
- 'django.contrib.contenttypes',
- 'django.contrib.sessions',
- 'django.contrib.messages',
- 'django.contrib.staticfiles',
- ]
- MIDDLEWARE = [
- 'django.middleware.security.SecurityMiddleware',
- 'django.contrib.sessions.middleware.SessionMiddleware',
- 'django.middleware.common.CommonMiddleware',
- 'django.middleware.csrf.CsrfViewMiddleware',
- 'django.contrib.auth.middleware.AuthenticationMiddleware',
- 'django.contrib.messages.middleware.MessageMiddleware',
- 'django.middleware.clickjacking.XFrameOptionsMiddleware',
- ]
- ROOT_URLCONF = 'ben_gurion_bkal.urls'
- TEMPLATES = [
- {
- 'BACKEND': 'django.template.backends.django.DjangoTemplates',
- 'DIRS': [BASE_DIR/"templates"],
- 'APP_DIRS': True,
- 'OPTIONS': {
- 'context_processors': [
- 'django.template.context_processors.debug',
- 'django.template.context_processors.request',
- 'django.contrib.auth.context_processors.auth',
- 'django.contrib.messages.context_processors.messages',
- ],
- },
- },
- ]
- WSGI_APPLICATION = 'ben_gurion_bkal.wsgi.application'
- # Database
- # https://docs.djangoproject.com/en/3.2/ref/settings/#databases
- DATABASES = {
- 'default': {
- 'ENGINE': 'django.db.backends.sqlite3',
- 'NAME': BASE_DIR / 'db.sqlite3',
- }
- }
- # Password validation
- # https://docs.djangoproject.com/en/3.2/ref/settings/#auth-password-validators
- AUTH_PASSWORD_VALIDATORS = [
- {
- 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
- },
- {
- 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
- },
- {
- 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
- },
- {
- 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
- },
- ]
- # Internationalization
- # https://docs.djangoproject.com/en/3.2/topics/i18n/
- LANGUAGE_CODE = 'en-us'
- TIME_ZONE = 'UTC'
- USE_I18N = True
- USE_L10N = True
- USE_TZ = True
- # Static files (CSS, JavaScript, Images)
- # https://docs.djangoproject.com/en/3.2/howto/static-files/
- STATIC_URL = '/static/'
- STATICFILES_DIRS = [
- BASE_DIR/"static"
- ]
- # Default primary key field type
- # https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field
- DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
- AUTH_USER_MODEL = 'main.website.NewUser'
- from django.urls import path
- from . import views
- urlpatterns = [
- path("",views.main_website_page,name="main_website_page"),
- path("courses",views.all_courses_page,name="all_courses"),
- #need to implement
- path("courses/enrolled",views.my_courses_page,name="all_courses"), #my courses
- path("courses/enrolled/<int:id>",views.my_courses_page,name="all_courses"), #spesefic course preview
- path("courses/semester/<int:semester>",views.my_courses_page,name="all_courses"), #my courses
- path("courses/author/<slug:slug>",views.my_courses_page,name="all_courses"), #my courses
- path("courses/<int:id>",views.course_page,name="course_page"), #spesefic course
- path("courses/<int:course_id>/lectures/<int:lecture_id>",views.course_page,name="course_page"), #spesefic lecture in a course
- #need to implement
- path("register",views.register,name="register"),
- path("login",views.login_page,name="login"),
- path("logout",views.logoutUser,name="logout"),
- ]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement