def post(self, request): try: request_data = request.data.copy() except AttributeError as e: logger.error('ClockinsMeView:post: %s' % str(e)) return Response(str(e), status=status.HTTP_500_INTERNAL_SERVER_ERROR) logger.info('ClockinsMeView:post: %s' % request_data) request_data['employee'] = self.employee.id request_data['author'] = self.employee.user.profile.id if 'latitude_in' in request_data: request_data['latitude_in'] = utils.stringToDecimal(request_data['latitude_in']) if 'latitude_out' in request_data: request_data['latitude_out'] = utils.stringToDecimal(request_data['latitude_out']) if 'longitude_in' in request_data: request_data['longitude_in'] = utils.stringToDecimal(request_data['longitude_in']) if 'longitude_out' in request_data: request_data['longitude_out'] = utils.stringToDecimal(request_data['longitude_out']) logger.debug('ClockinsMeView:post: {request_data}') if 'started_at' not in request_data and 'ended_at' not in request_data: return Response( validators.error_object("You need to specify started_at or ended_at"), # NOQA status=status.HTTP_400_BAD_REQUEST) instance = None if 'ended_at' in request_data: logger.info('ClockinsMeView:post: Is a Clock out Request') try: instance = Clockin.objects.get( shift=request_data["shift"], employee=request_data["employee"], ended_at=None) except Clockin.DoesNotExist: return Response( validators.error_object( "There is no previous clockin for this shift"), status=status.HTTP_400_BAD_REQUEST) except Clockin.MultipleObjectsReturned: return Response( validators.error_object( "It seems there is more than one clockin without clockout for this shift"), # NOQA status=status.HTTP_400_BAD_REQUEST) logger.info('ClockinsMeView:post:serializer') serializer = clockin_serializer.ClockinSerializer( instance, data=request_data, context={"request": request}) if not serializer.is_valid(): return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED)
def post(self, request): self.validate_employee(request) request.data['employee'] = self.employee.id # checkin if 'started_at' in request.data: request.data['latitude_in'] = round( decimal.Decimal(request.data['latitude_in']), 11) if request.data['latitude_in'] else None request.data['longitude_in'] = round( decimal.Decimal(request.data['longitude_in']), 11) if request.data['longitude_in'] else None serializer = clockin_serializer.ClockinSerializer( data=request.data, context={"request": request}) # checkout elif 'ended_at' in request.data: request.data['latitude_out'] = round( decimal.Decimal(request.data['latitude_out']), 11) if request.data['latitude_out'] else None request.data['longitude_out'] = round( decimal.Decimal(request.data['longitude_out']), 11) if request.data['longitude_out'] else None try: clockin = Clockin.objects.get( shift=request.data["shift"], employee=request.data["employee"], ended_at=None) serializer = clockin_serializer.ClockinSerializer( clockin, data=request.data, context={"request": request}) except Clockin.DoesNotExist: return Response(validators.error_object( "There is no previous clockin for this shift"), status=status.HTTP_400_BAD_REQUEST) except Clockin.MultipleObjectsReturned: return Response(validators.error_object( "It seems there is more than one clockin without clockout for this shif" ), status=status.HTTP_400_BAD_REQUEST) else: return Response(validators.error_object( "You need to specify started_at or ended_at"), status=status.HTTP_400_BAD_REQUEST) if serializer.is_valid(): serializer.save() else: return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) return Response(serializer.data, status=status.HTTP_201_CREATED)
def get(self, request, id=False): self.validate_employer(request) if (id): try: shift = Shift.objects.get(id=id, employer__id=self.employer.id) except Shift.DoesNotExist: return Response(validators.error_object('Not found.'), status=status.HTTP_404_NOT_FOUND) serializer = shift_serializer.ShiftGetSerializer(shift, many=False) return Response(serializer.data, status=status.HTTP_200_OK) else: shifts = Shift.objects.filter( employer__id=self.employer.id).order_by('starting_at') qStatus = request.GET.get('status') if validators.in_choices(qStatus, SHIFT_STATUS_CHOICES): return Response(validators.error_object("Invalid Status"), status=status.HTTP_400_BAD_REQUEST) elif qStatus: shifts = shifts.filter(status__in=qStatus.split(",")) qStatus = request.GET.get('not_status') if validators.in_choices(qStatus, SHIFT_STATUS_CHOICES): return Response(validators.error_object("Invalid Status"), status=status.HTTP_400_BAD_REQUEST) elif qStatus: shifts = shifts.filter(~Q(status=qStatus)) qUpcoming = request.GET.get('upcoming') if qUpcoming == 'true': shifts = shifts.filter(starting_at__gte=TODAY) qUnrated = request.GET.get('unrated') if qUnrated == 'true': shifts = shifts.filter(rate_set=None) if request.user.profile.employer is None: shifts = shifts.filter( employees__in=(request.user.profile.id, )) else: shifts = shifts.filter( employer=request.user.profile.employer.id) serializer = shift_serializer.ShiftGetSerializer(shifts, many=True) return Response(serializer.data, status=status.HTTP_200_OK)
def get(self, request, id=False): if (id): try: rate = Rate.objects.get(id=id) except Rate.DoesNotExist: return Response(validators.error_object('Not found.'), status=status.HTTP_404_NOT_FOUND) serializer = rating_serializer.RatingGetSerializer(rate, many=False) else: rates = Rate.objects.all() qEmployer = request.GET.get('employer') qEmployee = request.GET.get('employee') if qEmployee: rates = rates.filter(employee__id=qEmployee) elif qEmployer: rates = rates.filter(employee__id=qEmployer) qShift = request.GET.get('shift') if qShift: rates = rates.filter(shift__id=qShift) serializer = rating_serializer.RatingGetSerializer(rates, many=True) return Response(serializer.data, status=status.HTTP_200_OK)
def get(self, request, id=False): self.validate_employer(request) if (id): try: invite = ShiftInvite.objects.get(id=id, employer__id=self.employer.id) except ShiftInvite.DoesNotExist: return Response(validators.error_object( 'The invite was not found, maybe the shift does not exist anymore. Talk to the employer for any more details about this error.' ), status=status.HTTP_404_NOT_FOUND) serializer = shift_serializer.ShiftInviteGetSerializer(invite, many=False) else: invites = ShiftInvite.objects.filter( sender__employer__id=self.employer.id) qEmployee_id = request.GET.get('employee') if qEmployee_id: invites = invites.filter(employer__id=qEmployee_id) qShift_id = request.GET.get('shift') if qShift_id: invites = invites.filter(shift__id=qShift_id) qStatus = request.GET.get('status') if qStatus: invites = invites.filter(status=qStatus) serializer = shift_serializer.ShiftInviteGetSerializer(invites, many=True) return Response(serializer.data, status=status.HTTP_200_OK)
def put(self, request): if request.user.profile == None: raise PermissionDenied("You dont seem to have a profile") try: profile = Profile.objects.get(id=request.user.profile.id) except Profile.DoesNotExist: return Response(validators.error_object('Not found.'), status=status.HTTP_404_NOT_FOUND) if "latitude" in request.data: request.data["latitude"] = round(request.data["latitude"], 6) if "longitude" in request.data: request.data["longitude"] = round(request.data["longitude"], 6) serializer = profile_serializer.ProfileSerializer( profile, data=request.data, context={"request": request}, partial=True) userSerializer = user_serializer.UserUpdateSerializer( profile.user, data=request.data, partial=True) if serializer.is_valid() and userSerializer.is_valid(): serializer.save() userSerializer.save() return Response(serializer.data, status=status.HTTP_200_OK) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def put(self, request): if request.user.profile == None: raise PermissionDenied("You dont seem to have a profile") try: profile = Profile.objects.get(id=request.user.profile.id) except Profile.DoesNotExist: return Response(validators.error_object('Not found.'), status=status.HTTP_404_NOT_FOUND) result = cloudinary.uploader.upload(request.FILES['image'], public_id='profile' + str(request.user.profile.id), crop='limit', width=450, height=450, eager=[ { 'width': 200, 'height': 200, 'crop': 'thumb', 'gravity': 'face', 'radius': 100 }, ], tags=['profile_picture']) profile.picture = result['secure_url'] profile.save() serializer = profile_serializer.ProfileSerializer(profile) return Response(serializer.data, status=status.HTTP_200_OK)
def put(self, request, id): if (request.user.profile.employer == None): return Response( validators.error_object("You don't seem to be an employer"), status=status.HTTP_400_BAD_REQUEST) try: emp = Employee.objects.get(id=id) except Employee.DoesNotExist: return Response({"detail": "The employee was not found"}, status=status.HTTP_404_NOT_FOUND) _serializers = [] for clockin in request.data: try: old_clockin = Clockin.objects.get(id=clockin["id"]) serializer = clockin_serializer.ClockinPayrollSerializer( old_clockin, data=clockin) except Clockin.DoesNotExist: serializer = clockin_serializer.ClockinPayrollSerializer( data=clockin) if not serializer.is_valid(): return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) _serializers.append(serializer) for serializer in _serializers: serializer.save() return Response(serializer.data, status=status.HTTP_200_OK)
def post(self, request): email = request.data.get('email', None) if not email: return Response( validators.error_object('Email not found on the database'), status=status.HTTP_400_BAD_REQUEST) try: user = User.objects.get(email=email) serializer = auth_serializer.UserLoginSerializer(user) except User.DoesNotExist: return Response( validators.error_object('Email not found on the database'), status=status.HTTP_404_NOT_FOUND) notify_password_reset_code(user) return Response(serializer.data, status=status.HTTP_200_OK)
def delete(self, request, id): try: rate = Rate.objects.get(id=id) except Rate.DoesNotExist: return Response(validators.error_object('Not found.'), status=status.HTTP_404_NOT_FOUND) rate.delete() return Response(status=status.HTTP_204_NO_CONTENT)
def get(self, request, id): try: user = User.objects.get(id=id) serializer = user_serializer.UserGetSerializer(user) except User.DoesNotExist: return Response(validators.error_object('The user was not found'), status=status.HTTP_404_NOT_FOUND) return Response(serializer.data, status=status.HTTP_200_OK)
def delete(self, request, clockin_id): try: clockin = Clockin.objects.get(id=clockin_id) except Clockin.DoesNotExist: return Response(validators.error_object('Not found.'), status=status.HTTP_404_NOT_FOUND) clockin.delete() return Response(status=status.HTTP_204_NO_CONTENT)
def post(self, request): if 'document_type' not in request.data: return Response(validators.error_object( 'You need to specify the type of document you are uploading'), status=status.HTTP_400_BAD_REQUEST) if 'document' not in request.FILES: return Response( validators.error_object('Please specify a document'), status=status.HTTP_400_BAD_REQUEST) public_id = f'profile-{str(self.request.user.id)}-{datetime.now().strftime("%d-%m")}-{get_random_string(length=32)}' file_name = f'{str(self.request.user.id)}/i9_documents/{public_id}' try: result = cloudinary.uploader.upload(request.FILES['document'], public_id=file_name, tags=[ 'i9_document', 'profile-' + str(self.request.user.id), ], use_filename=1, unique_filename=1, resource_type='auto') print(result) except Exception as e: print(e) return JsonResponse({"error": str(e)}, status=400) data = { 'document': result['secure_url'], 'employee': self.employee.id, 'document_type': request.data['document_type'], 'public_id': public_id, } print('data picker', data) serializer = documents_serializer.EmployeeDocumentSerializer(data=data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) cloudinary.uploader.destroy(public_id) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def get(self, request, employee_id): try: employee = Employee.objects.get(id=employee_id) except Employee.DoesNotExist: return Response(validators.error_object('Not found.'), status=status.HTTP_404_NOT_FOUND) serializer = other_serializer.BadgeSerializer(employee.badges, many=True) return Response(serializer.data, status=status.HTTP_200_OK)
def delete(self, request, device_id=None): if request.user is None: return Response(validators.error_object('No user was identified'), status=status.HTTP_400_BAD_REQUEST) try: if device_id is None: devices = FCMDevice.objects.filter(user=request.user.id) devices.delete() else: device = FCMDevice.objects.get(registration_id=device_id, user=request.user.id) device.delete() return Response(status=status.HTTP_204_NO_CONTENT) except FCMDevice.DoesNotExist: return Response(validators.error_object('Device not found'), status=status.HTTP_404_NOT_FOUND)
def put(self, request, device_id): if request.user is None: return Response(validators.error_object('No user was identified'), status=status.HTTP_400_BAD_REQUEST) try: device = FCMDevice.objects.get(registration_id=device_id, user=request.user.id) serializer = notification_serializer.FCMDeviceSerializer( device, 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_400_BAD_REQUEST) except FCMDevice.DoesNotExist: return Response(validators.error_object('Device not found'), status=status.HTTP_404_NOT_FOUND)
def delete(self, request, id): self.validate_employer(request) try: venue = Venue.objects.get(id=id, employer__id=self.employer.id) except Venue.DoesNotExist: return Response(validators.error_object('Not found.'), status=status.HTTP_404_NOT_FOUND) venue.delete() return Response(status=status.HTTP_204_NO_CONTENT)
def get(self, request, application_id): try: application = ShiftApplication.objects.get(id=application_id) except ShiftApplication.DoesNotExist: return Response(validators.error_object('Not found.'), status=status.HTTP_404_NOT_FOUND) serializer = shift_serializer.ApplicantGetSmallSerializer(application, many=False) return Response(serializer.data, status=status.HTTP_200_OK)
def delete(self, request, id): permission_classes = [IsAdminUser] try: user = User.objects.get(id=id) except User.DoesNotExist: return Response(validators.error_object('The user was not found'), status=status.HTTP_404_NOT_FOUND) user.delete() return Response(status=status.HTTP_204_NO_CONTENT)
def delete(self, request, id): permission_classes = (IsAdminUser, ) try: badge = Badge.objects.get(id=id) except Badge.DoesNotExist: return Response(validators.error_object('Not found.'), status=status.HTTP_404_NOT_FOUND) badge.delete() return Response(status=status.HTTP_204_NO_CONTENT)
def get(self, request, employer_id=None): if employer_id == None: return Response( validators.error_object('The employer must be specified'), status=status.HTTP_404_NOT_FOUND) qStarted_at = request.GET.get('starting_at') if qStarted_at is None: return Response( validators.error_object('You need to specify starting_at'), status=status.HTTP_404_NOT_FOUND) qEmployee = request.GET.get('employee') qLen = request.GET.get('period_length') qType = request.GET.get('period_type') # try: projection = payment_serializer.get_projected_payments( employer_id=employer_id, start_date=get_aware_datetime(qStarted_at), talent_id=qEmployee, period_length=int(qLen) if qLen is not None else 7, period_type=qType.upper() if qType is not None else 'DAYS') talent = None if qEmployee: try: employee = Employee.objects.get(id=qEmployee) talent = employee_serializer.EmployeeGetSerializer( employee, many=False).data except Employee.DoesNotExist: return Response(validators.error_object('Not found.'), status=status.HTTP_404_NOT_FOUND) return Response({ "days": projection, "employee": talent }, status=status.HTTP_200_OK)
def get(self, request): self.validate_employee(request) try: employee = Employee.objects.get(id=self.employee.id) except Employee.DoesNotExist: return Response(validators.error_object('Not found.'), status=status.HTTP_404_NOT_FOUND) serializer = employee_serializer.EmployeeGetSerializer(employee, many=False) return Response(serializer.data, status=status.HTTP_200_OK)
def delete(self, request, unavailability_id): self.validate_employee(request) try: unavailability_block = EmployeeWeekUnvailability.objects.get( id=unavailability_id) except EmployeeWeekUnvailability.DoesNotExist: return Response(validators.error_object('Not found.'), status=status.HTTP_404_NOT_FOUND) unavailability_block.delete() return Response(status=status.HTTP_204_NO_CONTENT)
def get(self, request, device_id=None): if request.user is None: return Response(validators.error_object('You have to be loged in'), status=status.HTTP_400_BAD_REQUEST) if device_id is not None: try: device = FCMDevice.objects.get(registration_id=device_id, user=request.user.id) serializer = notification_serializer.FCMDeviceSerializer( device, many=False) return Response(serializer.data, status=status.HTTP_200_OK) except FCMDevice.DoesNotExist: return Response(validators.error_object('Not found.'), status=status.HTTP_404_NOT_FOUND) else: devices = FCMDevice.objects.filter(user=request.user.id) serializer = notification_serializer.FCMDeviceSerializer(devices, many=True) return Response(serializer.data, status=status.HTTP_200_OK)
def delete(self, request, id): self.validate_employer(request) try: favList = favlist_serializer.FavoriteList.objects.get( id=id, employer__id=self.employer.id) except FavoriteList.DoesNotExist: return Response(validators.error_object('Not found.'), status=status.HTTP_404_NOT_FOUND) favList.delete() return Response(status=status.HTTP_204_NO_CONTENT)
def delete(self, request, document_id): try: document = EmployeeDocument.objects.get(id=document_id) cloudinary.uploader.destroy(document.public_id) document.delete() except EmployeeDocument.DoesNotExist: return Response(validators.error_object('Not found.'), status=status.HTTP_404_NOT_FOUND) return Response(status=status.HTTP_204_NO_CONTENT)
def put(self, request, id): try: employer = Employer.objects.get(id=id) except Employer.DoesNotExist: return Response(validators.error_object('Not found.'), status=status.HTTP_404_NOT_FOUND) serializer = employer_serializer.EmployerSerializer(employer, 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_400_BAD_REQUEST)
def get(self, request): token = request.GET.get('token') payload = jwt_decode_handler(token) try: user = User.objects.get(id=payload["user_id"]) user.profile.status = ACTIVE #email validation completed user.profile.save() except User.DoesNotExist: return Response(validators.error_object('Not found.'), status=status.HTTP_404_NOT_FOUND) template = get_template_content('email_validated') return HttpResponse(template['html'])
def put(self, request, id): try: position = Position.objects.get(id=id) except Position.DoesNotExist: return Response(validators.error_object('Not found.'), status=status.HTTP_404_NOT_FOUND) serializer = other_serializer.PositionSerializer(position, 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_400_BAD_REQUEST)
def get(self, request): if request.user.profile == None: raise PermissionDenied("You dont seem to have a profile") try: profile = Profile.objects.get(id=request.user.profile.id) except Profile.DoesNotExist: return Response(validators.error_object('Not found.'), status=status.HTTP_404_NOT_FOUND) serializer = profile_serializer.ProfileGetSerializer(profile, many=False) return Response(serializer.data, status=status.HTTP_200_OK)