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

Implement features for sprint 1 and 2, Fixes #4 and #5 #9

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions backend/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.env
*.pyc
db.sqlite3
media
Empty file added backend/accounts/__init__.py
Empty file.
3 changes: 3 additions & 0 deletions backend/accounts/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 backend/accounts/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class AccountsConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'accounts'
44 changes: 44 additions & 0 deletions backend/accounts/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Generated by Django 4.2.2 on 2023-06-15 20:01

import django.contrib.auth.models
import django.contrib.auth.validators
from django.db import migrations, models
import django.utils.timezone


class Migration(migrations.Migration):

initial = True

dependencies = [
('auth', '0012_alter_user_first_name_max_length'),
]

operations = [
migrations.CreateModel(
name='MyUser',
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')),
('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 changes: 18 additions & 0 deletions backend/accounts/migrations/0002_myuser_profile_picture.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.2.2 on 2023-06-15 22:26

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('accounts', '0001_initial'),
]

operations = [
migrations.AddField(
model_name='myuser',
name='profile_picture',
field=models.ImageField(blank=True, null=True, upload_to='profile_img/'),
),
]
Empty file.
7 changes: 7 additions & 0 deletions backend/accounts/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from django.db import models
from django.contrib.auth.models import AbstractUser

# Create your models here.

class MyUser(AbstractUser):
profile_picture = models.ImageField(upload_to='profile_img/',null=True,blank=True)
21 changes: 21 additions & 0 deletions backend/accounts/serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from .models import MyUser
from rest_framework import serializers
from django.contrib.auth import get_user_model

class LoginSerializer(serializers.ModelSerializer):
password = serializers.CharField(write_only=True)
class Meta:
model = get_user_model()
fields = ['id','username','password']

class UserSerializer(serializers.ModelSerializer):
password = serializers.CharField(write_only=True)
class Meta:
model = MyUser
fields = ['id','username','email','first_name','last_name','password']

class ProfileSerializer(serializers.ModelSerializer):
profile_picture = serializers.ImageField(use_url=True,allow_null=True,required=False)
class Meta:
model = MyUser
fields = '__all__'
3 changes: 3 additions & 0 deletions backend/accounts/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.
8 changes: 8 additions & 0 deletions backend/accounts/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from django.urls import path
from .views import *

urlpatterns = [
path('signup/',SignUpView.as_view(),name='signup'),
path('login/',LoginView.as_view(),name='login'),
path('profile/',ProfileView.as_view(),name='profile'),
]
84 changes: 84 additions & 0 deletions backend/accounts/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
from rest_framework import generics,status
from rest_framework.response import Response
from django.contrib.auth import authenticate
from rest_framework.permissions import *
from rest_framework.views import APIView
from rest_framework.authtoken.models import Token
from rest_framework.authentication import *
from rest_framework.decorators import api_view
from rest_framework.reverse import reverse
from drf_spectacular.utils import extend_schema,OpenApiResponse
# import secrets



#
from .models import MyUser
from .serializers import UserSerializer,ProfileSerializer,LoginSerializer



# Create your views here.

@api_view(['GET'])
def api_root(request,format=None):
"""
Api Documentation URLs
"""
return Response(
{
"Swagger UI":reverse('swagger-ui',request=request,format=format),
"Redoc UI":reverse('redoc',request=request,format=format)
}

)

class SignUpView(generics.CreateAPIView):
queryset = MyUser.objects.all()
serializer_class = UserSerializer
permission_classes = [AllowAny]

def perform_create(self, serializer):
user = serializer.save()
user.set_password(serializer.validated_data["password"])
user.save()

class LoginView(APIView):
serializer_class = LoginSerializer
permission_classes = [AllowAny]
@extend_schema(
responses={
status.HTTP_200_OK: {
'token': 'string',
'example': {
'token': "string",
},
},
status.HTTP_401_UNAUTHORIZED: {
'error': 'string',
'example': {
'error': 'Invalid credentials'
}
}
}
)
def post(self, request, *args, **kwargs):
username = request.data.get('username')
password = request.data.get('password')
user = authenticate(username=username, password=password)
if user is not None:
# login(request, user)
# User credentials are valid, generate token
token, _ = Token.objects.get_or_create(user=user)
return Response({'token': token.key}, status=status.HTTP_200_OK)
else:
return Response({'error': 'Invalid credentials'}, status=status.HTTP_401_UNAUTHORIZED)


class ProfileView(generics.RetrieveUpdateAPIView):
serializer_class = ProfileSerializer
permission_classes = [IsAuthenticated]
authentication_classes=[TokenAuthentication]

def get_object(self):
return self.request.user
Empty file added backend/backend/__init__.py
Empty file.
16 changes: 16 additions & 0 deletions backend/backend/asgi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""
ASGI config for backend project.

It exposes the ASGI callable as a module-level variable named ``application``.

For more information on this file, see
https://docs.djangoproject.com/en/4.2/howto/deployment/asgi/
"""

import os

from django.core.asgi import get_asgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'backend.settings')

application = get_asgi_application()
Loading