suhaib0edu commited on
Commit
b3ddf7d
·
verified ·
1 Parent(s): 33b0606

Upload 14 files

Browse files
accounts/admin.py ADDED
File without changes
accounts/apps.py ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ from django.apps import AppConfig
2
+
3
+ class AccountsConfig(AppConfig):
4
+ default_auto_field = 'django.db.models.BigAutoField'
5
+ name = 'accounts'
accounts/models.py ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from django.contrib.auth.models import AbstractUser
2
+ from django.db import models
3
+
4
+ class CustomUser(AbstractUser):
5
+ email = models.EmailField(unique=True)
6
+ username = models.CharField(max_length=150, unique=True)
7
+
8
+ USERNAME_FIELD = 'email'
9
+ REQUIRED_FIELDS = ['username']
10
+
11
+ def __str__(self):
12
+ return self.username
accounts/serializers.py ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from rest_framework import serializers
2
+ from .models import CustomUser
3
+ from django.contrib.auth.hashers import make_password
4
+
5
+ class UserRegistrationSerializer(serializers.ModelSerializer):
6
+ password = serializers.CharField(write_only=True)
7
+
8
+ class Meta:
9
+ model = CustomUser
10
+ fields = ('email', 'username', 'password')
11
+
12
+ def create(self, validated_data):
13
+ validated_data['password'] = make_password(validated_data.get('password'))
14
+ return super(UserRegistrationSerializer, self).create(validated_data)
15
+
16
+
17
+ class UserLoginSerializer(serializers.Serializer):
18
+ email = serializers.EmailField()
19
+ password = serializers.CharField()
20
+
21
+
22
+ class UserUpdateSerializer(serializers.ModelSerializer):
23
+ class Meta:
24
+ model = CustomUser
25
+ fields = ('username', 'first_name', 'last_name')
26
+
accounts/tests.py ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ from django.test import TestCase
2
+
3
+ # Create your tests here.
accounts/urls.py ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ from django.urls import path
2
+ from .views import UserRegistrationView, UserLoginView, UserLogoutView, UserDeleteView, UserUpdateView
3
+
4
+ urlpatterns = [
5
+ path('register/', UserRegistrationView.as_view(), name='register'),
6
+ path('login/', UserLoginView.as_view(), name='login'),
7
+ path('logout/', UserLogoutView.as_view(), name='logout'),
8
+ path('delete/', UserDeleteView.as_view(), name='delete'),
9
+ path('update/', UserUpdateView.as_view(), name='update'),
10
+ ]
accounts/views.py ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from rest_framework import generics, status, views, permissions
2
+ from rest_framework.response import Response
3
+ from .serializers import UserRegistrationSerializer, UserLoginSerializer, UserUpdateSerializer
4
+ from django.contrib.auth import authenticate, login, logout
5
+ from rest_framework.authtoken.models import Token
6
+ from .models import CustomUser
7
+
8
+ class UserRegistrationView(generics.CreateAPIView):
9
+ serializer_class = UserRegistrationSerializer
10
+
11
+ def create(self, request, *args, **kwargs):
12
+ serializer = self.get_serializer(data=request.data)
13
+ serializer.is_valid(raise_exception=True)
14
+ user = serializer.save()
15
+ token, created = Token.objects.get_or_create(user=user)
16
+ headers = self.get_success_headers(serializer.data)
17
+ return Response({'token': token.key, 'user': serializer.data}, status=status.HTTP_201_CREATED, headers=headers)
18
+
19
+ class UserLoginView(generics.GenericAPIView):
20
+ serializer_class = UserLoginSerializer
21
+
22
+ def post(self, request):
23
+ serializer = self.get_serializer(data=request.data)
24
+ serializer.is_valid(raise_exception=True)
25
+ email = serializer.validated_data['email']
26
+ password = serializer.validated_data['password']
27
+ user = authenticate(request, email=email, password=password)
28
+
29
+ if user:
30
+ login(request, user)
31
+ token, created = Token.objects.get_or_create(user=user)
32
+ return Response({'token': token.key, 'message': 'Login successful'}, status=status.HTTP_200_OK)
33
+ return Response({'message': 'Invalid credentials'}, status=status.HTTP_401_UNAUTHORIZED)
34
+
35
+ class UserLogoutView(views.APIView):
36
+ permission_classes = [permissions.IsAuthenticated]
37
+
38
+ def post(self, request):
39
+ request.user.auth_token.delete()
40
+ logout(request)
41
+ return Response({'message': 'Logout successful'}, status=status.HTTP_200_OK)
42
+
43
+ class UserDeleteView(generics.DestroyAPIView):
44
+ permission_classes = [permissions.IsAuthenticated]
45
+ queryset = CustomUser.objects.all()
46
+
47
+ def get_object(self):
48
+ return self.request.user
49
+
50
+ def destroy(self, request, *args, **kwargs):
51
+ instance = self.get_object()
52
+ instance.delete()
53
+ return Response({'message': 'Account deleted successfully'}, status=status.HTTP_204_NO_CONTENT)
54
+
55
+
56
+ class UserUpdateView(generics.UpdateAPIView):
57
+ permission_classes = [permissions.IsAuthenticated]
58
+ serializer_class = UserUpdateSerializer
59
+ queryset = CustomUser.objects.all()
60
+
61
+ def get_object(self):
62
+ return self.request.user
il_env_backend_api/__init__.py ADDED
File without changes
il_env_backend_api/asgi.py ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ ASGI config for il_env_backend_api project.
3
+
4
+ It exposes the ASGI callable as a module-level variable named ``application``.
5
+
6
+ For more information on this file, see
7
+ https://docs.djangoproject.com/en/5.1/howto/deployment/asgi/
8
+ """
9
+
10
+ import os
11
+
12
+ from django.core.asgi import get_asgi_application
13
+
14
+ os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'il_env_backend_api.settings')
15
+
16
+ application = get_asgi_application()
il_env_backend_api/settings.py ADDED
@@ -0,0 +1,131 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Django settings for il_env_backend_api project.
3
+
4
+ Generated by 'django-admin startproject' using Django 5.1.4.
5
+
6
+ For more information on this file, see
7
+ https://docs.djangoproject.com/en/5.1/topics/settings/
8
+
9
+ For the full list of settings and their values, see
10
+ https://docs.djangoproject.com/en/5.1/ref/settings/
11
+ """
12
+
13
+ from pathlib import Path
14
+
15
+ # Build paths inside the project like this: BASE_DIR / 'subdir'.
16
+ BASE_DIR = Path(__file__).resolve().parent.parent
17
+
18
+
19
+ # Quick-start development settings - unsuitable for production
20
+ # See https://docs.djangoproject.com/en/5.1/howto/deployment/checklist/
21
+
22
+ # SECURITY WARNING: keep the secret key used in production secret!
23
+ SECRET_KEY = 'django-insecure-!cwt@%-b#(+q903^e!fdv!kkn5#l)r$r9orh)5p_fob*^6t-!b'
24
+
25
+ # SECURITY WARNING: don't run with debug turned on in production!
26
+ DEBUG = True
27
+
28
+ ALLOWED_HOSTS = ['0.0.0.0']
29
+
30
+
31
+ # Application definition
32
+
33
+ INSTALLED_APPS = [
34
+ 'django.contrib.admin',
35
+ 'django.contrib.auth',
36
+ 'django.contrib.contenttypes',
37
+ 'django.contrib.sessions',
38
+ 'django.contrib.messages',
39
+ 'django.contrib.staticfiles',
40
+
41
+ 'rest_framework',
42
+ 'rest_framework.authtoken' ,
43
+ 'django_filters',
44
+ 'accounts',
45
+ ]
46
+
47
+ MIDDLEWARE = [
48
+ 'django.middleware.security.SecurityMiddleware',
49
+ 'django.contrib.sessions.middleware.SessionMiddleware',
50
+ 'django.middleware.common.CommonMiddleware',
51
+ 'django.middleware.csrf.CsrfViewMiddleware',
52
+ 'django.contrib.auth.middleware.AuthenticationMiddleware',
53
+ 'django.contrib.messages.middleware.MessageMiddleware',
54
+ 'django.middleware.clickjacking.XFrameOptionsMiddleware',
55
+ ]
56
+
57
+ ROOT_URLCONF = 'il_env_backend_api.urls'
58
+
59
+ TEMPLATES = [
60
+ {
61
+ 'BACKEND': 'django.template.backends.django.DjangoTemplates',
62
+ 'DIRS': [],
63
+ 'APP_DIRS': True,
64
+ 'OPTIONS': {
65
+ 'context_processors': [
66
+ 'django.template.context_processors.debug',
67
+ 'django.template.context_processors.request',
68
+ 'django.contrib.auth.context_processors.auth',
69
+ 'django.contrib.messages.context_processors.messages',
70
+ ],
71
+ },
72
+ },
73
+ ]
74
+
75
+ WSGI_APPLICATION = 'il_env_backend_api.wsgi.application'
76
+
77
+
78
+ # Database
79
+ # https://docs.djangoproject.com/en/5.1/ref/settings/#databases
80
+
81
+ DATABASES = {
82
+ 'default': {
83
+ 'ENGINE': 'django.db.backends.sqlite3',
84
+ 'NAME': BASE_DIR / 'db.sqlite3',
85
+ }
86
+ }
87
+
88
+
89
+ # Password validation
90
+ # https://docs.djangoproject.com/en/5.1/ref/settings/#auth-password-validators
91
+
92
+ AUTH_PASSWORD_VALIDATORS = [
93
+ {
94
+ 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
95
+ },
96
+ {
97
+ 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
98
+ },
99
+ {
100
+ 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
101
+ },
102
+ {
103
+ 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
104
+ },
105
+ ]
106
+
107
+
108
+ # Internationalization
109
+ # https://docs.djangoproject.com/en/5.1/topics/i18n/
110
+
111
+ LANGUAGE_CODE = 'en-us'
112
+
113
+ TIME_ZONE = 'UTC'
114
+
115
+ USE_I18N = True
116
+
117
+ USE_TZ = True
118
+
119
+
120
+ # Static files (CSS, JavaScript, Images)
121
+ # https://docs.djangoproject.com/en/5.1/howto/static-files/
122
+
123
+ STATIC_URL = 'static/'
124
+
125
+ # Default primary key field type
126
+ # https://docs.djangoproject.com/en/5.1/ref/settings/#default-auto-field
127
+
128
+ DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
129
+
130
+
131
+ AUTH_USER_MODEL = 'accounts.CustomUser'
il_env_backend_api/urls.py ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ from django.contrib import admin
2
+ from django.urls import path, include
3
+
4
+ urlpatterns = [
5
+ path('admin/', admin.site.urls),
6
+ path('api/users/', include('accounts.urls')), # تضمين مسارات تطبيق الحسابات
7
+ ]
il_env_backend_api/wsgi.py ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ WSGI config for il_env_backend_api project.
3
+
4
+ It exposes the WSGI callable as a module-level variable named ``application``.
5
+
6
+ For more information on this file, see
7
+ https://docs.djangoproject.com/en/5.1/howto/deployment/wsgi/
8
+ """
9
+
10
+ import os
11
+
12
+ from django.core.wsgi import get_wsgi_application
13
+
14
+ os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'il_env_backend_api.settings')
15
+
16
+ application = get_wsgi_application()
manage.py ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python
2
+ """Django's command-line utility for administrative tasks."""
3
+ import os
4
+ import sys
5
+
6
+
7
+ def main():
8
+ """Run administrative tasks."""
9
+ os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'il_env_backend_api.settings')
10
+ try:
11
+ from django.core.management import execute_from_command_line
12
+ except ImportError as exc:
13
+ raise ImportError(
14
+ "Couldn't import Django. Are you sure it's installed and "
15
+ "available on your PYTHONPATH environment variable? Did you "
16
+ "forget to activate a virtual environment?"
17
+ ) from exc
18
+ execute_from_command_line(sys.argv)
19
+
20
+
21
+ if __name__ == '__main__':
22
+ main()
requirements.txt ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ aniso8601==9.0.1
2
+ asgiref==3.8.1
3
+ Django==5.1.4
4
+ django-filter==24.3
5
+ django-graphql-jwt==0.3.4
6
+ djangorestframework==3.15.2
7
+ Faker==18.2.0
8
+ graphene==3.2.1
9
+ graphene-django==3.0.0
10
+ graphql-core==3.2.3
11
+ graphql-relay==3.2.0
12
+ Markdown==3.7
13
+ promise==2.3
14
+ PyJWT==2.6.0
15
+ python-dateutil==2.8.2
16
+ pytz==2022.7.1
17
+ six==1.16.0
18
+ sqlparse==0.4.3
19
+ text-unidecode==1.3
20
+ typing_extensions==4.12.2