diff --git a/Dockerfile b/Dockerfile index 8f47e90..ceb8442 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,3 @@ -# Python version FROM python:3 # Set environment variables diff --git a/accounts/admin.py b/accounts/admin.py index 8c38f3f..33d94a9 100644 --- a/accounts/admin.py +++ b/accounts/admin.py @@ -1,3 +1,6 @@ + from django.contrib import admin +from django.contrib.auth.admin import UserAdmin +from .models import CustomUser -# Register your models here. +admin.site.register(CustomUser) \ No newline at end of file diff --git a/accounts/migrations/0001_initial.py b/accounts/migrations/0001_initial.py index 2fbf34b..5d3cac2 100644 --- a/accounts/migrations/0001_initial.py +++ b/accounts/migrations/0001_initial.py @@ -1,8 +1,8 @@ -# Generated by Django 4.0 on 2021-12-30 12:59 +# Generated by Django 4.0 on 2022-01-02 14:55 import django.contrib.auth.models +import django.contrib.auth.validators from django.db import migrations, models -import django.db.models.deletion import phone_field.models @@ -16,23 +16,37 @@ class Migration(migrations.Migration): operations = [ migrations.CreateModel( - name='Account', + name='CustomUser', fields=[ - ('user_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='auth.user')), - ('age', models.IntegerField()), + ('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.DateField(auto_now_add=True)), + ('roles', models.CharField(choices=[('Donater', 'Donater'), ('hospital', 'hospital'), ('Blood needer', 'Blood needer')], max_length=50, null=True)), ('blood_type', models.CharField(blank=True, max_length=10, null=True)), ('chronic_diseases', models.BooleanField(blank=True, default=False, null=True)), - ('image', models.ImageField(upload_to='image')), - ('data', models.DateField(blank=True, null=True)), - ('isAuthenticated', models.BooleanField(default=True)), + ('image', models.ImageField(default='../uploads/image/320.png', upload_to='image')), + ('date', models.DateField(blank=True, default='2022-01-01', null=True)), ('phone_number', phone_field.models.PhoneField(blank=True, help_text='Contact phone number', max_length=31)), + ('location', models.CharField(blank=True, max_length=50, null=True)), + ('donate', models.BooleanField(default=False)), + ('age', models.IntegerField(blank=True, null=True)), + ('website', models.CharField(blank=True, max_length=256, null=True)), + ('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, }, - bases=('auth.user',), managers=[ ('objects', django.contrib.auth.models.UserManager()), ], diff --git a/accounts/migrations/0002_account_group.py b/accounts/migrations/0002_account_group.py deleted file mode 100644 index e5868b9..0000000 --- a/accounts/migrations/0002_account_group.py +++ /dev/null @@ -1,21 +0,0 @@ -# Generated by Django 4.0 on 2021-12-30 13:14 - -from django.db import migrations, models -import django.db.models.deletion - - -class Migration(migrations.Migration): - - dependencies = [ - ('auth', '0012_alter_user_first_name_max_length'), - ('accounts', '0001_initial'), - ] - - operations = [ - migrations.AddField( - model_name='account', - name='group', - field=models.ForeignKey(default=1, on_delete=django.db.models.deletion.CASCADE, related_name='boes', to='auth.group'), - preserve_default=False, - ), - ] diff --git a/hospital/migrations/0003_alter_customuser_image.py b/accounts/migrations/0002_alter_customuser_phone_number.py similarity index 50% rename from hospital/migrations/0003_alter_customuser_image.py rename to accounts/migrations/0002_alter_customuser_phone_number.py index e6e0f89..3b43df3 100644 --- a/hospital/migrations/0003_alter_customuser_image.py +++ b/accounts/migrations/0002_alter_customuser_phone_number.py @@ -1,4 +1,4 @@ -# Generated by Django 4.0 on 2021-12-30 19:40 +# Generated by Django 4.0 on 2022-01-02 14:58 from django.db import migrations, models @@ -6,13 +6,13 @@ class Migration(migrations.Migration): dependencies = [ - ('hospital', '0002_customuser_image'), + ('accounts', '0001_initial'), ] operations = [ migrations.AlterField( model_name='customuser', - name='image', - field=models.ImageField(upload_to='image'), + name='phone_number', + field=models.CharField(blank=True, help_text='Contact phone number', max_length=20), ), ] diff --git a/accounts/migrations/0003_account_donate_account_location_alter_account_age.py b/accounts/migrations/0003_account_donate_account_location_alter_account_age.py deleted file mode 100644 index 8c92a2d..0000000 --- a/accounts/migrations/0003_account_donate_account_location_alter_account_age.py +++ /dev/null @@ -1,28 +0,0 @@ -# Generated by Django 4.0 on 2021-12-30 17:23 - -from django.db import migrations, models - - -class Migration(migrations.Migration): - - dependencies = [ - ('accounts', '0002_account_group'), - ] - - operations = [ - migrations.AddField( - model_name='account', - name='donate', - field=models.BooleanField(default=False), - ), - migrations.AddField( - model_name='account', - name='location', - field=models.CharField(blank=True, max_length=50, null=True), - ), - migrations.AlterField( - model_name='account', - name='age', - field=models.IntegerField(null=True), - ), - ] diff --git a/accounts/migrations/0003_remove_customuser_donate_alter_customuser_roles.py b/accounts/migrations/0003_remove_customuser_donate_alter_customuser_roles.py new file mode 100644 index 0000000..f67c3e7 --- /dev/null +++ b/accounts/migrations/0003_remove_customuser_donate_alter_customuser_roles.py @@ -0,0 +1,22 @@ +# Generated by Django 4.0 on 2022-01-02 15:43 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('accounts', '0002_alter_customuser_phone_number'), + ] + + operations = [ + migrations.RemoveField( + model_name='customuser', + name='donate', + ), + migrations.AlterField( + model_name='customuser', + name='roles', + field=models.CharField(blank=True, choices=[('Donater', 'Donater'), ('hospital', 'hospital'), ('Blood needer', 'Blood needer')], max_length=50, null=True), + ), + ] diff --git a/accounts/migrations/0004_remove_customuser_age_customuser_date_of_birth.py b/accounts/migrations/0004_remove_customuser_age_customuser_date_of_birth.py new file mode 100644 index 0000000..fff683a --- /dev/null +++ b/accounts/migrations/0004_remove_customuser_age_customuser_date_of_birth.py @@ -0,0 +1,22 @@ +# Generated by Django 4.0 on 2022-01-02 16:57 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('accounts', '0003_remove_customuser_donate_alter_customuser_roles'), + ] + + operations = [ + migrations.RemoveField( + model_name='customuser', + name='age', + ), + migrations.AddField( + model_name='customuser', + name='date_of_birth', + field=models.DateField(blank=True, default='2000-01-01', null=True), + ), + ] diff --git a/accounts/models.py b/accounts/models.py index e8475ea..f8d69ba 100644 --- a/accounts/models.py +++ b/accounts/models.py @@ -1,16 +1,27 @@ from django.db import models -from django.contrib.auth.models import User,Group -from phone_field import PhoneField -# Create your models here. -class Account(User): - age = models.IntegerField(null=True) +from django.contrib.auth.models import AbstractUser + +class CustomUser(AbstractUser): + ROLES = ( + + ('Donater', 'Donater'), + ('hospital', 'hospital'), + ("Blood needer","Blood needer") + + ) + + date_joined = models.DateField(auto_now_add=True) + roles = models.CharField(max_length=50, choices = ROLES, null=True,blank=True) blood_type = models.CharField(max_length=10 ,blank=True,null=True) chronic_diseases = models.BooleanField(default=False,blank=True,null=True) - image = models.ImageField(upload_to = 'image') - data = models.DateField(blank=True,null=True) - isAuthenticated = models.BooleanField(default=True) - phone_number = PhoneField(blank=True, help_text='Contact phone number') - group = models.ForeignKey(Group, related_name="boes", on_delete=models.CASCADE) + image = models.ImageField(upload_to = 'image',default='../uploads/image/320.png') + date = models.DateField(blank=True,null=True,default='2022-01-01') + phone_number = models.CharField(max_length=20,blank=True, help_text='Contact phone number') location = models.CharField(max_length=50,blank=True,null=True) - donate = models.BooleanField(default=False) + date_of_birth = models.DateField(blank=True,null=True,default='2000-01-01') + website = models.CharField(max_length=256 ,blank=True,null=True) + def __str__(self): + return self.username + + diff --git a/accounts/serializer.py b/accounts/serializer.py index 887c2fa..b2e4a46 100644 --- a/accounts/serializer.py +++ b/accounts/serializer.py @@ -1,37 +1,51 @@ from django.db.models import fields from rest_framework import serializers -from .models import Account +from .models import CustomUser from rest_framework_simplejwt.serializers import TokenObtainPairSerializer -class AddSerializer(serializers.ModelSerializer): +class AddDonaterSerializer(serializers.ModelSerializer): password = serializers.CharField(write_only=True) def create(self, validated_data): - user = Account.objects.create_user( + user = CustomUser.objects.create_user( username=validated_data['username'], password=validated_data['password'], - age=validated_data['age'], - group = validated_data['group'], + blood_type = validated_data['blood_type'], + first_name = validated_data['first_name'], + email = validated_data['email'], + roles = validated_data['roles'] + ) + + return user + + class Meta: + model = CustomUser + fields = ('username','password','first_name','email','blood_type','roles' + ) + +class EditDonaterSerializer(serializers.ModelSerializer): + def create(self, validated_data): + + user = CustomUser.objects.create_user( + username=validated_data['username'], + date_of_birth=validated_data['date_of_birth'], blood_type = validated_data['blood_type'], chronic_diseases = validated_data['chronic_diseases'], - data = validated_data['data'], + date = validated_data['date'], phone_number = validated_data['phone_number'], first_name = validated_data['first_name'], - last_name = validated_data['last_name'], email = validated_data['email'], location = validated_data['location'], - donate = validated_data['donate'], - image = validated_data['image'] + image = validated_data['image'], + ) return user - - class Meta: - model = Account - fields = ('id','first_name','last_name','username', - 'email','password','age','blood_type','phone_number','location', - 'chronic_diseases','data','donate','group','image' + class Meta: + model = CustomUser + fields = ('username','first_name','email','date_of_birth','image','location','phone_number' + ,'date','chronic_diseases','blood_type','date' ) class MyTokenObtainPairSerializer(TokenObtainPairSerializer): @@ -41,9 +55,59 @@ def validate(self, attrs): data['refresh'] = str(refresh) data['access'] = str(refresh.access_token) - # Add extra responses here data['id'] = self.user.id data['username'] = self.user.username + data['Role'] = self.user.roles return data + + +## hospital serializer + +class AddHospitalUser(serializers.ModelSerializer): + + password = serializers.CharField(write_only=True) + # website = serializers.CharField(write_only=True) + + def create(self, validated_data): + + user = CustomUser.objects.create_user( + first_name = validated_data['first_name'], + username=validated_data['username'], + password=validated_data['password'], + website = validated_data['website'], + email = validated_data['email'], + roles = validated_data['roles'] + ) + + return user + + class Meta: + model = CustomUser + # Tuple of serialized model fields (see link [2]) + fields = ( "first_name","username", "password", "website",'email','roles' ) + + +class EditHospitalUser(serializers.ModelSerializer): + + password = serializers.CharField(write_only=True) + # website = serializers.CharField(write_only=True) + + def create(self, validated_data): + + user = CustomUser.objects.create_user( + first_name = validated_data['first_name'], + username=validated_data['username'], + password=validated_data['password'], + website = validated_data['website'], + image = validated_data['image'], + email = validated_data['email'], + ) + + return user + + class Meta: + model = CustomUser + # Tuple of serialized model fields (see link [2]) + fields = ( "first_name","username", "password", "website",'email','image' ) \ No newline at end of file diff --git a/accounts/urls.py b/accounts/urls.py index 77507ce..18579b9 100644 --- a/accounts/urls.py +++ b/accounts/urls.py @@ -1,9 +1,14 @@ from django.urls import path -from .views import AddListView,DetailAddView,ListView,CustomObtainAuthToken +from .views import AddListView,DetailAddView,ListView,CustomObtainAuthToken,countview,CreateHospitalUserView,HospitalDetail,HospitalListView,counthospitalview urlpatterns = [ - path('signup',AddListView.as_view(),name= 'add_data'), - path('',DetailAddView.as_view(),name = 'detail_data'), - path("auth/", CustomObtainAuthToken.as_view()), - path('view',ListView.as_view(),name = 'list_data') + path('donater/signup/',AddListView.as_view(),name= 'add_data'), + path('donater//',DetailAddView.as_view(),name = 'detail_data'), + path("donater/auth/", CustomObtainAuthToken.as_view()), + path('donater/view/',ListView.as_view(),name = 'list_data'), + path('donater/count/',countview.as_view(),name = 'countview'), + path('hospital/signup/', CreateHospitalUserView.as_view(), name='signup'), + path("hospital//", HospitalDetail.as_view(), name="hospital_detail"), + path("hospital/view/", HospitalListView.as_view(), name="hospital_list"), + path('hospital/count/',counthospitalview.as_view(),name = 'counthospitalview'), ] \ No newline at end of file diff --git a/accounts/views.py b/accounts/views.py index a72aab0..9669731 100644 --- a/accounts/views.py +++ b/accounts/views.py @@ -1,29 +1,91 @@ from django.shortcuts import render from rest_framework import generics, permissions -from .serializer import AddSerializer,MyTokenObtainPairSerializer -from .models import Account +from .serializer import AddDonaterSerializer,MyTokenObtainPairSerializer,EditDonaterSerializer,EditHospitalUser,AddHospitalUser +from .models import CustomUser from rest_framework import permissions from django.shortcuts import get_object_or_404 from rest_framework_simplejwt.views import TokenObtainPairView -from .models import User + +from rest_framework.renderers import JSONRenderer +from rest_framework.response import Response +from rest_framework.views import APIView + + # Create your views here. class CustomObtainAuthToken(TokenObtainPairView): serializer_class = MyTokenObtainPairSerializer + + class AddListView(generics.CreateAPIView): - serializer_class = AddSerializer - queryset = Account.objects.all() + serializer_class = AddDonaterSerializer + queryset = CustomUser.objects.all() permission_classes = [permissions.AllowAny] class DetailAddView(generics.RetrieveUpdateDestroyAPIView): - serializer_class = AddSerializer - queryset = Account.objects.all() + serializer_class = EditDonaterSerializer + queryset = CustomUser.objects.all() + - def get_object(self): - UserName= self.kwargs.get("username") - return get_object_or_404(Account, username=UserName) + # def get_object(self): + # UserName= self.kwargs.get("username") + # return get_object_or_404(CustomUser, username=UserName) class ListView(generics.ListAPIView): - serializer_class = AddSerializer - queryset = Account.objects.all() - # permission_classes = [permissions.AllowAny] \ No newline at end of file + serializer_class = EditDonaterSerializer + queryset = CustomUser.objects.filter(roles="Donater") + + +class countview(APIView): + """ + A view that returns the count of active users. + """ + permission_classes = [permissions.AllowAny] + renderer_classes = [JSONRenderer] + + def get(self, request, format=None): + print(request.data) + user_count = CustomUser.objects.filter(roles="Donater").count() + content = {'user_count': user_count} + return Response(content) + + +## hospital + + +class CreateHospitalUserView(generics.CreateAPIView): + """ + A view that signup hospital. + """ + model = CustomUser + permission_classes = [ + permissions.AllowAny # Or anon users can't register + ] + serializer_class = AddHospitalUser + +class HospitalDetail(generics.RetrieveUpdateDestroyAPIView): + """ + A view that returns the detail of hospital + """ + queryset = CustomUser.objects.all() + serializer_class = EditHospitalUser + # def get_object(self): + # UserName= self.kwargs.get("username") + # return get_object_or_404(CustomUser, username=UserName) + +class HospitalListView(generics.ListAPIView): + serializer_class = EditHospitalUser + queryset = CustomUser.objects.filter(roles="hospital") + +class counthospitalview(APIView): + """ + A view that returns the count of active users. + """ + permission_classes = [permissions.AllowAny] + renderer_classes = [JSONRenderer] + + def get(self, request, format=None): + print(request.data) + user_count = CustomUser.objects.filter(roles="hospital").count() + content = {'user_count': user_count} + return Response(content) \ No newline at end of file diff --git a/api_tester.py b/api_tester.py index 782e8c6..6172c6b 100644 --- a/api_tester.py +++ b/api_tester.py @@ -1,145 +1,4 @@ -import fire -import requests +from hospital.models import customUser -API_HOST = "https://django34.herokuapp.com/" -RESOURCE_URI = "cookie_stands" -USERNAME = "yahialabib" -PASSWORD = "123123" - - -class ApiTester: - """CLI for testing API""" - - def __init__(self, host=API_HOST): - self.host = host - - def fetch_tokens(self): - """Fetches access and refresh JWT tokens from api - Returns: - tuple: access,refresh - """ - - token_url = f"{self.host}/api/token/" - - response = requests.post( - token_url, json={"username": USERNAME, "password": PASSWORD} - ) - - data = response.json() - - tokens = data["access"], data["refresh"] - - return tokens - - def get_all(self): - """get list of all resources from api - Usage: python api_tester.py get_all - Returns: JSON - """ - access_token = self.fetch_tokens()[0] - - url = f"{self.host}/api/all/" - - headers = { - "Authorization": f"Bearer {access_token}", - } - - response = requests.get(url, headers=headers) - - return response.json() - - def get_one(self, id): - """get 1 resource by id from api - Usage: - python api_tester.py get_one 1 - Returns: JSON - """ - access_token = self.fetch_tokens()[0] - - url = f"{self.host}/api/{id}" - - headers = { - "Authorization": f"Bearer {access_token}", - } - - response = requests.get(url, headers=headers) - - return response.json() - - # TODO adjust parameter names to match API - def create(self, name, description=None, owner=None): - """creates a resource in api - Usage: - python api_tester.py create / - --name=required --description=optional --owner=optional - Returns: JSON - """ - - access_token = self.fetch_tokens()[0] - - url = f"{self.host}/api/all" - - headers = { - "Authorization": f"Bearer {access_token}", - } - - data = { - "name": name, - "description": description, - "owner": owner, - } - - response = requests.post(url, json=data, headers=headers) - - return response.json() - - def update(self, id, name=None, description=None, owner=None): - """updates a resource in api - Usage: - python api_tester.py update 1 / - --name=optional --description=optional --owner=optional - Returns: JSON - """ - - access_token = self.fetch_tokens()[0] - - url = f"{self.host}/api/{id}/" - - headers = { - "Authorization": f"Bearer {access_token}", - } - - original = self.get_cookiestand(id) - - data = { - "name": name or original["name"], - "description": description or original["description"], - "owner": owner or original["owner"], - } - - response = requests.put(url, json=data, headers=headers) - - return response.text - - def delete(self, id): - """deletes a resource in api - Usage: - python api_tester.py delete 1 - Returns: Empty string if no error - """ - - access_token = self.fetch_tokens()[0] - - url = f"{self.host}/api/{id}/" - - headers = { - "Authorization": f"Bearer {access_token}", - } - - response = requests.delete(url, headers=headers) - - return response.text - - -if __name__ == "__main__": - fire.Fire(ApiTester) \ No newline at end of file +for p in customUser.objects.raw('SELECT * FROM hospital_customUser'): + print(p) \ No newline at end of file diff --git a/blood_donating/migrations/0001_initial.py b/blood_donating/migrations/0001_initial.py new file mode 100644 index 0000000..7b3c154 --- /dev/null +++ b/blood_donating/migrations/0001_initial.py @@ -0,0 +1,26 @@ +# Generated by Django 4.0 on 2022-01-02 22:22 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ('accounts', '0004_remove_customuser_age_customuser_date_of_birth'), + ] + + operations = [ + migrations.CreateModel( + name='Post', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('content', models.TextField()), + ('blood_type', models.CharField(max_length=10)), + ('time', models.DateTimeField(auto_now_add=True)), + ('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='accounts.customuser', to_field='username')), + ], + ), + ] diff --git a/blood_donating/models.py b/blood_donating/models.py index 71a8362..b38415a 100644 --- a/blood_donating/models.py +++ b/blood_donating/models.py @@ -1,3 +1,13 @@ from django.db import models +from django.db.models.deletion import CASCADE +from accounts.models import CustomUser + +class Post(models.Model): + user = models.ForeignKey(CustomUser,to_field='username',on_delete=CASCADE) + content = models.TextField() + blood_type = models.CharField(max_length=10) + time = models.DateTimeField(auto_now_add=True, blank=True) + def __str__(self) : + return self.content + -# Create your models here. diff --git a/blood_donating/serializer.py b/blood_donating/serializer.py new file mode 100644 index 0000000..40349f6 --- /dev/null +++ b/blood_donating/serializer.py @@ -0,0 +1,8 @@ +from .models import Post +from rest_framework import serializers + + +class PostSerializer(serializers.ModelSerializer): + class Meta: + model = Post + fields = ['user', 'content', 'blood_type', 'time'] \ No newline at end of file diff --git a/blood_donating/urls.py b/blood_donating/urls.py new file mode 100644 index 0000000..cc290c2 --- /dev/null +++ b/blood_donating/urls.py @@ -0,0 +1,7 @@ +from django.urls import path +from .views import PostViewSet , GetViewSet,UpdateDestroyViewSet +urlpatterns = [ + path('add/', PostViewSet.as_view(), name='add_post'), + path('show/',GetViewSet.as_view(), name= "show-post"), + path('update-delete/',UpdateDestroyViewSet.as_view(),name= "update-delete-post") +] \ No newline at end of file diff --git a/blood_donating/views.py b/blood_donating/views.py index 91ea44a..18ca43f 100644 --- a/blood_donating/views.py +++ b/blood_donating/views.py @@ -1,3 +1,27 @@ from django.shortcuts import render +from rest_framework.generics import CreateAPIView , ListAPIView,RetrieveUpdateDestroyAPIView +from .models import Post +from .serializer import PostSerializer +from rest_framework import permissions # Create your views here. + +class PostViewSet(CreateAPIView): + """ + API endpoint that allows users to be viewed or edited. + """ + serializer_class = PostSerializer + permission_classes = [permissions.IsAuthenticated] + +class GetViewSet(ListAPIView): + """ + API endpoint that allows users to be viewed or edited. + """ + serializer_class = PostSerializer + queryset =Post.objects.all().order_by("-time") + # permission_classes = [permissions.IsAuthenticated] + +class UpdateDestroyViewSet(RetrieveUpdateDestroyAPIView): + serializer_class = PostSerializer + queryset =Post.objects.all().order_by("-time") + # permission_classes = [permissions.IsAuthenticated] diff --git a/db.sqlite3 b/db.sqlite3 index 79e3f53..03cc1dc 100644 Binary files a/db.sqlite3 and b/db.sqlite3 differ diff --git a/docker-compose.yml b/docker-compose.yml index 19c83e0..cadc0db 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,10 +1,9 @@ -version: '4' - +version: '3.9' services: web: build: . - command: gunicorn cookie_project.wsgi:application --bind 0.0.0.0:8000 --workers 4 + command: python manage.py runserver 0.0.0.0:8000 volumes: - .:/code ports: - - 8000:8000 \ No newline at end of file + - "8000:8000" \ No newline at end of file diff --git a/hospital/__init__.py b/hospital/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/hospital/admin.py b/hospital/admin.py deleted file mode 100644 index 8c38f3f..0000000 --- a/hospital/admin.py +++ /dev/null @@ -1,3 +0,0 @@ -from django.contrib import admin - -# Register your models here. diff --git a/hospital/apps.py b/hospital/apps.py deleted file mode 100644 index 003288d..0000000 --- a/hospital/apps.py +++ /dev/null @@ -1,6 +0,0 @@ -from django.apps import AppConfig - - -class HospitalConfig(AppConfig): - default_auto_field = 'django.db.models.BigAutoField' - name = 'hospital' diff --git a/hospital/forms.py b/hospital/forms.py deleted file mode 100644 index 70028c0..0000000 --- a/hospital/forms.py +++ /dev/null @@ -1,43 +0,0 @@ -from django import forms -from django.contrib.auth.models import User -from django.contrib.auth.forms import UserCreationForm -from django.core.exceptions import ValidationError - - -class HospitalCreateForm(UserCreationForm): - username = forms.CharField(label='username', min_length=5, max_length=150) - email = forms.EmailField(label='email') - password1 = forms.CharField(label='password', widget=forms.PasswordInput) - password2 = forms.CharField(label='Confirm password', widget=forms.PasswordInput) - website = forms.CharField(label='website', max_length=256) - image = forms.ImageField() - - def username_clean(self): - username = self.cleaned_data['username'].lower() - new = User.objects.filter(username = username) - if new.count(): - raise ValidationError("User Already Exist") - return username - - def email_clean(self): - email = self.cleaned_data['email'].lower() - new = User.objects.filter(email=email) - if new.count(): - raise ValidationError(" Email Already Exist") - return email - - def clean_password2(self): - password1 = self.cleaned_data['password1'] - password2 = self.cleaned_data['password2'] - - if password1 and password2 and password1 != password2: - raise ValidationError("Password don't match") - return password2 - - def save(self, commit = True): - user = User.objects.create_user( - self.cleaned_data['username'], - self.cleaned_data['email'], - self.cleaned_data['password1'] - ) - return user \ No newline at end of file diff --git a/hospital/migrations/0001_initial.py b/hospital/migrations/0001_initial.py deleted file mode 100644 index f67d21c..0000000 --- a/hospital/migrations/0001_initial.py +++ /dev/null @@ -1,33 +0,0 @@ -# Generated by Django 4.0 on 2021-12-30 11:36 - -import django.contrib.auth.models -from django.db import migrations, models -import django.db.models.deletion - - -class Migration(migrations.Migration): - - initial = True - - dependencies = [ - ('auth', '0012_alter_user_first_name_max_length'), - ] - - operations = [ - migrations.CreateModel( - name='customUser', - fields=[ - ('user_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='auth.user')), - ('website', models.CharField(max_length=256)), - ], - options={ - 'verbose_name': 'user', - 'verbose_name_plural': 'users', - 'abstract': False, - }, - bases=('auth.user',), - managers=[ - ('objects', django.contrib.auth.models.UserManager()), - ], - ), - ] diff --git a/hospital/migrations/0002_customuser_image.py b/hospital/migrations/0002_customuser_image.py deleted file mode 100644 index 4aa1acd..0000000 --- a/hospital/migrations/0002_customuser_image.py +++ /dev/null @@ -1,18 +0,0 @@ -# Generated by Django 4.0 on 2021-12-30 11:59 - -from django.db import migrations, models - - -class Migration(migrations.Migration): - - dependencies = [ - ('hospital', '0001_initial'), - ] - - operations = [ - migrations.AddField( - model_name='customuser', - name='image', - field=models.ImageField(default='default.png', upload_to=''), - ), - ] diff --git a/hospital/migrations/0004_customuser_group.py b/hospital/migrations/0004_customuser_group.py deleted file mode 100644 index 35ef994..0000000 --- a/hospital/migrations/0004_customuser_group.py +++ /dev/null @@ -1,21 +0,0 @@ -# Generated by Django 4.0 on 2021-12-30 19:41 - -from django.db import migrations, models -import django.db.models.deletion - - -class Migration(migrations.Migration): - - dependencies = [ - ('auth', '0012_alter_user_first_name_max_length'), - ('hospital', '0003_alter_customuser_image'), - ] - - operations = [ - migrations.AddField( - model_name='customuser', - name='group', - field=models.ForeignKey(default=1, on_delete=django.db.models.deletion.CASCADE, related_name='boes2', to='auth.group'), - preserve_default=False, - ), - ] diff --git a/hospital/migrations/__init__.py b/hospital/migrations/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/hospital/migrations/__pycache__/0001_initial.cpython-39.pyc b/hospital/migrations/__pycache__/0001_initial.cpython-39.pyc deleted file mode 100644 index 961711a..0000000 Binary files a/hospital/migrations/__pycache__/0001_initial.cpython-39.pyc and /dev/null differ diff --git a/hospital/migrations/__pycache__/0002_customuser_image.cpython-39.pyc b/hospital/migrations/__pycache__/0002_customuser_image.cpython-39.pyc deleted file mode 100644 index 2892c46..0000000 Binary files a/hospital/migrations/__pycache__/0002_customuser_image.cpython-39.pyc and /dev/null differ diff --git a/hospital/migrations/__pycache__/__init__.cpython-39.pyc b/hospital/migrations/__pycache__/__init__.cpython-39.pyc deleted file mode 100644 index b52345e..0000000 Binary files a/hospital/migrations/__pycache__/__init__.cpython-39.pyc and /dev/null differ diff --git a/hospital/models.py b/hospital/models.py deleted file mode 100644 index d3f434b..0000000 --- a/hospital/models.py +++ /dev/null @@ -1,8 +0,0 @@ -from django.db import models -from django.contrib.auth.models import User,Group -# Create your models here. - -class customUser (User): - website = models.CharField(max_length=256) - group = models.ForeignKey(Group, related_name="boes2", on_delete=models.CASCADE) - image = models.ImageField(upload_to = 'image') \ No newline at end of file diff --git a/hospital/serializers.py b/hospital/serializers.py deleted file mode 100644 index 5081dbc..0000000 --- a/hospital/serializers.py +++ /dev/null @@ -1,41 +0,0 @@ -from django.contrib.auth.models import Group -from rest_framework import serializers -from django.contrib.auth import get_user_model # If used custom user model -from .models import customUser -from rest_framework_simplejwt.serializers import TokenObtainPairSerializer - -class UserSerializer(serializers.ModelSerializer): - - password = serializers.CharField(write_only=True) - # website = serializers.CharField(write_only=True) - - def create(self, validated_data): - - user = customUser.objects.create_user( - username=validated_data['username'], - password=validated_data['password'], - website = validated_data['website'], - image = validated_data['image'], - group = validated_data['group'], - email = validated_data['email'], - ) - - return user - - class Meta: - model = customUser - # Tuple of serialized model fields (see link [2]) - fields = ( "id", "username", "password", "website", "image",'group','email' ) - - -class MyTokenObtainPairSerializer(TokenObtainPairSerializer): - def validate(self, attrs): - data = super().validate(attrs) - refresh = self.get_token(self.user) - data['refresh'] = str(refresh) - data['access'] = str(refresh.access_token) - - # Add extra responses here - data['id'] = self.user.id - data['username'] = self.user.username - return data \ No newline at end of file diff --git a/hospital/tests.py b/hospital/tests.py deleted file mode 100644 index 7ce503c..0000000 --- a/hospital/tests.py +++ /dev/null @@ -1,3 +0,0 @@ -from django.test import TestCase - -# Create your tests here. diff --git a/hospital/urls.py b/hospital/urls.py deleted file mode 100644 index a388a64..0000000 --- a/hospital/urls.py +++ /dev/null @@ -1,10 +0,0 @@ -# accounts/urls.py -from django.urls import path -from .views import CreateUserView, HospitalDetail -from .views import CustomObtainAuthToken - -urlpatterns = [ - path('signup', CreateUserView.as_view(), name='signup'), - path("", HospitalDetail.as_view(), name="hospital_detail"), - path("auth/", CustomObtainAuthToken.as_view()) -] \ No newline at end of file diff --git a/hospital/views.py b/hospital/views.py deleted file mode 100644 index f9179f8..0000000 --- a/hospital/views.py +++ /dev/null @@ -1,30 +0,0 @@ -from rest_framework import permissions -from rest_framework.generics import CreateAPIView, RetrieveAPIView -from django.contrib.auth import get_user_model # If used custom user model -from .models import customUser -from .serializers import UserSerializer -from rest_framework_simplejwt.views import TokenObtainPairView -from .serializers import MyTokenObtainPairSerializer -from django.shortcuts import get_object_or_404 - - - -class CustomObtainAuthToken(TokenObtainPairView): - serializer_class = MyTokenObtainPairSerializer - -class CreateUserView(CreateAPIView): - model = get_user_model() - permission_classes = [ - permissions.AllowAny # Or anon users can't register - ] - serializer_class = UserSerializer - -class HospitalDetail(RetrieveAPIView): - queryset = customUser.objects.all() - serializer_class = UserSerializer - permission_classes = [ - permissions.AllowAny # Or anon users can't register - ] - def get_object(self): - UserName= self.kwargs.get("username") - return get_object_or_404(customUser, username=UserName) \ No newline at end of file diff --git a/lifeshare/settings.py b/lifeshare/settings.py index 941c2f8..c81f563 100644 --- a/lifeshare/settings.py +++ b/lifeshare/settings.py @@ -41,7 +41,7 @@ ALLOWED_HOSTS = tuple(env.list("ALLOWED_HOSTS")) # Application definition - +CORS_ORIGIN_ALLOW_ALL=True INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', @@ -57,7 +57,6 @@ ## local 'blood_donating', 'accounts', - 'hospital' ] MIDDLEWARE = [ @@ -156,10 +155,10 @@ DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' - +AUTH_USER_MODEL = 'accounts.CustomUser' REST_FRAMEWORK = { 'DEFAULT_PERMISSION_CLASSES': [ - 'rest_framework.permissions.IsAuthenticated', + 'rest_framework.permissions.AllowAny', ], 'DEFAULT_AUTHENTICATION_CLASSES': [ 'rest_framework_simplejwt.authentication.JWTAuthentication', diff --git a/lifeshare/urls.py b/lifeshare/urls.py index 965af18..824dd6b 100644 --- a/lifeshare/urls.py +++ b/lifeshare/urls.py @@ -23,9 +23,9 @@ urlpatterns = [ path('admin/', admin.site.urls), - path('account/',include('accounts.urls')), + path('',include('accounts.urls')), path('api/token/', jwt_views.TokenObtainPairView.as_view(), name='token_obtain_pair'), path('api/token/refresh/', jwt_views.TokenRefreshView.as_view(), name='token_refresh'), - path('hospital/', include('hospital.urls')), + path('post/',include('blood_donating.urls')) ] diff --git a/hospital/default.png b/uploads/image/320.png similarity index 100% rename from hospital/default.png rename to uploads/image/320.png diff --git a/uploads/image/code39.jpg b/uploads/image/code39.jpg new file mode 100644 index 0000000..d661f27 Binary files /dev/null and b/uploads/image/code39.jpg differ diff --git a/uploads/image/code39_Gd4Ghe8.jpg b/uploads/image/code39_Gd4Ghe8.jpg new file mode 100644 index 0000000..d661f27 Binary files /dev/null and b/uploads/image/code39_Gd4Ghe8.jpg differ diff --git a/uploads/image/code39_hZiG2Of.jpg b/uploads/image/code39_hZiG2Of.jpg new file mode 100644 index 0000000..d661f27 Binary files /dev/null and b/uploads/image/code39_hZiG2Of.jpg differ diff --git a/uploads/image/pp1.jpg b/uploads/image/pp1.jpg new file mode 100644 index 0000000..710a603 Binary files /dev/null and b/uploads/image/pp1.jpg differ diff --git a/uploads/image/pp1_dR7VZ8y.jpg b/uploads/image/pp1_dR7VZ8y.jpg new file mode 100644 index 0000000..710a603 Binary files /dev/null and b/uploads/image/pp1_dR7VZ8y.jpg differ diff --git a/uploads/image/whiteboardyahia.jpg b/uploads/image/whiteboardyahia.jpg new file mode 100644 index 0000000..4e12d0d Binary files /dev/null and b/uploads/image/whiteboardyahia.jpg differ diff --git a/uploads/image/yaseen_112jspG.PNG b/uploads/image/yaseen_112jspG.PNG new file mode 100644 index 0000000..8828bf6 Binary files /dev/null and b/uploads/image/yaseen_112jspG.PNG differ diff --git a/uploads/image/yaseen_DLfciZC.PNG b/uploads/image/yaseen_DLfciZC.PNG new file mode 100644 index 0000000..8828bf6 Binary files /dev/null and b/uploads/image/yaseen_DLfciZC.PNG differ