Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Project app added, swagger configured and user api generated #7

Open
wants to merge 12 commits into
base: muqadim/admin_functionalities
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 38 additions & 25 deletions UniManage/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,12 @@
"""

from pathlib import Path
import datetime
import os

# 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/4.2/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'django-insecure-x2y&oi!8odi#qzfbd1rni1)$1z^d1m1c-zt($bc6igk$0y@8u@'

Expand All @@ -27,9 +25,7 @@

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
Expand All @@ -38,12 +34,41 @@
'django.contrib.messages',
'django.contrib.staticfiles',
'user',
'project',
'rest_framework.authtoken',
'rest_framework',
'djoser',
'drf_yasg',
'corsheaders',
]

REST_FRAMEWORK = {
'DEFAULT_PERMISSION_CLASSES': [
'rest_framework.permissions.IsAuthenticated',
],
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework_simplejwt.authentication.JWTAuthentication',
'rest_framework.authentication.SessionAuthentication',
'rest_framework.authentication.BasicAuthentication',
),
}

# Simple JWT settings
SIMPLE_JWT = {
'ALGORITHM': 'HS256',
'SIGNING_KEY': SECRET_KEY,
'VERIFYING_KEY': None,
'ACCESS_TOKEN_LIFETIME': datetime.timedelta(minutes=1),
'SLIDING_TOKEN_REFRESH_EXP_CLAIM': 'refresh_exp',
'SLIDING_TOKEN_REFRESH_LIFETIME': datetime.timedelta(days=5),
'ROTATE_REFRESH_TOKENS': False,
'BLACKLIST_AFTER_ROTATION': True,
'UPDATE_LAST_LOGIN': False,
'VALIDATE_TOKEN_CLAIMS': True,

ROOT_URLCONF = 'UniManage.urls'
}

ROOT_URLCONF = 'UniManage.urls'

TEMPLATES = [
{
Expand All @@ -63,21 +88,15 @@

WSGI_APPLICATION = 'UniManage.wsgi.application'


# Database
# https://docs.djangoproject.com/en/4.2/ref/settings/#databases

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}


# Password validation
# https://docs.djangoproject.com/en/4.2/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
Expand All @@ -93,40 +112,34 @@
},
]


# Internationalization
# https://docs.djangoproject.com/en/4.2/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/4.2/howto/static-files/

STATIC_URL = 'static/'

# Default primary key field type
# https://docs.djangoproject.com/en/4.2/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'corsheaders.middleware.CorsMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'user.middleware.CustomAuthenticationMiddleware',

]

AUTH_USER_MODEL = 'user.CustomUser'

MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')
AUTHENTICATION_BACKENDS = ['user.backends.EmailBackend']

CORS_ALLOW_ALL_ORIGINS = True
33 changes: 15 additions & 18 deletions UniManage/urls.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,25 @@
"""
URL configuration for UniManage project.

The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/4.2/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.urls import path, include
from user.admin_site import admin_site
from django.views.generic import RedirectView
from django.contrib import admin
from django.contrib.auth.decorators import login_required
from rest_framework import permissions
from drf_yasg.views import get_schema_view
from drf_yasg import openapi


schema_view = get_schema_view(
openapi.Info(
title="University Projects API",
default_version='v1',
description="API documentation for the School Projects app",
),
public=True,
permission_classes=(permissions.AllowAny,),
)

urlpatterns = [
path('admin/', admin_site.urls),
path('user/', include('user.urls')),
path('user/', include('user.urls')), # Assuming 'user.urls' has both regular and API urls differentiated with 'api/' prefix
path('project/', include('project.urls')), # Assuming 'project.urls' has both regular and API urls differentiated with 'api/' prefix
path('swagger/', schema_view.with_ui('swagger', cache_timeout=0), name='schema-swagger-ui'),
]
Empty file added core/__init__.py
Empty file.
3 changes: 3 additions & 0 deletions core/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.contrib import admin

# Register your models here.
6 changes: 6 additions & 0 deletions core/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class CoreConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'core'
Empty file added core/migrations/__init__.py
Empty file.
3 changes: 3 additions & 0 deletions core/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.db import models

# Create your models here.
Empty file added core/templates/core/home.html
Empty file.
3 changes: 3 additions & 0 deletions core/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.test import TestCase

# Create your tests here.
3 changes: 3 additions & 0 deletions core/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.shortcuts import render

# Create your views here.
Binary file added db.sqlite3
Abdul-Muqadim-Arbisoft marked this conversation as resolved.
Show resolved Hide resolved
Binary file not shown.
Empty file added project/__init__.py
Empty file.
39 changes: 39 additions & 0 deletions project/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# projects/admin.py

from django import forms
from django.forms.widgets import PasswordInput
from .models import Supervisor, Project
from user.models import CustomUser
from django.contrib import admin
from user.admin_site import admin_site



