def put(self, request, format=None): profile = Profile.objects.get(user=request.user) serialized = ProfileSerializer(profile, data=request.data, partial=True) if serialized.is_valid(): serialized.save() return Response(serialized.data, status=status.HTTP_200_OK) return Response(serialized.errors, status=status.HTTP_400_BAD_REQUEST)
def put(self, request): profile = request.user.profile data = request.data serializer = ProfileSerializer(profile, data=data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_200_OK) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def patch(self, request): serializer = ProfileSerializer(instance=request.user.profile, data=request.data, partial=True) if serializer.is_valid(): serializer.save() return Response({'status': 'success', 'data': serializer.data}) return Response({'status': 'errors', 'message': serializer.errors})
def update(self, request, *args, **kwargs): print("------") data = request.data profile = self.get_object() userID = User.objects.get(id=profile.profileUser.id) # check if user which send request == this profile owner if not request.user.is_authenticated: return HttpResponseNotAllowed("You must be logged!") if request.user.id != userID.id: return HttpResponseNotAllowed("Dont try to change somebody else profile!") if not len(data['profileUserName']) > 0 and data['profileUserName'].isalpha() or not len( data['profileUserSurname']) > 0 and data['profileUserSurname'].isalpha(): return HttpResponseNotAllowed("Name and surname have to be letter strings") if len(data['profileUserName']) > 0: profile.profileUserName = data['profileUserName'] if len(data['profileUserSurname']) > 0: profile.profileUserSurname = data['profileUserSurname'] if len(data['profileTelephoneNumber']) > 0: profile.profileTelephoneNumber = data['profileTelephoneNumber'] if data['profileAvatar'] is not None and not isinstance(data['profileAvatar'], str): profile.profileAvatar = data['profileAvatar'] profile.save() serializer = ProfileSerializer(profile, many=False) return Response(serializer.data)
def get(self, request, format=None): user = request.user profile = Profile.objects.get(user=user) profile_ser = ProfileSerializer(profile) img = Photo.objects.get(user=user) img_ser = PhotoSerializer(img) summary = Summary.objects.get(user=user) summary_ser = SummarySerializer(summary) socials = Socials.objects.filter(user=user) socials_ser = GetSocialSerializer(socials) education = School.objects.filter(user=user) edu_ser = SchoolSerializer(education, many=True) jobs = Job.objects.filter(user=user) jobs_ser = JobSerializer(jobs, many=True) skills = Skill.objects.filter(user=user) skills_ser = SkillSerializer(skills, many=True) d = { "profile": profile_ser.data, "img": img_ser.data, "socials": socials_ser.data, "summary": summary_ser.data, "exp": jobs_ser.data, "edu": edu_ser.data, "skills": skills_ser.data, } return Response(d, status.HTTP_200_OK)
def profile_detail(request, pk): """ Retrieve, update or delete a code snippet. """ try: snippet = Profile.objects.get(pk=pk) except Profile.DoesNotExist: return HttpResponse(status=404) if request.method == 'GET': serializer = ProfileSerializer(snippet) return JsonResponse(serializer.data) elif request.method == 'PUT': data = JSONParser().parse(request) serializer = ProfileSerializer(snippet, data=data) if serializer.is_valid(): serializer.save() return JsonResponse(serializer.data) return JsonResponse(serializer.errors, status=400) elif request.method == 'DELETE': snippet.delete() return HttpResponse(status=204) return JsonResponse({"Method Not Allowed": "True"}, status=405)
def post_list(request): """ List all code snippets, or create a new snippet. """ if request.method == 'GET': snippets = Post.objects.all() serializer = PostSerializer(snippets, many=True) return JsonResponse(serializer.data, safe=False) elif request.method == 'POST': data = JSONParser().parse(request) serializer = ProfileSerializer(data=data) if serializer.is_valid(): serializer.save() return JsonResponse(serializer.data, status=201) return JsonResponse(serializer.errors, status=400) return JsonResponse({"Method Not Allowed": "True"}, status=405)
def get_profile_data(profile): profile_ser = ProfileSerializer(instance=profile) profile_data = profile_ser.data boards_data = [] profile_boards = ProfileBoard.objects.filter( profile=profile).prefetch_related('routes').order_by('-id') for board in profile_boards: routes_set = board.routes.all() grades = set(a.grade for a in routes_set) problems = { grade: [{ 'board_id': board.id, 'x_holds': rec.x_holds.split(','), 'y_holds': rec.y_holds.split(','), **{ k: getattr(rec, k) for k in ['id', 'name', 'ticked', 'rating', 'notes'] } } for rec in routes_set if rec.grade == grade] for grade in grades } board_setup = BoardSetup(x_num=int(board.board_dim[:2]), y_num=int(board.board_dim[2:])) boards_data.append({ 'board_id': str(board.id), 'grades': list(grades), 'problems': problems, 'hold_set': json.loads(board.hold_set), 'board_name': board.name, **{ f'{s}_coords': getattr(board_setup, f'{s}_coords') for s in ['x', 'y'] } }) boards_data.reverse() profile_data.update({'boards': boards_data}) return profile_data
def get(self, request): user = request.user profile = user.profile serializer = ProfileSerializer(profile) return Response(serializer.data, status=status.HTTP_200_OK)
def create(self, request): ''' Vista para crear usuarios ''' #Validate data with serializer serializer = ProfileSerializer(data=request.data) if not serializer.is_valid(): return Response(serializer.errors, status=400) try: #Search user current user_current = Profile.objects.get( user__username=request.data['username']) return Response( { 'error': 'El usuario ya se encuentra creado en el aplicativo.' }, status=400) except: try: #Validate correct group group = Group.objects.get(id=request.data['groups']) try: #Save user in modalnative django user = User.objects.create_user( username=request.data['username'], password=request.data['password'], first_name=request.data['first_name'], email=request.data['username'], is_active=True) group.user_set.add(user.id) try: #Save info complementary in model profile profile = Profile.objects.create( user=user, code=request.data['code'], phone_number=request.data['phone_number'], type_identification=request. data['type_identification'], identification_number=request. data['identification_number']) return Response( { 'id': profile.id, 'user': { 'id': user.id, 'first_name': user.first_name, 'username': user.username, 'groups': group.id }, 'code': profile.code, 'identification_number': profile.identification_number, 'phone_number': profile.phone_number }, status=200) except: return Response( { 'error': 'No se pudo crear el usuario, vuelva a intentarlo.' }, status=400) except: return Response( {'error': 'No se puso guardar el usuario.'}, status=400) except: return Response( { 'error': 'No se encontró el tipo de usuario seleccionado. Contacte al administrador.' }, status=400)
def get(self, request): serializer = ProfileSerializer(instance=request.user.profile) return Response({'status': 'success', 'data': serializer.data})
def get(self, request, format=None): profile = Profile.objects.get(user=request.user) serialized = ProfileSerializer(profile) if serialized: return Response(serialized.data, status=status.HTTP_200_OK) return Response(serialized.errors, status=status.HTTP_404_NOT_FOUND)