Преглед на файлове

added components and basic routing and serialize

Apostol Mihai преди 2 години
родител
ревизия
165adb657b

+ 4 - 1
NFTmarket/NFTmarket/settings.py

@@ -39,6 +39,9 @@ INSTALLED_APPS = [
     'django.contrib.messages',
     'django.contrib.staticfiles',
     'rest_framework',
+
+    'userprofile',
+    'market',
     #'info_pages',
 ]
 
@@ -74,7 +77,7 @@ REST_FRAMEWORK = {
     # Use Django's standard `django.contrib.auth` permissions,
     # or allow read-only access for unauthenticated users.
     'DEFAULT_PERMISSION_CLASSES': [
-        'rest_framework.permissions.DjangoModelPermissionsOrAnonReadOnly'
+        #'rest_framework.permissions.DjangoModelPermissionsOrAnonReadOnly'
     ]
 }
 

+ 5 - 1
NFTmarket/NFTmarket/urls.py

@@ -19,13 +19,17 @@ from django.urls import include, path
 
 from rest_framework import routers
 
+from userprofile.views import (user_list, user_details)
 
 router = routers.DefaultRouter()
 
+
 urlpatterns = [
 
     path('admin/', admin.site.urls),
     path('', include(router.urls)),
     #path(r'', include(infopages_urlpatterns)),
-    path('api-auth/', include('rest_framework.urls'))
+    path('api-auth/', include('rest_framework.urls')),
+    path('userlist/', user_list, name='user-list'),
+    path('userdetails/<int:pk>', user_details, name='user-details'),
 ]

+ 0 - 0
NFTmarket/market/__init__.py


+ 3 - 0
NFTmarket/market/admin.py

@@ -0,0 +1,3 @@
+from django.contrib import admin
+
+# Register your models here.

+ 6 - 0
NFTmarket/market/apps.py

@@ -0,0 +1,6 @@
+from django.apps import AppConfig
+
+
+class MarketConfig(AppConfig):
+    default_auto_field = 'django.db.models.BigAutoField'
+    name = 'market'

+ 0 - 0
NFTmarket/market/migrations/__init__.py


+ 3 - 0
NFTmarket/market/models.py

@@ -0,0 +1,3 @@
+from django.db import models
+
+# Create your models here.

+ 3 - 0
NFTmarket/market/tests.py

@@ -0,0 +1,3 @@
+from django.test import TestCase
+
+# Create your tests here.

+ 3 - 0
NFTmarket/market/views.py

@@ -0,0 +1,3 @@
+from django.shortcuts import render
+
+# Create your views here.

+ 0 - 0
NFTmarket/userprofile/__init__.py


+ 8 - 0
NFTmarket/userprofile/admin.py

@@ -0,0 +1,8 @@
+from django.contrib import admin
+
+# Register your models here.
+from .models import UserProfile
+
+
+
+admin.site.register(UserProfile)

+ 6 - 0
NFTmarket/userprofile/apps.py

@@ -0,0 +1,6 @@
+from django.apps import AppConfig
+
+
+class UserextensionConfig(AppConfig):
+    default_auto_field = 'django.db.models.BigAutoField'
+    name = 'userprofile'

+ 25 - 0
NFTmarket/userprofile/migrations/0001_initial.py

@@ -0,0 +1,25 @@
+# Generated by Django 4.0.4 on 2022-05-10 07:53
+
+from django.conf import settings
+from django.db import migrations, models
+import django.db.models.deletion
+
+
+class Migration(migrations.Migration):
+
+    initial = True
+
+    dependencies = [
+        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
+    ]
+
+    operations = [
+        migrations.CreateModel(
+            name='UserProfile',
+            fields=[
+                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
+                ('wallet', models.CharField(blank=True, max_length=128, null=True)),
+                ('user', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
+            ],
+        ),
+    ]

+ 0 - 0
NFTmarket/userprofile/migrations/__init__.py


+ 22 - 0
NFTmarket/userprofile/models.py

@@ -0,0 +1,22 @@
+from django.db import models
+from django.contrib.auth.models import User
+
+# Create your models here.
+from django.db.models.signals import post_save, post_delete
+from django.dispatch import receiver
+
+
+
+class UserProfile(models.Model):
+    '''
+    Extension for the default Django User model
+    '''
+    user = models.OneToOneField(User, on_delete=models.CASCADE)
+    wallet = models.CharField(max_length = 128, blank=True, null=True)
+
+    
+    @receiver(post_save, sender=User)
+    def create_user_profile(sender, instance, created, **kwargs):
+        if created:
+            UserProfile.objects.create(user=instance)
+

+ 15 - 0
NFTmarket/userprofile/serializer.py

@@ -0,0 +1,15 @@
+from django.contrib.auth.models import User
+from rest_framework import serializers
+
+from django.contrib.auth.models import User
+from .models import UserProfile
+
+
+class UserSerializer(serializers.ModelSerializer):
+    class Meta:
+        model = User
+        fields = ['id', 'username', 'first_name', 'last_name','email']
+
+class UserProfileSerializer(serializers.ModelSerializer):
+        model = UserProfile
+        fields = ['name']

+ 3 - 0
NFTmarket/userprofile/tests.py

@@ -0,0 +1,3 @@
+from django.test import TestCase
+
+# Create your tests here.

+ 41 - 0
NFTmarket/userprofile/views.py

@@ -0,0 +1,41 @@
+import json
+from django.shortcuts import render
+from django.shortcuts import get_object_or_404
+
+from rest_framework import viewsets
+# Create your views here.
+from django.http import HttpResponse, JsonResponse
+from rest_framework.parsers import JSONParser
+
+from django.contrib.auth.models import User
+from rest_framework import serializers
+from rest_framework.response import Response
+
+
+from .serializer import UserProfileSerializer, UserSerializer
+
+def user_list(request):
+    '''
+    List all users or create a user
+    '''
+    if request.method == 'GET':
+        queryset = User.objects.all()
+        serializer = UserSerializer(queryset, many=True)
+        return JsonResponse(serializer.data, safe=False)
+
+    elif request.method == 'POST':
+        data = JSONParser().parse(request)
+        serializer = UserSerializer(data=data)
+        if serializer.is_valid():
+            serializer.save()
+            return JsonResponse(serializer.data, status=201)
+        return JsonResponse(serializer.errors, status=400)
+
+def user_details(request, pk):
+    if request.method == 'GET':
+        try:
+            queryset = User.objects.get(pk=pk)
+            serializer = UserSerializer(queryset)
+            return JsonResponse(serializer.data, safe=False)
+        except:
+            return JsonResponse({"error":"User does not exist"})