Advertisement
Guest User

Untitled

a guest
Aug 18th, 2021
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.43 KB | None | 0 0
  1.  
  2. youtube link
  3. https://www.youtube.com/watch?v=Ae7nc1EGv-A&t=246s&ab_channel=VeryAcademy
  4.  
  5.  
  6.  
  7.  
  8. from django.db import models
  9. from django.db.models.deletion import CASCADE, DO_NOTHING
  10. from django.contrib.auth.models import AbstractBaseUser,PermissionsMixin,BaseUserManager
  11. from django.utils.translation import gettext_lazy as _
  12.  
  13.  
  14.  
  15.  
  16.  
  17. class CustomAccountManager(BaseUserManager):
  18.  
  19. def create_superuser(self, email, user_name, first_name,last_name,phone_number, password, **other_fields):
  20.  
  21. other_fields.setdefault('is_staff', True)
  22. other_fields.setdefault('is_superuser', True)
  23. other_fields.setdefault('verified_email', True)
  24.  
  25. if other_fields.get('is_staff') is not True:
  26. raise ValueError(
  27. 'Superuser must be assigned to is_staff=True.')
  28. if other_fields.get('is_superuser') is not True:
  29. raise ValueError(
  30. 'Superuser must be assigned to is_superuser=True.')
  31.  
  32. return self.create_user(email, user_name, first_name,last_name,phone_number, password, **other_fields)
  33.  
  34. def create_user(self, email, user_name, first_name,last_name,phone_number, password, **other_fields):
  35.  
  36. if not email:
  37. raise ValueError(_('You must provide an email address'))
  38.  
  39. email = self.normalize_email(email)
  40. user = self.model(email=email, user_name=user_name,
  41. first_name=first_name,last_name=last_name,phone_number=phone_number **other_fields)
  42. user.set_password(password)
  43. user.save()
  44. return user
  45.  
  46.  
  47. class lecture(models.Model):
  48. name = models.CharField(max_length=50)
  49. video = models.FileField(upload_to='videos/')
  50.  
  51. class course_section(models.Model):
  52. name = models.CharField(max_length=50)
  53. description = models.CharField(max_length=50)
  54. lectures = models.ForeignKey(lecture,on_delete=models.SET_NULL,related_name="course_section",null=True)
  55.  
  56. class course(models.Model):
  57. name = models.CharField(max_length=50)
  58. description = models.CharField(max_length=500)
  59. price = models.IntegerField()
  60. course_img = models.ImageField(upload_to="images")
  61. course_sections = models.ManyToManyField(course_section)
  62.  
  63. def __str__(self) -> str:
  64. return f"{self.name} - {self.price}$"
  65.  
  66. class semester(models.Model):
  67. name = models.CharField(max_length=50)
  68. semester_courses = models.ForeignKey(course,on_delete=models.SET_NULL,related_name="semester",null=True)
  69.  
  70.  
  71.  
  72. class degree(models.Model):
  73. name = models.CharField(max_length=50)
  74. semesters = models.ForeignKey(semester,on_delete=models.SET_NULL,related_name="degree",null=True)
  75. university = models.CharField(max_length=50)
  76.  
  77.  
  78.  
  79.  
  80. class NewUser(AbstractBaseUser, PermissionsMixin):
  81.  
  82. email = models.EmailField(_('email address'), unique=True)
  83. user_name = models.CharField(max_length=150, unique=True)
  84. first_name = models.CharField(max_length=150, blank=True)
  85.  
  86. verified_email = models.BooleanField(default=False)
  87. last_name = models.CharField(max_length=50)
  88. user_image = models.ImageField(upload_to="images",null=True)
  89. phone_number = models.CharField(max_length=12)
  90. user_degree = models.ForeignKey(degree,on_delete=models.SET_NULL,related_name="users",null=True)
  91. writen_courses = models.ForeignKey(course,related_name='author',on_delete=models.SET_NULL,null=True)
  92. purchased_courses = models.ManyToManyField(course,related_name='course_users')
  93.  
  94. is_staff = models.BooleanField(default=False)
  95.  
  96. objects = CustomAccountManager()
  97.  
  98. USERNAME_FIELD = user_name
  99. REQUIRED_FIELDS = ['user_name', 'first_name']
  100. def __str__(self):
  101. return self.user_name
  102.  
  103.  
  104.  
  105. from django.contrib import admin
  106.  
  107. # Register your models here.
  108. from.models import NewUser,degree,semester,course_section,course,lecture
  109. from django.contrib import admin
  110. from .models import NewUser
  111. from django.contrib.auth.admin import UserAdmin
  112. from django.forms import TextInput, Textarea
  113.  
  114.  
  115. admin.site.register(degree)
  116.  
  117. admin.site.register(semester)
  118.  
  119. admin.site.register(course_section)
  120.  
  121. admin.site.register(course)
  122.  
  123. admin.site.register(lecture)
  124.  
  125. class UserAdminConfig(UserAdmin):
  126.  
  127. ordering = ('email')
  128. model = NewUser
  129. search_fields = ('email', 'user_name',)
  130. list_filter = ('email', 'verified_email', 'is_staff')
  131. list_display = ('email', 'user_name', 'phone_number',
  132. 'verified_email')
  133. fieldsets = (
  134. (None, {'fields': ('email', 'user_name', 'first_name','last_name','phone_number')}),
  135. ('Permissions', {'fields': ('is_staff', 'verified_email')}),
  136. )
  137. add_fieldsets = (
  138. (None, {
  139. 'classes': ('wide',),
  140. 'fields': ('email', 'user_name', 'first_name', 'password1', 'password2', 'verified_email', 'is_staff','last_name','phone_number','user_degree')}
  141. ),
  142. )
  143.  
  144.  
  145. admin.site.register(NewUser, UserAdminConfig)
  146.  
  147.  
  148.  
  149. """
  150. Django settings for ben_gurion_bkal project.
  151.  
  152. Generated by 'django-admin startproject' using Django 3.2.6.
  153.  
  154. For more information on this file, see
  155. https://docs.djangoproject.com/en/3.2/topics/settings/
  156.  
  157. For the full list of settings and their values, see
  158. https://docs.djangoproject.com/en/3.2/ref/settings/
  159. """
  160.  
  161. from pathlib import Path
  162.  
  163. # Build paths inside the project like this: BASE_DIR / 'subdir'.
  164. BASE_DIR = Path(__file__).resolve().parent.parent
  165.  
  166.  
  167. # Quick-start development settings - unsuitable for production
  168. # See https://docs.djangoproject.com/en/3.2/howto/deployment/checklist/
  169.  
  170. # SECURITY WARNING: keep the secret key used in production secret!
  171. SECRET_KEY = 'django-insecure-k@q#fs40xdi%cmy5dg*4_27&56#42u=ag&a4ws13o((6@80w2l'
  172.  
  173. # SECURITY WARNING: don't run with debug turned on in production!
  174. DEBUG = True
  175.  
  176. ALLOWED_HOSTS = []
  177.  
  178.  
  179. # Application definition
  180.  
  181. INSTALLED_APPS = [
  182. 'main_website',
  183. 'django.contrib.admin',
  184. 'django.contrib.auth',
  185. 'django.contrib.contenttypes',
  186. 'django.contrib.sessions',
  187. 'django.contrib.messages',
  188. 'django.contrib.staticfiles',
  189. ]
  190.  
  191. MIDDLEWARE = [
  192. 'django.middleware.security.SecurityMiddleware',
  193. 'django.contrib.sessions.middleware.SessionMiddleware',
  194. 'django.middleware.common.CommonMiddleware',
  195. 'django.middleware.csrf.CsrfViewMiddleware',
  196. 'django.contrib.auth.middleware.AuthenticationMiddleware',
  197. 'django.contrib.messages.middleware.MessageMiddleware',
  198. 'django.middleware.clickjacking.XFrameOptionsMiddleware',
  199. ]
  200.  
  201. ROOT_URLCONF = 'ben_gurion_bkal.urls'
  202.  
  203. TEMPLATES = [
  204. {
  205. 'BACKEND': 'django.template.backends.django.DjangoTemplates',
  206. 'DIRS': [BASE_DIR/"templates"],
  207. 'APP_DIRS': True,
  208. 'OPTIONS': {
  209. 'context_processors': [
  210. 'django.template.context_processors.debug',
  211. 'django.template.context_processors.request',
  212. 'django.contrib.auth.context_processors.auth',
  213. 'django.contrib.messages.context_processors.messages',
  214. ],
  215. },
  216. },
  217. ]
  218.  
  219. WSGI_APPLICATION = 'ben_gurion_bkal.wsgi.application'
  220.  
  221.  
  222. # Database
  223. # https://docs.djangoproject.com/en/3.2/ref/settings/#databases
  224.  
  225. DATABASES = {
  226. 'default': {
  227. 'ENGINE': 'django.db.backends.sqlite3',
  228. 'NAME': BASE_DIR / 'db.sqlite3',
  229. }
  230. }
  231.  
  232.  
  233. # Password validation
  234. # https://docs.djangoproject.com/en/3.2/ref/settings/#auth-password-validators
  235.  
  236. AUTH_PASSWORD_VALIDATORS = [
  237. {
  238. 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
  239. },
  240. {
  241. 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
  242. },
  243. {
  244. 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
  245. },
  246. {
  247. 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
  248. },
  249. ]
  250.  
  251.  
  252. # Internationalization
  253. # https://docs.djangoproject.com/en/3.2/topics/i18n/
  254.  
  255. LANGUAGE_CODE = 'en-us'
  256.  
  257. TIME_ZONE = 'UTC'
  258.  
  259. USE_I18N = True
  260.  
  261. USE_L10N = True
  262.  
  263. USE_TZ = True
  264.  
  265.  
  266. # Static files (CSS, JavaScript, Images)
  267. # https://docs.djangoproject.com/en/3.2/howto/static-files/
  268.  
  269. STATIC_URL = '/static/'
  270.  
  271. STATICFILES_DIRS = [
  272. BASE_DIR/"static"
  273.  
  274. ]
  275.  
  276.  
  277. # Default primary key field type
  278. # https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field
  279.  
  280. DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
  281.  
  282. AUTH_USER_MODEL = 'main.website.NewUser'
  283.  
  284.  
  285. from django.urls import path
  286. from . import views
  287.  
  288.  
  289. urlpatterns = [
  290. path("",views.main_website_page,name="main_website_page"),
  291. path("courses",views.all_courses_page,name="all_courses"),
  292.  
  293. #need to implement
  294. path("courses/enrolled",views.my_courses_page,name="all_courses"), #my courses
  295. path("courses/enrolled/<int:id>",views.my_courses_page,name="all_courses"), #spesefic course preview
  296. path("courses/semester/<int:semester>",views.my_courses_page,name="all_courses"), #my courses
  297. path("courses/author/<slug:slug>",views.my_courses_page,name="all_courses"), #my courses
  298. path("courses/<int:id>",views.course_page,name="course_page"), #spesefic course
  299. path("courses/<int:course_id>/lectures/<int:lecture_id>",views.course_page,name="course_page"), #spesefic lecture in a course
  300. #need to implement
  301.  
  302.  
  303.  
  304. path("register",views.register,name="register"),
  305. path("login",views.login_page,name="login"),
  306. path("logout",views.logoutUser,name="logout"),
  307.  
  308. ]
  309.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement