def update_address(self, request, *args, **kwargs): try: instance = self.get_object() serializer = UserAddressSerializer(data=request.data) serializer.is_valid(raise_exception=True) address, created = Address.objects.get_or_create( line1=serializer.validated_data['line1'], locality=serializer.validated_data['locality'], state=serializer.validated_data['state'], country=serializer.validated_data['country'], postcode=serializer.validated_data['postcode'], user=instance) instance.residential_address = address with transaction.atomic(): instance.save() instance.log_user_action( EmailUserAction.ACTION_POSTAL_ADDRESS_UPDATE.format( '{} {} ({})'.format(instance.first_name, instance.last_name, instance.email)), request) serializer = UserSerializer(instance) return Response(serializer.data) except serializers.ValidationError: print(traceback.print_exc()) raise except ValidationError as e: print(traceback.print_exc()) raise serializers.ValidationError(repr(e.error_dict)) except Exception as e: print(traceback.print_exc()) raise serializers.ValidationError(str(e))
def update_personal(self, request, *args, **kwargs): try: instance = self.get_object() serializer = PersonalSerializer(instance, data=request.data) serializer.is_valid(raise_exception=True) with transaction.atomic(): instance = serializer.save() instance.log_user_action( EmailUserAction.ACTION_PERSONAL_DETAILS_UPDATE.format( '{} {} ({})'.format( instance.first_name, instance.last_name, instance.email)), request) serializer = UserSerializer(instance) return Response(serializer.data) except serializers.ValidationError: print(traceback.print_exc()) raise except ValidationError as e: print(traceback.print_exc()) raise serializers.ValidationError(repr(e.error_dict)) except Exception as e: print(traceback.print_exc()) raise serializers.ValidationError(str(e))
def upload_id(self, request, *args, **kwargs): from wildlifecompliance.management.securebase_manager import ( SecureBaseUtils ) try: instance = self.get_object() SecureBaseUtils.timestamp_id_request(request) instance.upload_identification(request) with transaction.atomic(): instance.save() instance.log_user_action( EmailUserAction.ACTION_ID_UPDATE.format( '{} {} ({})'.format( instance.first_name, instance.last_name, instance.email)), request) # For any of the submitter's applications that have requested ID update, # email the assigned officer applications = instance.wildlifecompliance_applications.filter( submitter=instance, id_check_status=Application.ID_CHECK_STATUS_AWAITING_UPDATE, org_applicant=None, proxy_applicant=None ).exclude(customer_status__in=( Application.CUSTOMER_STATUS_ACCEPTED, Application.CUSTOMER_STATUS_DECLINED) ).order_by('id') if applications: officers = applications[0].licence_officers if applications[0].is_assigned: officers = [applications[0].assigned_officer] send_id_updated_notification( instance, applications, officers, request ) # assigned_officers = [application.assigned_officer.email # for application # in applications # if application.assigned_officer] # remove duplicate email addresses from assigned_officers list # assigned_officers = list(dict.fromkeys(assigned_officers)) # if len(assigned_officers) > 0: # send_id_updated_notification(instance, applications, assigned_officers, request) serializer = UserSerializer(instance, partial=True) return Response(serializer.data) except serializers.ValidationError: print(traceback.print_exc()) raise except ValidationError as e: print(traceback.print_exc()) raise serializers.ValidationError(repr(e.error_dict)) except Exception as e: print(traceback.print_exc()) raise serializers.ValidationError(str(e))
def update_profile(self, request, *args, **kwargs): try: instance = self.get_object() serializer = UserProfileSerializer(instance, data=request.data) serializer.is_valid(raise_exception=True) instance = serializer.save() serializer = UserSerializer(instance) return Response(serializer.data) except serializers.ValidationError: print(traceback.print_exc()) raise except ValidationError as e: print(traceback.print_exc()) raise serializers.ValidationError(repr(e.error_dict)) except Exception as e: print(traceback.print_exc()) raise serializers.ValidationError(str(e))
def list(self, request, **kwargs): if request.query_params: try: users = query_emailuser_by_args(**request.query_params) serializer = UserSerializer(users['items'], many=True) result = dict() result['data'] = serializer.data result['draw'] = int(users['draw']) result['recordsTotal'] = users['total'] result['recordsFiltered'] = users['count'] return Response(result) except Exception as e: return Response(e) else: try: return super(UserViewSet, self).list(request, **kwargs) except Exception as e: return Response(e)
def upload_id(self, request, *args, **kwargs): try: instance = self.get_object() instance.upload_identification(request) with transaction.atomic(): instance.save() instance.log_user_action( EmailUserAction.ACTION_ID_UPDATE.format( '{} {} ({})'.format(instance.first_name, instance.last_name, instance.email)), request) serializer = UserSerializer(instance, partial=True) return Response(serializer.data) except serializers.ValidationError: print(traceback.print_exc()) raise except ValidationError as e: print(traceback.print_exc()) raise serializers.ValidationError(repr(e.error_dict)) except Exception as e: print(traceback.print_exc()) raise serializers.ValidationError(str(e))
def get(self, request, format=None): serializer = UserSerializer(request.user, context={'request': request}) return Response(serializer.data)
def create_new_person(self, request, *args, **kwargs): print("create_new_person") with transaction.atomic(): try: email_user_id_requested = request.data.get('id', {}) email_address = request.data.get('email', '') if not email_address: first_name = request.data.get('first_name', '') last_name = request.data.get('last_name', '') email_address = generate_dummy_email(first_name, last_name) if email_user_id_requested: email_user_instance = EmailUser.objects.get(id=email_user_id_requested) email_user_instance.email = email_address else: email_user_instance = EmailUser.objects.create_user(email_address, '') request.data.update({'email': email_address}) email_user_serializer = SaveEmailUserSerializer( email_user_instance, data=request.data, partial=True) if email_user_serializer.is_valid(raise_exception=True): email_user_serializer.save() # Residential address # UPDATE user_id of residential address in order to save the residential address request.data['residential_address'].update({'user_id': email_user_serializer.data['id']}) residential_address_id_requested = request.data.get('residential_address', {}).get('id', {}) if residential_address_id_requested: residential_address_instance = Address.objects.get(id=residential_address_id_requested) address_serializer = SaveUserAddressSerializer( instance=residential_address_instance, data=request.data['residential_address'], partial=True) else: address_serializer = SaveUserAddressSerializer( data=request.data['residential_address'], partial=True) if address_serializer.is_valid(raise_exception=True): address_serializer.save() # Update relation between email_user and residential_address request.data.update({'residential_address_id': address_serializer.data['id']}) email_user = EmailUser.objects.get(id=email_user_serializer.instance.id) email_user_serializer = SaveEmailUserSerializer(email_user, request.data) if email_user_serializer.is_valid(): email_user_serializer.save() except serializers.ValidationError: print(traceback.print_exc()) raise except ValidationError as e: print(traceback.print_exc()) raise serializers.ValidationError(repr(e.error_dict)) except Exception as e: print(traceback.print_exc()) raise serializers.ValidationError(str(e)) email_user = EmailUser.objects.get(id=email_user_serializer.instance.id) email_user_serializer = UserSerializer(email_user,) return Response( email_user_serializer.data, status=status.HTTP_201_CREATED, headers=self.get_success_headers(email_user_serializer.data) )