def post(self, request, format='json'): user_serializer = UserSerializer(data=request.data) if user_serializer.is_valid(): # check to make sure company data is valid company_serializer = CompanySerializer(data=request.data) if company_serializer.is_valid(): company = company_serializer.save() else: message = {'error': 'invalid company data'} return Response(message, status=status.HTTP_400_BAD_REQUEST) # save user user = user_serializer.save() # connect company to user data = {'user': user.id, 'company': company.id} user_company_serializer = UserCompanySerializer(data=data) if user_company_serializer.is_valid(): user_company = user_company_serializer.save() if user_company: return Response(user_serializer.data, status=status.HTTP_201_CREATED) else: message = {'error': 'problem connecting user and company'} return Response(message, status=status.HTTP_400_BAD_REQUEST) else: message = user_serializer.errors return Response(message, status=status.HTTP_400_BAD_REQUEST)
def post(self, request, format='json'): serializer = UserSerializer(data=request.data) if serializer.is_valid(): user = serializer.save() if user: return Response(serializer.data, status=status.HTTP_201_CREATED)
def post(self, request, format='json'): try: with transaction.atomic(): california_tz = pytz.timezone('US/Pacific') key = request.data.get('key', None) company_id = None invite = None # if we have the key, locate the company if key: # verify the invite -- find the company associated with it invite = Invite.objects.get(invite_key=key) if not invite: return Response({'error': 'Invalid invite key'}, status=status.HTTP_400_BAD_REQUEST) if invite.expires_on < california_tz.localize( datetime.now()): return Response({'error': 'Invite key has expired'}, status=status.HTTP_400_BAD_REQUEST) if invite.is_claimed: return Response( {'error': 'Invite key has already been used'}, status=status.HTTP_400_BAD_REQUEST) invite.is_claimed = True invite.save() company_id = invite.company_id # else we do not have the key, make a new company else: # verify the company is not null company_serializer = CompanySerializer(data=request.data) if not company_serializer.is_valid(): raise Exception(company_serializer.errors) # save the company -- get the id where we should save company_id = company_serializer.save().id # verify the user had valid data data = request.data if invite is not None: data['first_name'] = invite.first_name data['last_name'] = invite.last_name data['is_staff'] = True if invite.role else False else: data['first_name'] = '' data['last_name'] = '' data['is_staff'] = False user_serializer = UserSerializer(data=data) if not user_serializer.is_valid(): raise Exception(user_serializer.errors) user_id = user_serializer.save().id data = {'user': user_id, 'company': company_id} user_company_serializer = UserCompanySerializer(data=data) if not user_company_serializer.is_valid(): raise Exception(user_company_serializer.errors) user_company = user_company_serializer.save() return Response(user_serializer.data, status=status.HTTP_201_CREATED) except Exception as err: print(err) return Response({'error': str(err)}, status=status.HTTP_400_BAD_REQUEST)
def post(self, request, format='json'): # make this a function in the future please. california_tz = pytz.timezone('US/Pacific') # obtain the key key =request.data.get('key', None) # verify the user is unique user_serializer = UserSerializer(data=request.data) # if we cannot verify the user, then throw if not user_serializer.is_valid(): return Response(user_serializer.errors, status=status.HTTP_400_BAD_REQUEST) # verify the company # if we have the key, locate the company if key: # verify the invite -- find the company associated with it try: # get the company id that has the key and expiration is greater than equal to today company_id = CompanyInvite.objects.get( invite_key=key, expires_on__gte = california_tz.localize(datetime.now()) ).company_id except Exception as err: # invite not found or expired return Response({'error': 'Invite is invalid or has expired.'}, status=status.HTTP_400_BAD_REQUEST) # if we do not have the key, make a new company else: # verify the company is not null company_serializer = CompanySerializer(data=request.data) # if we cannot verify, throw if not company_serializer.is_valid(): return Response(company.errors, status=status.HTTP_400_BAD_REQUEST) # save the company -- get the id where we should save company_id = company_serializer.save().id # get the user user_id = user_serializer.save().id # bind user and company -- company already has the id data = { 'user' : user_id, 'company' : company_id } # attempt to bind otherwise throw user_company_serializer = UserCompanySerializer(data=data) if not user_company_serializer.is_valid(): return Response(user_company_serializer.errors, status=status.HTTP_400_BAD_REQUEST) # successfully created user and company, so we can now save user_company = user_company_serializer.save() return Response(user_serializer.data, status=status.HTTP_201_CREATED)