def patch(self, request): profile = Profile.objects.get(user=request.user) serializer = ProfileSerializer(profile, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_200_OK) return Response(serializer.errors, status=status.HTTP_406_NOT_ACCEPTABLE)
def register(request): form = RegisterForm(request.POST) if form.is_valid(): user = form.save(commit=False) user.save() profile = Profile.objects.get_or_create(user=user) serializer = ProfileSerializer(profile, data=request.data) if serializer.is_valid(): serializer.save() return Response(status=status.HTTP_201_CREATED) return Response(form.errors, status=status.HTTP_406_NOT_ACCEPTABLE)
def get(self, request): if request.user.is_authenticated: profile = Profile.objects.get(user=request.user) data = ProfileSerializer(profile).data data['level'] = get_level(data['exp']) return Response(data, status=status.HTTP_200_OK) return Response(status=status.HTTP_400_BAD_REQUEST)
def get(self, request, string): try: user = User.objects.get(username=string) except: return Response(status=status.HTTP_404_NOT_FOUND) profile = Profile.objects.get_or_create(user=user) return Response(ProfileSerializer(profile).data, status=status.HTTP_200_OK)
def page_ranking(request): begin = request.GET.get('begin') end = request.GET.get('end') profiles = Profile.objects.all().order_by('exp').reverse() serializer = ProfileSerializer(profiles, many=True) users = serializer.data[begin:end] for user in users: user['level'] = get_level(user['exp']) return Response(users)
def register(request): # fix here """ registeration --- # Parameters - username: string - email: string | email-format - password1: string | password - password2: string | password confirmation - name: string | the user's name - weight: float - average_sleep_time : float - sleep_at: string | time format | nullable - image: string | image url | nullable """ form = RegisterForm(request.POST) if form.is_valid(): user = form.save(commit=False) try: # fix it later user.save() except: pass profile = Profile.objects.create( user=user, name='default', weight=-1, average_sleep_time=-1, ) serializer = ProfileSerializer(profile, data=request.data) if serializer.is_valid(): serializer.save() return Response(status=status.HTTP_201_CREATED) Profile.objects.get(user=user).delete() #import pdb; pdb.set_trace() print(serializer.errors) return Response(serializer.errors, status=status.HTTP_406_NOT_ACCEPTABLE) print("form not valid") print(form.errors) return Response(form.errors, status=status.HTTP_406_NOT_ACCEPTABLE)
def applications_list(request, ): if request.user.is_authenticated: user = Profile.objects.get(user=request.user) profile = ProfileSerializer(user).data applications = Application.objects.filter(club=profile['club'], is_hide=False) serializer = ApplicationSerializer(applications, many=True) dic = { "data": serializer.data, "length": count_applications(serializer.data) } return Response(dic, status=status.HTTP_200_OK) return Response(status=status.HTTP_401_UNAUTHORIZED)
def get(self, request): if request.user.is_authenticated: profile = Profile.objects.get(user=user) return Response(ProfileSerializer(profile).data, status=status.HTTP_200_OK) return Response(status=status.HTTP_401_UNAUTHORIZED)
def get(self, request): profile = Profile.objects.get(user=user) return Response(ProfileSerializer(profile).data, status=status.HTTP_200_OK)
def has_object_permission(self, request, view, obj): user = Profile.objects.get(user=request.user) application = ApplicationSerializer(obj) profile = ProfileSerializer(user) return application['club'].value == profile['club'].value