def save(cls, request_data, instance=None): try: file = request_data.pop('file', None) data = instance if instance else Data() for key in request_data: setattr(data, key, request_data[key]) with transaction.atomic(): #save info data data.save() ArangoDataService.save_post(data.__dict__) # save file to local if file: extension = data.name[data.name.rfind('.'):] file_name = str(data.id)+extension local_file_path = join(settings.MEDIA_ROOT, file_name) with open(local_file_path, 'wb') as dest: for chunk in file.chunks(): dest.write(chunk) #update memory used from SocialNetwork_API.services import UserService user = UserService.get_user(data.user_id) user_data = {'memory_used': user.memory_used + data.capacity} UserService.save(instance=user, user_data=user_data) return data except Exception as exception: cls.log_exception(exception) raise exception
def create(self, validated_data): # for key in ['groups', 'user_permission']: # if key in validated_data: # del validated_data[key] user = UserService.save(validated_data) return user
def update(self, instance, validated_data): return UserService.save(validated_data, instance)