reverted to old web configuration on main branch
This commit is contained in:
0
api/__init__.py
Normal file
0
api/__init__.py
Normal file
3
api/admin.py
Normal file
3
api/admin.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from django.contrib import admin
|
||||
|
||||
# Register your models here.
|
||||
6
api/apps.py
Normal file
6
api/apps.py
Normal file
@@ -0,0 +1,6 @@
|
||||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class ApiConfig(AppConfig):
|
||||
default_auto_field = 'django.db.models.BigAutoField'
|
||||
name = 'api'
|
||||
14
api/models.py
Normal file
14
api/models.py
Normal file
@@ -0,0 +1,14 @@
|
||||
from django.db import models
|
||||
# Create your models here.
|
||||
|
||||
from django.contrib.auth.models import User
|
||||
|
||||
class UserProfile(models.Model):
|
||||
user = models.OneToOneField(User, on_delete=models.CASCADE)
|
||||
# Add custom fields here
|
||||
bio = models.TextField(blank=True)
|
||||
birthdate = models.DateField(null=True, blank=True)
|
||||
profile_picture = models.ImageField(upload_to='profile_pics/', null=True, blank=True)
|
||||
|
||||
def __str__(self):
|
||||
return f'{self.user.username} Profile'
|
||||
7
api/permissions.py
Normal file
7
api/permissions.py
Normal file
@@ -0,0 +1,7 @@
|
||||
from rest_framework_api_key.permissions import HasAPIKey
|
||||
|
||||
class UserEditAPIKeyPermissions(HasAPIKey):
|
||||
"""
|
||||
Custom permision for restricting access using API key.
|
||||
"""
|
||||
pass
|
||||
21
api/serializers.py
Normal file
21
api/serializers.py
Normal file
@@ -0,0 +1,21 @@
|
||||
from rest_framework import serializers
|
||||
from .models import User
|
||||
|
||||
#Serializers are for what views can show fields of models
|
||||
|
||||
class PublicUserSerializers(serializers.ModelSerializer):
|
||||
"""
|
||||
Serializer for public User fields
|
||||
"""
|
||||
class Meta:
|
||||
model = User
|
||||
fields = ['id', 'username']
|
||||
|
||||
class SecureUserSerializers(serializers.ModelSerializer):
|
||||
"""
|
||||
Serializer for all User fields
|
||||
Requires API key
|
||||
"""
|
||||
class Meta:
|
||||
model = User
|
||||
fields = ['id', 'username', 'email', 'first_name', 'last_name', 'bio']
|
||||
3
api/tests.py
Normal file
3
api/tests.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from django.test import TestCase
|
||||
|
||||
# Create your tests here.
|
||||
10
api/urls.py
Normal file
10
api/urls.py
Normal file
@@ -0,0 +1,10 @@
|
||||
from django.urls import path
|
||||
from .views import PublicUserView, SecureUserUpdateView
|
||||
|
||||
urlpatterns = [
|
||||
# URL for the public view to list users with public fields
|
||||
path('users/', PublicUserView.as_view(), name='public-user-list'),
|
||||
|
||||
# URL for secure view to retrieve and update user with all fields
|
||||
path('users/<int:pk>/', SecureUserUpdateView.as_view(), name='secure-user-update'),
|
||||
]
|
||||
20
api/views.py
Normal file
20
api/views.py
Normal file
@@ -0,0 +1,20 @@
|
||||
from django.shortcuts import render
|
||||
|
||||
# Create your views here.
|
||||
|
||||
from rest_framework import generics, permissions
|
||||
from .models import User
|
||||
from .serializers import PublicUserSerializers, SecureUserSerializers
|
||||
from .permissions import UserEditAPIKeyPermissions
|
||||
|
||||
#Public view: List users with only public fields
|
||||
class PublicUserView(generics.ListAPIView):
|
||||
queryset = User.objects.all()
|
||||
serializer_class = PublicUserSerializers
|
||||
permission_classes = [permissions.AllowAny]
|
||||
|
||||
#Secure view for retrive/update user all fields (API key)
|
||||
class SecureUserUpdateView(generics.RetrieveUpdateAPIView):
|
||||
queryset = User.objects.all()
|
||||
serializer_class = SecureUserSerializers
|
||||
permission_classes = [UserEditAPIKeyPermissions]
|
||||
Reference in New Issue
Block a user