def update_details(self, request): if not request.body: return HttpResponseBadRequest(self.json_msg('all', "There was nothing submitted")) body = json.loads(request.body) user = User.objects.get(auth_token=body['auth']) if 'current-password' not in body: return HttpResponseBadRequest(self.json_msg('current-password', "No password provided")) if not user.check_password(body['current-password']): return HttpResponseBadRequest(self.json_msg('current-password', "The password provided isn't correct.")) # Need to explicitly set password to have Django hash it if 'password' in body and body['password']: user.set_password(str(body['password'])) body.pop('password', None) user.save() serializer = self.get_serializer( instance=user, data=body, partial=True ) serializer.is_valid(raise_exception=True) serializer.save() return Response(serializer.data)
def get(self, request): todo = favoriteBooks.objects.all() serializer = favoritesSerializer(data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def attempt_login(self, request): if not request.body: return self.error_response( status.HTTP_400_BAD_REQUEST, 'No request body provided', ) body = json.loads(request.body) if 'api_url' not in body: return self.error_response( status.HTTP_400_BAD_REQUEST, 'No api_url provided', ) if 'username' not in body: return self.error_response( status.HTTP_400_BAD_REQUEST, 'No username provided', ) if 'password' not in body: return self.error_response( status.HTTP_400_BAD_REQUEST, 'No password provided', ) mds_login_response = requests.post( self.get_mds_url(body['api_url'], 'login/'), data={ 'username': body['username'], 'password': body['password'], }, ) response_data = {} # If login was successful, update the mds instance info if mds_login_response.status_code == status.HTTP_200_OK: api_key = mds_login_response.body['api_key'] mds_instance = models.MDSInstance.objects.get( user=self.request.user, ) mds_instance_data = { 'api_key': api_key, 'api_url': body['api_url'], } serializer = self.get_serializer( instance=mds_instance, data=mds_instance_data, partial=True, ) serializer.is_valid(raise_exception=True) serializer.save() response_data['mds_instance'] = serializer.data response_data['mds_status_code'] = mds_login_response.status_code return Response(response_data)
def save(self): global _appconfig try: utils.makedirs(const.ConfigDir) serializer.save(const.ConfigFile, _appconfig) log.debug("Saved configuration") # Ensure it is secured. utils.secure_file(const.ConfigFile) except Exception as e: log.error("Unable to save configuration: " + str(e)) raise e
def load_preprocessed_data(self): data = {} for counter, filepath in enumerate(os.listdir('../data/tmp'), 1): logger.info('Part {} loading'.format(counter)) update = serializer.load('../data/tmp/' + filepath) if not update: logger.error( 'Part {} loading error. Tereminate process'.format( counter)) break for key, value in update.items(): serializer.save(value, '../data/processed/' + str(key) + '.dat') logger.info('Part {} loaded'.format(counter)) return data
def partial_bulk_update(self, request, *args, **kwargs): queryset = self.filter_queryset(self.get_queryset()) if not request.body: return Response(status=status.HTTP_400_BAD_REQUEST) serializer = self.get_serializer(instance=queryset, data=json.loads(request.body), many=True, partial=True) serializer.is_valid(raise_exception=True) serializer.save() return Response(serializer.data)
def preprocess_data(self): BATCH_SIZE = 7000 data = {} counter = 0 for counter, item in enumerate(self.dl.content(), 1): data[item[0]] = item[1] if counter % BATCH_SIZE == 0: filename = '../data/tmp/' + str(counter // BATCH_SIZE) + '.dat' if not serializer.save(data, filename): return False data = {} if counter % BATCH_SIZE != 0: filename = '../data/tmp/' + str(counter // BATCH_SIZE + 1) + '.dat' serializer.save(data, filename) logger.info('Data handle finished. {} files processed'.format(counter)) return True
def user_list(request): """ Api request for all users in the system, check. Extract Object from db get call / post create one. """ if request.method == 'GET': users = User.objects.all() serializer = Userserializer(users, many=True) return Response(serializer.data) elif request.method == 'POST': serializer = Userserializer(data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def partial_bulk_update(self, request, *args, **kwargs): queryset = self.filter_queryset(self.get_queryset()) if not request.body: return Response(status=status.HTTP_400_BAD_REQUEST) serializer = self.get_serializer( instance=queryset, data=json.loads(request.body), many=True, partial=True ) serializer.is_valid(raise_exception=True) serializer.save() return Response(serializer.data)
def update_details(self, request): if not request.body: return self.error_response( status.HTTP_400_BAD_REQUEST, 'There was nothing submitted') body = json.loads(request.body) user = User.objects.get(auth_token=body['auth']) if 'current-password' not in body: return self.error_response( status.HTTP_400_BAD_REQUEST, 'No password provided') if not user.check_password(body['current-password']): return self.error_response( status.HTTP_400_BAD_REQUEST, "The password provided isn't correct.") # Need to explicitly set password to have Django hash it if 'password' in body and body['password']: user.set_password(str(body['password'])) body.pop('password', None) user.save() serializer = self.get_serializer( instance=user, data=body, partial=True ) serializer.is_valid(raise_exception=True) serializer.save() user_details = { 'id': user.pk, 'is_superuser': user.is_superuser, 'first_name': user.first_name, 'last_name': user.last_name, 'username': user.username, 'email': user.email, } return JsonResponse({'user': user_details})
def update_details(self, request): if not request.body: return self.error_response(status.HTTP_400_BAD_REQUEST, 'There was nothing submitted') body = json.loads(request.body) user = User.objects.get(auth_token=body['auth']) if 'current-password' not in body: return self.error_response(status.HTTP_400_BAD_REQUEST, 'No password provided') if not user.check_password(body['current-password']): return self.error_response(status.HTTP_400_BAD_REQUEST, "The password provided isn't correct.") # Need to explicitly set password to have Django hash it if 'password' in body and body['password']: user.set_password(str(body['password'])) body.pop('password', None) user.save() serializer = self.get_serializer(instance=user, data=body, partial=True) serializer.is_valid(raise_exception=True) serializer.save() user_details = { 'id': user.pk, 'is_superuser': user.is_superuser, 'first_name': user.first_name, 'last_name': user.last_name, 'username': user.username, 'email': user.email, } return JsonResponse({'user': user_details})
def CreateIDF(self): logger.info('Creating IDF') data_dir = os.path.join(os.path.dirname(__file__), '../data/') data_dir = os.path.join(data_dir, 'tmp') data_list = os.listdir(data_dir) if not data_list: raise Exception('Data not preprocessed') self._idf = Counter() for cnt, filepath in enumerate(data_list, 1): logger.info('Part {} processing'.format(cnt)) for word_list in serializer.load(os.path.join(data_dir, filepath)).values(): self._idf.update(word_list) idf_filepath = os.path.join(data_dir, 'idf.dat') if os.path.isfile(idf_filepath): ans = input( 'file {} alredy exist, replace it? (Y/N)'.format(idf_filepth)) if ans.lower() != 'y': return serializer.save(self._idf, idf_filepath)
""" if request.method == 'DELETE': messaging = Messaging.objects.filter(message_id=message_id) serializer = Messagingserializer(messaging, many=True) structure = serializer.data if len(structure) == 0: return HttpResponse({'error: The message id doesnt exits'}, status=204) else: check = structure[0]['message_id'] if check == message_id: messaging.delete() return HttpResponse({'The message deleted'},status=200) @csrf_exempt @api_view(['POST']) @permission_classes((AllowAny,)) def create_message(request): """ create message """ if request.method == 'POST': data = JSONParser().parse(request) serializer = Messagingserializer(data=data) if serializer.is_valid(): serializer.save() return JsonResponse(serializer.data, status=201) return JsonResponse(serializer.errors, status=400)
def perform_create(self, serializer): serializer.save(owner=self.request.user)
def saveGame(self, save_name): serializer.save(self.world, "saves/" + save_name + ".sav") self.gui.combat_log.printMessage("Game saved.")
def saveWorld(self, world): serializer.save(world, "saves/replaystart.sav")
def createOrder(self): serializer.save(owner=self.request.user)
def saveGame(self, save_name): self.world.refresh() serializer.save(self.world, "saves/" + save_name + ".sav") print("* Game saved!")
def saveCommands(self): serializer.save(self.commands, "saves/replaycommands.replay")