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 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 userfalse(request, current_smod): if request.META['REQUEST_METHOD'] == 'GET': users = User.objects.filter(status=False) if users: serializer = UserSerializer(users, many=True) return JsonResponse({"data": serializer.data}) return JsonResponse({'data': []})
def list_user(request, current_smod): if request.META['REQUEST_METHOD'] == 'GET': users = User.objects.all() if users: serializer = UserSerializer(users, many=True) return JsonResponse({'data': serializer.data}) return JsonResponse({'data': []})
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 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 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 confirm_node(request, current_smod): data = json.loads(json.dumps(request.data)) try: ktra = Mod.objects.filter(type=current_smod.id) realestatenode = Realestatenode.objects.get( id=data['realestatenode_id'], modid__in=ktra, status=False) except: return JsonResponse({'message': 'Not result!!!'}) current_user = User.objects.get(id=realestatenode.userid) coin = Coin.objects.get(vip=realestatenode.vip) realcoins = coin.coin * (realestatenode.timeto - realestatenode.timefrom).days if current_user.coin < realcoins: return JsonResponse({'data': 'Khong du coin'}) if request.method == 'PUT': data['id'] = realestatenode.id data['title'] = realestatenode.title data['latitude'] = realestatenode.latitude data['longitude'] = realestatenode.longitude data['price'] = realestatenode.price data['pricem2'] = realestatenode.pricem2 data['area'] = realestatenode.area data['tinh'] = realestatenode.tinh data['huyen'] = realestatenode.huyen data['xa'] = realestatenode.xa data['duong'] = realestatenode.duong data['details_address'] = realestatenode.details_address data['address'] = realestatenode.address data['huong'] = realestatenode.huong data['sophongngu'] = realestatenode.sophongngu data['tang'] = realestatenode.tang data['sotang'] = realestatenode.sotang data['rongtien'] = realestatenode.rongtien data['rongduong'] = realestatenode.rongduong data['details'] = realestatenode.details data['status'] = True data['thumbs'] = realestatenode.thumbs data['anh360'] = realestatenode.anh360 data['panorama_image'] = realestatenode.panorama_image data['video'] = realestatenode.video data['tenduan'] = realestatenode.tenduan data['tenlienhe'] = realestatenode.tenlienhe data['diachi'] = realestatenode.diachi data['dienthoai'] = realestatenode.dienthoai data['email'] = realestatenode.email data['rank'] = realestatenode.rank data['timefrom'] = realestatenode.timefrom data['timeto'] = realestatenode.timeto data['typenode'] = realestatenode.typenode data['vip'] = realestatenode.vip data['modname'] = realestatenode.modname data['timecreate'] = realestatenode.timecreate data['timemodify'] = realestatenode.timemodify data['duanid'] = realestatenode.duanid data['modid'] = realestatenode.modid data['type'] = realestatenode.type data['userid'] = realestatenode.userid serializer = RealestatenodeSerializer(realestatenode, data=data) if serializer.is_valid(): serializer.save() 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) - realcoins 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() return JsonResponse({'message': 'OK'}) 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' })
def postagain(request, current_user, node_id): if request.method == 'POST': data = json.loads(json.dumps(request.data)) realestatenode = Realestatenode.objects.get(id=node_id, userid=current_user.id) if not realestatenode: return JsonResponse({'data': []}) #Tinh gia data['vip'] coin = Coin.objects.get(vip=data['vip']) timef = datetime.datetime.strptime(data['timefrom'], '%Y-%m-%d %H:%M:%S') timet = datetime.datetime.strptime(data['timeto'], '%Y-%m-%d %H:%M:%S') if (timet - timef).days <= 0 or (timet - realestatenode.timeto).days <= 0: return JsonResponse({'data': 'Sai thoi gian'}) if (timef - realestatenode.timeto).days > 0: realcoins = coin.coin * (timet - timef).days else: realcoins = coin.coin * (timet - realestatenode.timeto).days if current_user.coin < realcoins: return JsonResponse({'data': 'Khong du coin'}) data['id'] = realestatenode.id data['title'] = realestatenode.title data['latitude'] = realestatenode.latitude data['longitude'] = realestatenode.longitude data['price'] = realestatenode.price data['pricem2'] = realestatenode.pricem2 data['area'] = realestatenode.area data['tinh'] = realestatenode.tinh data['huyen'] = realestatenode.huyen data['xa'] = realestatenode.xa data['duong'] = realestatenode.duong data['details_address'] = realestatenode.details_address data['address'] = realestatenode.address data['huong'] = realestatenode.huong data['sophongngu'] = realestatenode.sophongngu data['tang'] = realestatenode.tang data['sotang'] = realestatenode.sotang data['rongtien'] = realestatenode.rongtien data['rongduong'] = realestatenode.rongduong data['details'] = realestatenode.details data['status'] = realestatenode.status data['thumbs'] = realestatenode.thumbs data['anh360'] = realestatenode.anh360 data['panorama_image'] = realestatenode.panorama_image data['video'] = realestatenode.video data['tenduan'] = realestatenode.tenduan data['tenlienhe'] = realestatenode.tenlienhe data['diachi'] = realestatenode.diachi data['dienthoai'] = realestatenode.dienthoai data['email'] = realestatenode.email data['rank'] = realestatenode.rank data['typenode'] = realestatenode.typenode data['modname'] = realestatenode.modname data['timecreate'] = realestatenode.timecreate data['timemodify'] = datetime.datetime.now().strftime( '%Y-%m-%d %H:%M:%S') data['duanid'] = realestatenode.duanid data['modid'] = realestatenode.modid data['type'] = realestatenode.type data['userid'] = realestatenode.userid serializer = RealestatenodeSerializer(realestatenode, data=data) if serializer.is_valid(): serializer.save() 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['details'] = current_user.details data['sex'] = current_user.sex data['birthday'] = current_user.birthday data['coin'] = int(current_user.coin) - realcoins data['avatar'] = current_user.avatar data['status'] = current_user.status data['rank'] = current_user.rank data['social'] = current_user.social serializer = UserSerializer(current_user, data=data) if serializer.is_valid(): serializer.save() return JsonResponse({'data': 'OK'}) return JsonResponse({'data': 'error'})
def info(request, current_user): if request.META['REQUEST_METHOD'] == 'GET': current_user.password = '' serializer = UserSerializer(current_user) result = serializer.data return Response(result)