2 Commitit 5526d54209 ... eeed65b611

Tekijä SHA1 Viesti Päivämäärä
  Apostol Mihai eeed65b611 Merge branch 'master' of http://gogs.ici.ro:3000/mihai.apostol/Market_Place_NFT 2 vuotta sitten
  Apostol Mihai 9865c65145 added userprofile creation endpoint 2 vuotta sitten

+ 1 - 1
NFTmarket/NFTmarket/settings.py

@@ -49,7 +49,7 @@ MIDDLEWARE = [
     'django.middleware.security.SecurityMiddleware',
     'django.contrib.sessions.middleware.SessionMiddleware',
     'django.middleware.common.CommonMiddleware',
-    'django.middleware.csrf.CsrfViewMiddleware',
+    #'django.middleware.csrf.CsrfViewMiddleware',
     'django.contrib.auth.middleware.AuthenticationMiddleware',
     'django.contrib.messages.middleware.MessageMiddleware',
     'django.middleware.clickjacking.XFrameOptionsMiddleware',

+ 3 - 2
NFTmarket/NFTmarket/urls.py

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

+ 4 - 6
NFTmarket/userprofile/migrations/0001_initial.py

@@ -1,8 +1,6 @@
-# Generated by Django 4.0.4 on 2022-05-10 07:53
+# Generated by Django 4.0.4 on 2022-05-20 08:35
 
-from django.conf import settings
 from django.db import migrations, models
-import django.db.models.deletion
 
 
 class Migration(migrations.Migration):
@@ -10,7 +8,6 @@ class Migration(migrations.Migration):
     initial = True
 
     dependencies = [
-        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
     ]
 
     operations = [
@@ -18,8 +15,9 @@ class Migration(migrations.Migration):
             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)),
+                ('user', models.CharField(max_length=64)),
+                ('wallet', models.CharField(blank=True, max_length=128, null=True, unique=True)),
+                ('visits', models.IntegerField()),
             ],
         ),
     ]

+ 3 - 16
NFTmarket/userprofile/models.py

@@ -1,22 +1,9 @@
 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)
+    user = models.CharField(max_length = 64)
+    wallet = models.CharField(max_length = 128, blank=True, null=True, unique=True)
+    visits = models.IntegerField()
 
-    
-    @receiver(post_save, sender=User)
-    def create_user_profile(sender, instance, created, **kwargs):
-        if created:
-            UserProfile.objects.create(user=instance)
 

+ 2 - 1
NFTmarket/userprofile/serializer.py

@@ -11,5 +11,6 @@ class UserSerializer(serializers.ModelSerializer):
         fields = ['id', 'username', 'first_name', 'last_name','email']
 
 class UserProfileSerializer(serializers.ModelSerializer):
+    class Meta:
         model = UserProfile
-        fields = ['name']
+        fields = ['user', 'wallet', 'visits']

+ 22 - 3
NFTmarket/userprofile/views.py

@@ -1,6 +1,4 @@
 import json
-from django.shortcuts import render
-from django.shortcuts import get_object_or_404
 
 from rest_framework import viewsets
 # Create your views here.
@@ -10,7 +8,7 @@ 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 .models import UserProfile
 
 from .serializer import UserProfileSerializer, UserSerializer
 
@@ -39,3 +37,24 @@ def user_details(request, pk):
             return JsonResponse(serializer.data, safe=False)
         except:
             return JsonResponse({"error":"User does not exist"})
+
+def log_or_create(request, wallet):
+    if request.method == 'POST':
+        if UserProfile.objects.filter(wallet = wallet).exists():
+            queryset = UserProfile.objects.get(wallet = wallet)
+            queryset.visits += 1
+            queryset.save()
+            serializer = UserProfileSerializer(queryset)
+            return JsonResponse(serializer.data, safe=False)
+        else:
+            UserProfile.objects.create(wallet = wallet, user="NOT_SET", visits = 1)
+            queryset = UserProfile.objects.get(wallet = wallet)
+            serializer = UserProfileSerializer(queryset)
+            return JsonResponse(serializer.data, safe=False)
+    # verific daca exista cont+ chia wallet
+    # if exista return modelul userului
+    # altfel create si return model user
+    return JsonResponse({"error":"Work in progress"})
+
+
+#def change_name_user(request, wallet, nickname):