def detail_user(request, current_smod, user_id): try: user = User.objects.get(id=user_id) except User.DoesNotExist: return JsonResponse({'message': 'Not found!!!'}) if request.method == 'GET': serializer = UserSerializer(user) return JsonResponse({'data': serializer.data}) elif request.method == 'PUT': data = json.loads(json.dumps(request.data)) data['id'] = user_id data['password'] = user.password data['username'] = user.username data['email'] = user.email data['coin'] = user.coin data['sex'] = user.sex serializer = UserSerializer(user, data=data) if serializer.is_valid(): serializer.save() return JsonResponse({"data": serializer.data}) else: return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) elif request.method == 'DELETE': user.delete() return Response(status=status.HTTP_204_NO_CONTENT)
def change_coin(request, current_smod): if request.method == 'PUT': data = json.loads(json.dumps(request.data)) if current_smod.typemod == 0: return JsonResponse({'message': 'Not found!!!'}) try: user = User.objects.get(id=data['user_id']) except User.DoesNotExist: return JsonResponse({'message': 'Not found!!!'}) history = {} check = 0 if data['coin'] > 0: check = 1 data['id'] = user.id data['username'] = user.username data['password'] = user.password data['name'] = user.name data['email'] = user.email data['phone'] = user.phone data['address'] = user.address data['company'] = user.company data['sex'] = user.sex data['birthday'] = user.birthday data['avatar'] = user.avatar data['details'] = user.details data['status'] = user.status data['rank'] = user.rank data['social'] = user.social history['coin'] = abs(data['coin']) data['coin'] = int(user.coin) + int(data['coin']) serializer = UserSerializer(user, data=data) if serializer.is_valid(): serializer.save() if check == 1: history['type'] = 1 else: history['type'] = 0 history['user'] = user.id history['staff'] = current_smod.id history['status'] = True serializer = HistorySerializer(data=history) if serializer.is_valid(): serializer.save() return Response({'message': 'OK'}) else: return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def changepass(request, current_user): if request.method == 'PUT': data = json.loads(json.dumps(request.data)) data['oldpassword'] = str(data['oldpassword']) data['password'] = str(data['password']) if check_password_hash(current_user.password, data['oldpassword']) and data['password'] != '': data['id'] = current_user.id data['username'] = current_user.username data['password'] = generate_password_hash(data['password'], method='sha256') data['name'] = current_user.name data['email'] = current_user.email data['phone'] = current_user.phone data['address'] = current_user.address data['company'] = current_user.company data['sex'] = current_user.sex data['birthday'] = current_user.birthday data['coin'] = current_user.coin data['avatar'] = current_user.avatar data['status'] = current_user.status data['rank'] = current_user.rank data['details'] = current_user.details data['social'] = current_user.social response = {} captcha_rs = data['g-recaptcha-response'].encode("utf-8") url = "https://www.google.com/recaptcha/api/siteverify" params = { 'secret': settings.RECAPTCHA_SECRET_KEY, 'response': captcha_rs } verify_rs = requests.get(url, params=params, verify=True) verify_rs = verify_rs.json() response["status"] = verify_rs.get("success", False) response['message'] = verify_rs.get('error-codes', None) or "Unspecified error." if response['status'] == False: return JsonResponse({'message': 'Lỗi', 'status': 'error'}) serializer = UserSerializer(current_user, data=data) if serializer.is_valid(): serializer.save() return JsonResponse({ 'data': serializer.data, 'status': 'success' }) else: return JsonResponse({'message': 'Lỗi', 'status': 'error'}) return JsonResponse({'message': 'Lỗi', 'status': 'error'})
def duyetcoin(request, current_mod): if request.method == 'PUT': data = json.loads(json.dumps(request.data)) if current_mod.typemod == 0 and current_mod.rank == 0: return JsonResponse({'data': []}) history = History.objects.get(id=data['historyid']) if not history or history.status == True: return JsonResponse({'data': []}) current_user = User.objects.get(id=history.user) if not current_user or current_user.status == False: return JsonResponse({'data': []}) data['id'] = current_user.id data['username'] = current_user.username data['password'] = current_user.password data['name'] = current_user.name data['email'] = current_user.email data['phone'] = current_user.phone data['address'] = current_user.address data['company'] = current_user.company data['sex'] = current_user.sex data['birthday'] = current_user.birthday data['coin'] = int(current_user.coin) + int(history.coin) data['avatar'] = current_user.avatar data['status'] = current_user.status data['rank'] = current_user.rank data['details'] = current_user.details data['social'] = current_user.social serializer = UserSerializer(current_user, data=data) if serializer.is_valid(): serializer.save() data['id'] = history.id data['coin'] = history.coin data['type'] = history.type data['user'] = history.user data['staff'] = current_mod.id data['date'] = history.date data['status'] = True serializer = HistorySerializer(history, data=data) if serializer.is_valid(): serializer.save() return JsonResponse({'data': 'OK'}) else: return JsonResponse({'data': []}) else: return JsonResponse({'data': []})
def edit_user(request, current_user): if request.method == 'PUT': data = json.loads(json.dumps(request.data)) data['id'] = current_user.id data['name'] = current_user.name data['username'] = current_user.username data['email'] = current_user.email data['password'] = current_user.password data['rank'] = current_user.rank data['status'] = current_user.status data['coin'] = current_user.coin data['avatar'] = current_user.avatar serializer = UserSerializer(current_user, data=data) if serializer.is_valid(): serializer.save() serializer.data['password'] = '' return JsonResponse({'data': serializer.data}) else: return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def edit_avataruser(request, current_user): if request.method == 'POST': if not request.FILES: return JsonResponse({ 'data': 'Không có dữ liệu', 'status': 'error' }) matches = re.search('\w+\.(jpg|gif|png|jpeg)', request.FILES['avatar'].name) if not matches: return JsonResponse({ 'data': 'Không đúng định dạng ảnh', 'status': 'error' }) Y = str(datetime.datetime.now().year) m = str(datetime.datetime.now().month) d = str(datetime.datetime.now().day) name = request.FILES['avatar'].name.split('.') request.FILES['avatar'].name = current_user.id + '.' + name[len(name) - 1] save_path = os.path.join( settings.MEDIA_ROOT, 'profile/' + current_user.id + '/' + Y + '/' + m + '/' + d + '/', request.FILES['avatar'].name) # if default_storage.exists(save_path): # default_storage.delete(save_path) path = default_storage.save(save_path, request.FILES['avatar']) data = {} data['id'] = current_user.id data['username'] = current_user.username data['password'] = current_user.password data['name'] = current_user.name data['email'] = current_user.email data['phone'] = current_user.phone data['address'] = current_user.address data['company'] = current_user.company data['sex'] = current_user.sex data['birthday'] = current_user.birthday data['coin'] = current_user.coin data['status'] = current_user.status data['rank'] = current_user.rank data['details'] = current_user.details data[ 'avatar'] = 'https://www.mappy.com.vn/media/profile/' + current_user.id + '/' + Y + '/' + m + '/' + d + '/' + path.split( '/')[10] data['social'] = current_user.social serializer = UserSerializer(current_user, data=data) if serializer.is_valid(): serializer.save() serializer.data['password'] = '' return JsonResponse({'data': data['avatar'], 'status': 'success'}) else: return JsonResponse({ 'data': 'Lỗi hệ thống! Vui lòng liên hệ với quản trị viên để được hỗ trợ sớm nhất!', 'status': 'error' })