import json 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 .models import UserProfile 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"}) 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):