class SupervisorAdminForm(forms.ModelForm):
user_email = forms.EmailField()
password = forms.CharField(widget=PasswordInput(), required=False)

class Meta:
model = Supervisor
fields = '__all__'

def save(self, commit=True):
supervisor = super().save(commit=False)

# Logic to create or find CustomUser and set it to Supervisor
user, created = CustomUser.objects.get_or_create(email=self.cleaned_data['user_email'])
if created or self.cleaned_data.get('password'):
user.set_password(self.cleaned_data['password'])
user.save()
supervisor.user = user

if commit:
supervisor.save()

return supervisor

class SupervisorAdmin(admin.ModelAdmin):
form = SupervisorAdminForm

admin_site.register(Supervisor, SupervisorAdmin)
admin_site.register(Project)
6 changes: 6 additions & 0 deletions project/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class ProjectConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'project'
7 changes: 7 additions & 0 deletions project/forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from django import forms
from .models import Project

class ProjectForm(forms.ModelForm):
class Meta:
model = Project
fields = ['name', 'description', 'start_date', 'end_date', 'students'] # 'supervisor' field will be filled automatically
39 changes: 39 additions & 0 deletions project/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Generated by Django 4.2.3 on 2023-09-06 10:36

from django.db import migrations, models


class Migration(migrations.Migration):

initial = True

dependencies = [
]

operations = [
migrations.CreateModel(
name='Comment',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('text', models.TextField(help_text='Text of the comment.')),
('timestamp', models.DateTimeField(auto_now_add=True, help_text='The time when the comment was created.')),
],
),
migrations.CreateModel(
name='Project',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(help_text='Name of the project.', max_length=200)),
('description', models.TextField(help_text='Description or details of the project.')),
('start_date', models.DateField(help_text='The start date of the project.')),
('end_date', models.DateField(help_text='The expected end date of the project.')),
],
),
migrations.CreateModel(
name='Supervisor',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('expertise', models.CharField(help_text='Area of expertise of the supervisor.', max_length=200)),
],
),
]
43 changes: 43 additions & 0 deletions project/migrations/0002_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Generated by Django 4.2.3 on 2023-09-06 10:36

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

initial = True

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('project', '0001_initial'),
]

operations = [
migrations.AddField(
model_name='supervisor',
name='user',
field=models.OneToOneField(help_text='The associated user for this supervisor.', on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL),
),
migrations.AddField(
model_name='project',
name='students',
field=models.ManyToManyField(help_text='Students associated with this project.', related_name='student_projects', to=settings.AUTH_USER_MODEL),
),
migrations.AddField(
model_name='project',
name='supervisor',
field=models.ForeignKey(help_text='The supervisor overseeing this project.', on_delete=django.db.models.deletion.CASCADE, related_name='supervised_projects', to='project.supervisor'),
),
migrations.AddField(
model_name='comment',
name='project',
field=models.ForeignKey(help_text='The project this comment is associated with.', on_delete=django.db.models.deletion.CASCADE, to='project.project'),
),
migrations.AddField(
model_name='comment',
name='user',
field=models.ForeignKey(help_text='The user who made this comment.', on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL),
),
]
Empty file added project/migrations/__init__.py
Empty file.
48 changes: 48 additions & 0 deletions project/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
from django.db import models
from user.models import CustomUser


class Supervisor(models.Model):
"""
Represents a supervisor who oversees projects.
Has a one-to-one relation with a custom user and possesses expertise in a specific domain.
"""
user = models.OneToOneField(CustomUser,
on_delete=models.CASCADE,
help_text="The associated user for this supervisor."
)
expertise = models.CharField(max_length=200, help_text="Area of expertise of the supervisor.")


class Project(models.Model):
"""
Represents a project supervised by a supervisor and possibly worked on by multiple students.
"""
name = models.CharField(max_length=200, help_text="Name of the project.")
description = models.TextField(help_text="Description or details of the project.")
start_date = models.DateField(help_text="The start date of the project.")
end_date = models.DateField(help_text="The expected end date of the project.")
students = models.ManyToManyField(CustomUser,
related_name='student_projects',
help_text="Students associated with this project."
)
supervisor = models.ForeignKey(Supervisor,
on_delete=models.CASCADE,
related_name='supervised_projects',
help_text="The supervisor overseeing this project."
)


class Comment(models.Model):
"""
Represents a comment made by a user on a project.
Stores the associated project, the user who commented, the text of the comment, and the timestamp of when
it was made.
"""
project = models.ForeignKey(Project,
on_delete=models.CASCADE,
help_text="The project this comment is associated with."
)
user = models.ForeignKey(CustomUser, on_delete=models.CASCADE, help_text="The user who made this comment.")
text = models.TextField(help_text="Text of the comment.")
timestamp = models.DateTimeField(auto_now_add=True, help_text="The time when the comment was created.")
Loading