user ok
This commit is contained in:
parent
54d8cb9846
commit
635ad35c55
@ -1,3 +1,3 @@
|
|||||||
from django.contrib import admin
|
#from django.contrib import admin
|
||||||
|
|
||||||
# Register your models here.
|
# Register your models here.
|
||||||
|
47
softdesk/authentication/migrations/0001_initial.py
Normal file
47
softdesk/authentication/migrations/0001_initial.py
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
# Generated by Django 5.2.1 on 2025-05-23 03:58
|
||||||
|
|
||||||
|
import django.contrib.auth.models
|
||||||
|
import django.contrib.auth.validators
|
||||||
|
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='User',
|
||||||
|
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')),
|
||||||
|
('username', models.CharField(error_messages={'unique': 'A user with that username already exists.'}, help_text='Required. 150 characters or fewer. Letters, digits and @/./+/-/_ only.', max_length=150, unique=True, validators=[django.contrib.auth.validators.UnicodeUsernameValidator()], verbose_name='username')),
|
||||||
|
('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')),
|
||||||
|
('email', models.EmailField(blank=True, max_length=254, verbose_name='email address')),
|
||||||
|
('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')),
|
||||||
|
('can_be_contacted', models.BooleanField(default=False)),
|
||||||
|
('can_data_be_shared', models.BooleanField(default=False)),
|
||||||
|
('age', models.IntegerField()),
|
||||||
|
('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')),
|
||||||
|
],
|
||||||
|
options={
|
||||||
|
'verbose_name': 'user',
|
||||||
|
'verbose_name_plural': 'users',
|
||||||
|
'abstract': False,
|
||||||
|
},
|
||||||
|
managers=[
|
||||||
|
('objects', django.contrib.auth.models.UserManager()),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
]
|
18
softdesk/authentication/migrations/0002_alter_user_age.py
Normal file
18
softdesk/authentication/migrations/0002_alter_user_age.py
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
# Generated by Django 5.2.1 on 2025-05-23 04:00
|
||||||
|
|
||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('authentication', '0001_initial'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.AlterField(
|
||||||
|
model_name='user',
|
||||||
|
name='age',
|
||||||
|
field=models.IntegerField(null=True),
|
||||||
|
),
|
||||||
|
]
|
@ -5,10 +5,8 @@ from django.contrib.auth.models import AbstractUser, Group
|
|||||||
class User(AbstractUser):
|
class User(AbstractUser):
|
||||||
can_be_contacted = models.BooleanField(default=False)
|
can_be_contacted = models.BooleanField(default=False)
|
||||||
can_data_be_shared = models.BooleanField(default=False)
|
can_data_be_shared = models.BooleanField(default=False)
|
||||||
age = models.IntegerField()
|
age = models.IntegerField(null=True)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return self.username
|
||||||
|
|
||||||
|
66
softdesk/authentication/serializers.py
Normal file
66
softdesk/authentication/serializers.py
Normal file
@ -0,0 +1,66 @@
|
|||||||
|
from rest_framework.serializers import ModelSerializer, SerializerMethodField, ValidationError
|
||||||
|
from rest_framework import serializers
|
||||||
|
from support.models import Project, Issue, Comment, Contributor
|
||||||
|
from authentication.models import User
|
||||||
|
|
||||||
|
|
||||||
|
class UserSerializer(ModelSerializer):
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
model = User
|
||||||
|
fields = ['id', 'username', 'email', 'age', 'can_be_contacted', 'can_data_be_shared']
|
||||||
|
|
||||||
|
|
||||||
|
class UserUpdateSerializer(ModelSerializer):
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
model = User
|
||||||
|
fields = ['email', 'can_be_contacted', 'can_data_be_shared']
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
class UserRegisterSerializer(ModelSerializer):
|
||||||
|
password2 = serializers.CharField(style={'input-type': 'password'}, write_only=True)
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
model = User
|
||||||
|
fields = ['username', 'email', 'password', 'password2', 'age', 'can_be_contacted', 'can_data_be_shared']
|
||||||
|
extra_kwargs = {'password': {'write_only': True}}
|
||||||
|
|
||||||
|
|
||||||
|
def validate(self, data):
|
||||||
|
if data['password'] != data['password2']:
|
||||||
|
raise ValidationError("Passwords don't match.")
|
||||||
|
return data
|
||||||
|
|
||||||
|
def validate_age(self, value):
|
||||||
|
if value < 15:
|
||||||
|
raise ValidationError("You must be older than 15")
|
||||||
|
return value
|
||||||
|
|
||||||
|
def create(self, validated_data):
|
||||||
|
"""
|
||||||
|
Create and return a new `User` instance, given the validated data.
|
||||||
|
"""
|
||||||
|
#if self.validate(validated_data):
|
||||||
|
user = User.objects.create_user(
|
||||||
|
username=validated_data['username'],
|
||||||
|
email=validated_data['email'],
|
||||||
|
password=validated_data['password'],
|
||||||
|
age=validated_data['age'],
|
||||||
|
can_be_contacted=validated_data['can_be_contacted'],
|
||||||
|
can_data_be_shared=validated_data['can_data_be_shared'],
|
||||||
|
)
|
||||||
|
return user
|
||||||
|
|
||||||
|
|
||||||
|
class PasswordUpdateSerializer(ModelSerializer):
|
||||||
|
old_password = serializers.CharField(required=True)
|
||||||
|
new_password = serializers.CharField(required=True)
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
model = User
|
||||||
|
fields = ['old_password', 'new_password']
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -1,3 +1,87 @@
|
|||||||
|
from django.contrib.auth import update_session_auth_hash
|
||||||
from django.shortcuts import render
|
from django.shortcuts import render
|
||||||
|
from django.utils.autoreload import raise_last_exception
|
||||||
|
from rest_framework.views import APIView
|
||||||
|
from rest_framework.response import Response
|
||||||
|
from rest_framework.viewsets import ModelViewSet, ReadOnlyModelViewSet
|
||||||
|
from rest_framework import status
|
||||||
|
from rest_framework.permissions import IsAuthenticated
|
||||||
|
|
||||||
|
from authentication.models import User
|
||||||
|
from authentication.serializers import (UserSerializer,
|
||||||
|
UserUpdateSerializer,
|
||||||
|
UserRegisterSerializer,
|
||||||
|
PasswordUpdateSerializer)
|
||||||
|
|
||||||
|
|
||||||
|
class UserCreateView(APIView):
|
||||||
|
"""
|
||||||
|
Allow user registration for anyone
|
||||||
|
"""
|
||||||
|
|
||||||
|
#TODELETE : for testing purpose
|
||||||
|
def get(self, request, *args, **kwargs):
|
||||||
|
user = User.objects.all()
|
||||||
|
print(request.user)
|
||||||
|
serializer = UserSerializer(user, many=True)
|
||||||
|
print(serializer.data)
|
||||||
|
#if serializer.is_valid():
|
||||||
|
return Response(serializer.data)
|
||||||
|
#return Response("prout", status=status.HTTP_226_IM_USED)
|
||||||
|
|
||||||
|
def post(self, request):
|
||||||
|
serializer = UserRegisterSerializer(data=request.data)
|
||||||
|
if serializer.is_valid(raise_exception=True):
|
||||||
|
serializer.save()
|
||||||
|
response = {
|
||||||
|
"message": "User created successfully",
|
||||||
|
"data": serializer.data
|
||||||
|
}
|
||||||
|
return Response(data=response, status=status.HTTP_201_CREATED)
|
||||||
|
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
|
||||||
|
|
||||||
|
|
||||||
|
class PasswordUpdateView(APIView):
|
||||||
|
permission_classes = [IsAuthenticated]
|
||||||
|
|
||||||
|
def put(self, request):
|
||||||
|
user = request.user
|
||||||
|
serializer = PasswordUpdateSerializer(data=request.data)
|
||||||
|
if serializer.is_valid():
|
||||||
|
if not user.check_password(serializer.data.get("old_password")):
|
||||||
|
return Response({"old_password":"Wrong password"}, status=status.HTTP_400_BAD_REQUEST)
|
||||||
|
user.set_password(serializer.data.get('new_password'))
|
||||||
|
user.save()
|
||||||
|
update_session_auth_hash(request, user)
|
||||||
|
return Response(serializer.errors, status=status.HTTP_204_NO_CONTENT)
|
||||||
|
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
|
||||||
|
|
||||||
|
class UserView(APIView):
|
||||||
|
permission_classes = [IsAuthenticated]
|
||||||
|
|
||||||
|
def get(self, request, *args, **kwargs):
|
||||||
|
return Response(UserSerializer(request.user).data)
|
||||||
|
|
||||||
|
def put(self, request):
|
||||||
|
user = request.user
|
||||||
|
serializer = UserUpdateSerializer(user, data=request.data)
|
||||||
|
print(serializer.initial_data)
|
||||||
|
if serializer.is_valid():
|
||||||
|
serializer.save()
|
||||||
|
return Response("Data updated", status=status.HTTP_201_CREATED)
|
||||||
|
return Response("Error", status=status.HTTP_400_BAD_REQUEST)
|
||||||
|
|
||||||
|
def delete(self, request):
|
||||||
|
user = request.user
|
||||||
|
username = request.user.username
|
||||||
|
user.delete()
|
||||||
|
return Response(f"User {username} deleted.", status=status.HTTP_204_NO_CONTENT)
|
||||||
|
|
||||||
|
|
||||||
|
class UserRegistrationViewSet(ModelViewSet):
|
||||||
|
#serializer_class = UserRegistrationSerializer
|
||||||
|
|
||||||
|
def get_queryset(self):
|
||||||
|
return User.objects.get(self.request.user)
|
||||||
|
|
||||||
|
|
||||||
# Create your views here.
|
|
||||||
|
@ -11,6 +11,7 @@ https://docs.djangoproject.com/en/5.2/ref/settings/
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
from datetime import timedelta
|
||||||
|
|
||||||
# Build paths inside the project like this: BASE_DIR / 'subdir'.
|
# Build paths inside the project like this: BASE_DIR / 'subdir'.
|
||||||
BASE_DIR = Path(__file__).resolve().parent.parent
|
BASE_DIR = Path(__file__).resolve().parent.parent
|
||||||
@ -127,9 +128,11 @@ DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
|
|||||||
|
|
||||||
AUTH_USER_MODEL = 'authentication.User'
|
AUTH_USER_MODEL = 'authentication.User'
|
||||||
|
|
||||||
REST_FRAMERWORK = {
|
REST_FRAMEWORK = {
|
||||||
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.LimitOffsetPagination',
|
|
||||||
'PAGE_SIZE': 5,
|
|
||||||
'DEFAULT_AUTHENTICATION_CLASSES': ('rest_framework_simplejwt.authentication.JWTAuthentication',)
|
'DEFAULT_AUTHENTICATION_CLASSES': ('rest_framework_simplejwt.authentication.JWTAuthentication',)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
SIMPLE_JWT = {
|
||||||
|
'ACCESS_TOKEN_LIFETIME': timedelta(minutes=30),
|
||||||
|
'REFRESH_TOKEN_LIFETIME': timedelta(days=5),
|
||||||
|
}
|
||||||
|
@ -15,8 +15,24 @@ Including another URLconf
|
|||||||
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
|
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
|
||||||
"""
|
"""
|
||||||
from django.contrib import admin
|
from django.contrib import admin
|
||||||
from django.urls import path
|
from django.urls import path, include
|
||||||
|
from authentication.views import (UserRegistrationViewSet,
|
||||||
|
UserView, UserCreateView,
|
||||||
|
PasswordUpdateView)
|
||||||
|
from rest_framework import routers
|
||||||
|
from rest_framework_simplejwt.views import TokenObtainPairView, TokenRefreshView
|
||||||
|
|
||||||
|
|
||||||
|
router = routers.SimpleRouter()
|
||||||
|
#router.register('user', UserViewSet, basename='user')
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path('admin/', admin.site.urls),
|
path('admin/', admin.site.urls),
|
||||||
|
path('api-auth/', include('rest_framework.urls')),
|
||||||
|
path('api/', include(router.urls)),
|
||||||
|
path('api/user/', UserView.as_view(), name='user'),
|
||||||
|
path('api/user/create/', UserCreateView.as_view(), name='user_create'),
|
||||||
|
path('api/user/password-update/', PasswordUpdateView.as_view(), name='password_update'),
|
||||||
|
path('api/token/', TokenObtainPairView.as_view(), name='token_obtain_pair'),
|
||||||
|
path('api/token/refresh/', TokenRefreshView.as_view(), name='token_refresh'),
|
||||||
]
|
]
|
||||||
|
@ -1,7 +1,11 @@
|
|||||||
from django.contrib import admin
|
from django.contrib import admin
|
||||||
from support.models import Project, Issue, Comment, Contributor
|
from support.models import Project, Issue, Comment, Contributor
|
||||||
|
from authentication.models import User
|
||||||
|
|
||||||
|
class AdminUser:
|
||||||
|
pass
|
||||||
|
|
||||||
|
admin.site.register(User)
|
||||||
admin.site.register(Project)
|
admin.site.register(Project)
|
||||||
admin.site.register(Issue)
|
admin.site.register(Issue)
|
||||||
admin.site.register(Comment)
|
admin.site.register(Comment)
|
||||||
|
71
softdesk/support/migrations/0001_initial.py
Normal file
71
softdesk/support/migrations/0001_initial.py
Normal file
@ -0,0 +1,71 @@
|
|||||||
|
# Generated by Django 5.2.1 on 2025-05-23 03:58
|
||||||
|
|
||||||
|
import django.db.models.deletion
|
||||||
|
from django.conf import settings
|
||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
initial = True
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.CreateModel(
|
||||||
|
name='Contributor',
|
||||||
|
fields=[
|
||||||
|
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||||
|
('data', models.CharField(blank=True, max_length=255)),
|
||||||
|
('contributor', models.ForeignKey(on_delete=django.db.models.deletion.DO_NOTHING, to=settings.AUTH_USER_MODEL)),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
migrations.CreateModel(
|
||||||
|
name='Issue',
|
||||||
|
fields=[
|
||||||
|
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||||
|
('title', models.CharField(max_length=255, verbose_name='title')),
|
||||||
|
('date_created', models.DateTimeField(auto_now_add=True)),
|
||||||
|
('description', models.TextField()),
|
||||||
|
('status', models.CharField(max_length=15, verbose_name=[('ToDo', 'Todo'), ('InProgress', 'Inprogress'), ('Finished', 'Finished')])),
|
||||||
|
('priority', models.CharField(max_length=15, verbose_name=[('L', 'Low'), ('M', 'Medium'), ('H', 'High')])),
|
||||||
|
('tag', models.CharField(max_length=15, verbose_name=[('Bug', 'Bug'), ('Feature', 'Feature'), ('Task', 'Task')])),
|
||||||
|
('author', models.ForeignKey(on_delete=django.db.models.deletion.DO_NOTHING, to='support.contributor')),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
migrations.CreateModel(
|
||||||
|
name='Comment',
|
||||||
|
fields=[
|
||||||
|
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||||
|
('title', models.CharField(max_length=255)),
|
||||||
|
('date_created', models.DateTimeField(auto_now_add=True)),
|
||||||
|
('description', models.CharField(max_length=4000)),
|
||||||
|
('author', models.ForeignKey(on_delete=django.db.models.deletion.DO_NOTHING, to='support.contributor')),
|
||||||
|
('issue', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='support.issue')),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
migrations.CreateModel(
|
||||||
|
name='Project',
|
||||||
|
fields=[
|
||||||
|
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||||
|
('title', models.CharField(max_length=255)),
|
||||||
|
('date_created', models.DateTimeField(auto_now_add=True)),
|
||||||
|
('type', models.CharField(choices=[('BackEnd', 'Backend'), ('FrontEnd', 'Frontend'), ('iOS', 'Ios'), ('Android', 'Android')], max_length=10)),
|
||||||
|
('description', models.CharField(max_length=4000)),
|
||||||
|
('author', models.ForeignKey(on_delete=django.db.models.deletion.DO_NOTHING, related_name='author', to='support.contributor')),
|
||||||
|
('contributors', models.ManyToManyField(related_name='contribution', through='support.Contributor', to=settings.AUTH_USER_MODEL)),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
migrations.AddField(
|
||||||
|
model_name='issue',
|
||||||
|
name='project',
|
||||||
|
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to='support.project'),
|
||||||
|
),
|
||||||
|
migrations.AddField(
|
||||||
|
model_name='contributor',
|
||||||
|
name='project',
|
||||||
|
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='project', to='support.project'),
|
||||||
|
),
|
||||||
|
]
|
@ -50,7 +50,7 @@ class Issue(models.Model):
|
|||||||
title = models.CharField(max_length=255, verbose_name='title')
|
title = models.CharField(max_length=255, verbose_name='title')
|
||||||
date_created = models.DateTimeField(auto_now_add=True)
|
date_created = models.DateTimeField(auto_now_add=True)
|
||||||
description = models.TextField()
|
description = models.TextField()
|
||||||
project = models.ForeignKey(Project, null=True, on_delete=models.SET_NULL, blank=True)
|
project = models.ForeignKey(Project, null=True, on_delete=models.CASCADE, blank=True)
|
||||||
status = models.CharField(Status.choices, max_length=15)
|
status = models.CharField(Status.choices, max_length=15)
|
||||||
priority = models.CharField(Priority.choices, max_length=15)
|
priority = models.CharField(Priority.choices, max_length=15)
|
||||||
tag = models.CharField(Tag.choices, max_length=15)
|
tag = models.CharField(Tag.choices, max_length=15)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user