def destroy(self, request, *args, **kwargs): instance = self.get_object() if instance is None: return AlpsRestResponse.fail(status=status.HTTP_403_FORBIDDEN) user = get_user_from_request(request) message = '%s soft deleted %s' % (user.username, str(instance)) log_deletion(user, instance, message) self.perform_destroy(instance) return AlpsRestResponse.success(status=status.HTTP_202_ACCEPTED)
def save_model(self, request, obj, form, change): user = get_user_from_request(request) obj.modified_by = user obj.modified_time = now() if not change: obj.owner = user obj.created_by = user obj.created_time = now() super().save_model(request, obj, form, change)
def create(self, request, *args, **kwargs): serializer = self.get_serializer(data=request.data) serializer.is_valid(raise_exception=True) self.perform_create(serializer) headers = self.get_success_headers(serializer.data) instance = serializer.instance user = get_user_from_request(request) message = '%s created %s' % (user.username, str(instance)) log_addition(user, instance, message) return AlpsRestResponse.success(serializer.data, status=status.HTTP_201_CREATED, headers=headers)
def current(self, request): user = get_user_from_request(request) if user.is_active: profile = get_user_profile(user) if profile is None: return AlpsRestResponse.success({"roles": 'guest'}) serializer = self.get_serializer(profile, many=False) return AlpsRestResponse.success({ "user": serializer.data, "roles": get_user_roles(user) }) else: return AlpsRestResponse.fail(status=status.HTTP_403_FORBIDDEN, error_message='inactive user')
def avatar(self, request, pk=None): file = request.FILES['file'] if file is None: return AlpsRestResponse.fail(error_code=-2, error_message="File data not found", status=status.HTTP_400_BAD_REQUEST) user = get_user_from_request(request) if user is None: return AlpsRestResponse.fail(status=status.HTTP_404_NOT_FOUND) if not LocalFile.is_image(file.content_type): logger.debug( "User %s change avatar failed, file %s is not supported" % (user.username, file.name)) return AlpsRestResponse.fail( error_code=-2, error_message="Unsupperted file type", status=status.HTTP_415_UNSUPPORTED_MEDIA_TYPE) profile = self.get_object() if profile.auth_user != user: return AlpsRestResponse.fail( error_code=-2, error_message="can't change other user's avatar", status=status.HTTP_403_FORBIDDEN) # delete old one if exist if profile.avatar: LocalFile.delete(MEDIA_ROOT + profile.avatar) destination = LocalFile.save(request, to_path=MEDIA_PATH_AVATAR) profile.avatar = destination profile.save() if destination is None: return AlpsRestResponse.fail( error_code=-2, error_message="Failed save file data", status=status.HTTP_500_INTERNAL_SERVER_ERROR) log_change( user, user, "%s has changed avatar with file %s " % (user.username, profile.avatar)) logger.info("%s has changed avatar with file %s " % (user.username, profile.avatar)) return AlpsRestResponse.success( UserProfileSerializer(profile, context={ "request": request }).data)
def update(self, request, *args, **kwargs): partial = kwargs.pop('partial', False) instance = self.get_object() user = get_user_from_request(request) serializer = self.get_serializer(instance, data=request.data, partial=partial) serializer.is_valid(raise_exception=True) message = '%s changed %s' % (user.username, str(instance)) log_change(user, instance, message) self.perform_update(serializer) if getattr(instance, '_prefetched_objects_cache', None): # If 'prefetch_related' has been applied to a queryset, we need to # forcibly invalidate the prefetch cache on the instance. instance._prefetched_objects_cache = {} return AlpsRestResponse.success(serializer.data)