def post(self, request): # get url_name to check permission url_name = return_url_name(request.path) # check permission if not has_permission(request.user.role_id, url_name, request.method): return JsonResponse({'error': 'No permission'}, status=403) # check if request is ajax elif request.is_ajax(): form = self.form_class(request.POST) # check if form is valid if form.is_valid(): instance = form.save(commit=False) # model_owner = add user instance.model_owner = request.user instance.save() # send to client return JsonResponse({'Added model': instance.model_name}, status=200) else: # Make error string str_error = '' for field, errors in form.errors.items(): # add error field str_error += field + ": " # add all error in field for error in errors: str_error += error + ', ' return JsonResponse({"error": str_error[:-2]}, status=400) # request not ajax else: return JsonResponse({"Error": "Something when terribly wrong"}, status=400)
def post(self, request, id): """Update data""" url_name = return_url_name(request.path) if not has_permission(request.user.role_id, url_name, request.method): return JsonResponse({"error": "No permission"}, status=403) if request.is_ajax: update_data = queries.get_model_train_data_by_id(id) if not update_data: return HttpResponse('404 not found') if request.user != update_data.added_by: return JsonResponse({"error": "No permission"}, status=403) form = self.form_class(request.POST) if form.is_valid(): instance = form.save(commit=False) instance.added_by = request.user instance.save() # send to client side. return JsonResponse({"Edit data": { "instance": "Success" }}, status=200) else: str_error = '' for field, errors in form.errors.items(): str_error += field + ": " for error in errors: str_error += error + ', ' return JsonResponse({"error": str_error[:-2]}, status=400) else: return JsonResponse({"error": "Something went terribly wrong"}, status=400)
def post(self, request): """ Add category :param request: :return: Category name if added, otherwise error """ # check permission url_name = return_url_name(request.path) if not has_permission(request.user.role_id, url_name, request.method): return JsonResponse({"error": "No permission"}, status=400) # get request, check is ajax if request.is_ajax: form = self.form_class(request.POST) if form.is_valid(): instance = form.save() # send to client side. return JsonResponse({"Add data": instance.name}, status=200) else: # send form's error str_error = '' for field, errors in form.errors.items(): str_error += field + ": " for error in errors: str_error += error + ', ' return JsonResponse({"error": str_error[:-2]}, status=400) return JsonResponse({"error": "Something went terribly wrong"}, status=400)
def delete(self, request, id): """delete data""" url_name = return_url_name(request.path) # check permission if not has_permission(request.user.role_id, url_name, request.method): return JsonResponse({"error": "No permission"}, status=403) # check request is ajax elif request.is_ajax: # get data by id delete_data = queries.get_data_by_id(id) # check owner if request.user != delete_data.data_owner: return JsonResponse({"error": "No permission"}, status=403) # not found if not delete_data: return JsonResponse({"error": "Not found"}, status=400) # delete data delete_data.delete() # delete image if delete_data.data_avatar: # join path delete_image = os.path.join("media", str(delete_data.data_avatar)) # remove try: os.remove(delete_image) except FileNotFoundError: pass return JsonResponse({}, status=200) else: return JsonResponse({"error": "Something went terribly wrong"}, status=400)
def post(self, request, pk): # check permission url_name = return_url_name(request.path) if not has_permission(request.user.role_id, url_name, request.method): return JsonResponse({"error": "No permission"}, status=400) if request.is_ajax: # get data_category by id update_data = queries.get_data_category_by_filter( data_category_id=pk) form = self.form_class(request.POST, instance=update_data) if form.is_valid(): instance = form.save() ser_instance = serializers.serialize('json', [ instance, ]) # send to client side. return JsonResponse({"Edit data": { "instance": ser_instance }}, status=200) else: str_error = '' for field, errors in form.errors.items(): str_error += field + ": " for error in errors: str_error += error + ', ' return JsonResponse({"error": str_error[:-2]}, status=400) return JsonResponse({"error": "Something went terribly wrong"}, status=400)
def post(self, request): """Create data""" # get url name url_name = return_url_name(request.path) # check permission if not has_permission(request.user.role_id, url_name, request.method): return JsonResponse({"error": "No permission"}, status=400) # check if request is ajax if request.is_ajax: # get data from form form = self.form_class(request.POST, request.FILES) # check valid form if form.is_valid(): instance = form.save(commit=False) # set owner = request user instance.data_owner = request.user instance.save() # send to client side. return JsonResponse({'instance': instance.name}, status=200) else: # create error string str_error = '' for field, errors in form.errors.items(): # get all field that error str_error += field + ": " # add all error of that field for error in errors: str_error += error + ', ' # return to client return JsonResponse({"error": str_error[:-2]}, status=400) # if request not ajax return JsonResponse({"error": "Something went terribly wrong"}, status=400)
def post(self, request): """ Create record in model_train_data :param request: :return: Record if created, otherwise error """ url_name = return_url_name(request.path) # check permission if not has_permission(request.user.role_id, url_name, request.method): return JsonResponse({'error': 'No permission'}, status=403) # check request is ajax if request.is_ajax(): form = self.form_class(request.POST) if form.is_valid(): # save record instance = form.save(commit=False) instance.added_by = request.user instance.save() # instance to json ser_instance = serializers.serialize('json', [ instance, ]) # send to client return JsonResponse({'Added data': ser_instance}, status=200) else: # error: Send form's error str_error = '' for field, errors in form.errors.items(): str_error += field + ": " for error in errors: str_error += error + ', ' return JsonResponse({"error": str_error[:-2]}, status=400) else: return JsonResponse({"Error": "Something when terribly wrong"}, status=400)
def delete(self, request, id): """ Delete Model train data record :param request: :param id: id to delete :return: None if delete, otherwise error """ url_name = return_url_name(request.path) # check permission if not has_permission(request.user.role_id, url_name, request.method): return JsonResponse({"error": "No permission"}, status=403) if request.is_ajax: # get ModelTrainData record to delete delete_data = queries.get_model_train_data_by_id(id) # if delete data not found if not delete_data: return JsonResponse({"error": "Not found"}, status=404) # check if request user is owner if request.user != delete_data.added_by: return JsonResponse({"error": "No permission"}, status=403) # pass all: delete data delete_data.delete() # send to client return JsonResponse({}, status=200) else: # send error return JsonResponse({"error": "Something went terribly wrong"}, status=400)
def post(self, request, id): """Update data""" url_name = return_url_name(request.path) if not has_permission(request.user.role_id, url_name, request.method): return JsonResponse({"error": "No permission"}, status=400) if request.is_ajax and request.method == "POST": update_data = queries.get_data_by_id(id) if request.user != update_data.data_owner: return JsonResponse({"error": "No permission"}, status=400) delete_image = None if update_data.data_avatar: delete_image = os.path.join("media", str(update_data.data_avatar)) form = self.form_class(request.POST, request.FILES, instance=update_data) if form.is_valid(): # if update image if request.FILES: if delete_image: # if delete_image_exist try: os.remove(delete_image) except FileNotFoundError: pass instance = form.save() instance = serializers.serialize('json', [ instance, ]) # convert to json to change value of foreign key, null to None and Analyzed to string instance = json.loads(instance) instance[0]["fields"]['data_category'] = str( queries.get_data_category_by_filter( data_category_id=instance[0]["fields"] ['data_category'])) instance[0]["fields"]['best_analyzed_model'] = str( queries.get_ai_model_by_id( id=instance[0]["fields"]['best_analyzed_model'])) if not instance[0]["fields"]['analyzed']: instance[0]["fields"]['analyzed'] = "False" else: instance[0]["fields"]['analyzed'] = "True" for key, value in instance[0]["fields"].items(): if value is None: instance[0]["fields"][key] = "None" # back to string to send to client instance = json.dumps(instance) # send to client side. return JsonResponse({'instance': instance}, status=200) else: str_error = '' for field, errors in form.errors.items(): str_error += field + ": " for error in errors: str_error += error + ', ' return JsonResponse({"error": str_error[:-2]}, status=400) else: return JsonResponse({"error": "Something went terribly wrong"}, status=400)
def delete(self, request, id): """delete data""" url_name = return_url_name(request.path) if not has_permission(request.user.role_id, url_name, request.method): return JsonResponse({"error": "No permission"}, status=403) elif request.is_ajax: delete_data = queries.get_model_train_data_by_id(id) if request.user != delete_data.added_by: return JsonResponse({"error": "No permission"}, status=403) # fix sau khi co du model if not delete_data: return JsonResponse({"error": "Not found"}, status=404) delete_data.delete() return JsonResponse({}, status=200) else: return JsonResponse({"error": "Something went terribly wrong"}, status=400)
def post(self, request, id): """ Update ModelTrainData record :param request: :param id: id to update :return: Success message update, otherwise error """ url_name = return_url_name(request.path) # check permission if not has_permission(request.user.role_id, url_name, request.method): return JsonResponse({"error": "No permission"}, status=403) # check request is ajax if request.is_ajax: # get update data update_data = queries.get_model_train_data_by_id(id) # not found if not update_data: return HttpResponse('404 not found') # check if request user is owner if request.user != update_data.added_by: return JsonResponse({"error": "No permission"}, status=403) # get form form = self.form_class(request.POST) if form.is_valid(): # update instance = form.save(commit=False) instance.added_by = request.user instance.save() # send to client side. return JsonResponse({"Edit data": { "instance": "Success" }}, status=200) else: # if error send error to client str_error = '' for field, errors in form.errors.items(): str_error += field + ": " for error in errors: str_error += error + ', ' return JsonResponse({"error": str_error[:-2]}, status=400) # request is not ajax else: return JsonResponse({"error": "Something went terribly wrong"}, status=400)
def post(self, request): url_name = return_url_name(request.path) if not has_permission(request.user.role_id, url_name, request.method): return JsonResponse({'error': 'No permission'}, status=403) if request.is_ajax(): form = self.form_class(request.POST) if form.is_valid(): instance = form.save(commit=False) instance.added_by = request.user instance.save() return JsonResponse({'Added data': "Hello"}, status=200) else: str_error = '' for field, errors in form.errors.items(): str_error += field + ": " for error in errors: str_error += error + ', ' return JsonResponse({"error": str_error[:-2]}, status=400) else: return JsonResponse({"Error": "Something when terribly wrong"}, status=400)
def post(self, request, id): # get url_name url_name = return_url_name(request.path) # check permission if not has_permission(request.user.role_id, url_name, request.method): return JsonResponse({'error': 'No permission'}, status=403) # check if request is ajax if request.is_ajax(): # get update model update_model = queries.get_ai_model_by_id(id) # check if model exist if update_model: # check if user request is the owner if update_model.model_owner == request.user: form = self.form_class(request.POST, instance=update_model) # check if form is valid if form.is_valid(): instance = form.save() return JsonResponse( {'Edit model': instance.model_name}, status=200) else: # make error string str_error = '' for field, errors in form.errors.items(): # add all error fields str_error += field + ": " # add all error of one field for error in errors: str_error += error + ', ' return JsonResponse({"error": str_error[:-2]}, status=400) else: # user is not the owner return JsonResponse({'error': 'No permission'}, status=403) # if update data not found else: return HttpResponse("404 Not found") # request not ajax else: return JsonResponse({'error': 'Something when terribly wrong'})
def post(self, request): url_name = return_url_name(request.path) if not has_permission(request.user.role_id, url_name, request.method): messages.error(request, "No permission") return redirect("/change_password/") password = request.POST.get('password') new_password = request.POST.get('new_password') cnf_password = request.POST.get('cnf_password') if new_password != cnf_password: messages.error(request, "Password does not match") return redirect("/change_password/") user = queries.get_user_by_username(request.user.username) # check password user send if not user.check_password(password): messages.error(request, "Invalid password") return redirect("/change_password/") # set new password user.set_password(new_password) user.save() # update session update_session_auth_hash(request, user) messages.info(request, "Change password successfully") return redirect("/change_password")
def delete(self, request, id): url_name = return_url_name(request.path) # check permission before delete if not has_permission(request.user.role_id, url_name, request.method): return JsonResponse({'error': 'No permission'}, status=403) # check if request is ajax if request.is_ajax(): # get delete model delete_model = queries.get_ai_model_by_id(id) # check if model exist if delete_model: # check user request == owner if delete_model.model_owner == request.user: delete_model.delete() return JsonResponse({}, status=200) # no permission if not owner else: return JsonResponse({'error': 'No permission'}, status=403) # delete model not found else: return HttpResponse("404 Not found") # request not ajax else: return JsonResponse({'error': 'Something when terribly wrong'})