def post(self, request): if request.user.is_authenticated: data = request.data user_image_url = data.get('user_image_url') q = Auth( configs.get('qiniu').get('AK'), configs.get('qiniu').get('SK')) username = str(request.user) try: user = User.objects.get(username__exact=username) except User.DoesNotExist: user = None user.user_image_url = user_image_url # 一定要加上这句话,要不然数据库数据不会更新 user.save(update_fields=['user_image_url']) user_image_url = q.private_download_url(user_image_url, expires=86400) token = Token.objects.get(user_id=user.id) new_obj = { # 'username': username, # 'token': token.key, 'user_image_url': user_image_url } return Response({ "stateCode": 200, "msg": new_obj }, status=HTTP_200_OK) else: return Response({"stateCode": 201, "msg": "您没有权限执行此操作"}, 201)
def get_dl_link(file): ticket = get_dl_ticket(file, configs.get('login'), configs.get('key')) url = 'https://api.openload.co/1/file/dl?file=%s&ticket=%s' % (file, ticket) response, content = h.request(url, 'GET') contect = json.loads(content) link = '' if (contect.get('status') == 200): link = contect.get('result').get('url') return link
def post(self, request, format=None): data = request.data filetype = data.get('filetype') # if request.user.is_authenticated: # 构建鉴权对象 q = Auth( configs.get('qiniu').get('AK'), configs.get('qiniu').get('SK')) # 生成图片名 salt = ''.join(random.sample(string.ascii_letters + string.digits, 8)) key = salt + '_' + str(int(time.time())) + '.' + filetype # 生成上传 Token,可以指定过期时间等 token = q.upload_token( configs.get('qiniu').get('bucket_name'), key, 3600) return Response({"stateCode": 200, "token": token, "key": key}, 200)
def post(self, request, format=None): data = request.data username = data.get('username') password = data.get('password') try: user = User.objects.get(username__exact=username) except User.DoesNotExist: user = None if user: if check_password(password, user.password): serializer = UserSerializer(user) new_data = serializer.data # 记忆已登录用户,作为用户是否拥有某项权限的认证 # self.request.session['user_username'] = user.username token = Token.objects.get(user_id=user.id) q = Auth( configs.get('qiniu').get('AK'), configs.get('qiniu').get('SK')) base_url = configs.get('qiniu').get('domain') + '/' user_image_url = q.private_download_url( base_url + new_data.get('user_image_url'), expires=86400) timeArray = time.strptime(str(user.last_login), "%Y-%m-%d %H:%M:%S") new_obj = { 'id': user.id, # 'password': user.password, 'username': new_data.get('username'), 'token': token.key, 'user_image_url': user_image_url, 'last_login': time.mktime(timeArray), } # print(time.mktime(timeArray)) user.last_login = time.strftime("%Y-%m-%d %H:%M:%S") user.save(update_fields=['last_login']) return Response({ "stateCode": 200, "msg": new_obj }, status=HTTP_200_OK) else: return Response({ "stateCode": 202, "msg": "密码不正确" }, 202) # 密码不正确 else: return Response({"stateCode": 201, "msg": "用户不存在"}, 201) # 用户不存在
async def receive_json(self, message, **kwargs): # 收到信息时调用 to_user = message.get('to_user') from_user = message.get('from_user') time = message.get('time') # 信息发送 length = len(ChatConsumer.chats[self.group_name]) if length == 2: # print('两个人') await self.channel_layer.group_send( self.group_name, { "type": "chat.message", "message": message.get('message'), "from_user": from_user, "to_user": to_user, "time": time, }, ) else: try: user = User.objects.get(id__exact=from_user) except User.DoesNotExist: user = None q = Auth( configs.get('qiniu').get('AK'), configs.get('qiniu').get('SK')) avatar_url = q.private_download_url(user.user_image_url, expires=3600) from_username = user.username await self.channel_layer.group_send( to_user, { "type": "push.message", "event": { 'message': message.get('message'), 'group': self.group_name, 'from_user': from_user, 'time': time, 'avatar_url': avatar_url, 'from_username': from_username, }, }, )
def init_path(self, place): home_path = configs.get("path").get("svn_home_path") # if self.svn.is_system_windows(): if platform.uname().system == "Windows": home_path = configs.get("path").get("win_svn_home_path") if not os.path.exists(home_path): home_path = "/mnt/e" print(f"home_path:{home_path}") self.code_home = os.path.join(home_path, "svn") from_folder = "发布登记表_UAT" if place.upper() == 'PRD': from_folder = "发布登记表_PRD" self.__register_folder = os.path.join(self.code_home, "1300_编码", from_folder) self.__svnup_dir = os.path.join(self.code_home, "1300_编码") code_beta_path = os.path.join(home_path, "yx_walk", "beta") self.__beta_path = os.path.join(code_beta_path, self.date_str + "beta_" + place)
}, }, ] WSGI_APPLICATION = 'onehomeServer.wsgi.application' # Database # https://docs.djangoproject.com/en/dev/ref/settings/#databases from config_default import configs DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': configs.get('db_name'), # 库名 'USER': configs.get('db_user'), # 用户名 'PASSWORD': configs.get('db_password'), # 密码 'HOST': configs.get('db_host'), # 数据库主机ip } } # Password validation # https://docs.djangoproject.com/en/dev/ref/settings/#auth-password-validators AUTH_PASSWORD_VALIDATORS = [ { 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', }, {
def setProcedurePath(self): if platform.uname().system == "Windows": self.procedure_path = configs.get("path").get( "win_svn_procedure_path") else: self.procedure_path = configs.get("path").get("svn_procedure_path")
def sort_out_list(request, data): # 构建鉴权对象 q = Auth(configs.get('qiniu').get('AK'), configs.get('qiniu').get('SK')) new_data = [] for obj in data: # 获取商品图片链接,整理成数组 imgs = [] private_url = q.private_download_url(obj.get('goods_img1'), expires=3600) imgs.append(private_url) if obj.get('goods_img2'): private_url = q.private_download_url(obj.get('goods_img2'), expires=3600) imgs.append(private_url) if obj.get('goods_img3'): private_url = q.private_download_url(obj.get('goods_img3'), expires=3600) imgs.append(private_url) if obj.get('goods_img4'): private_url = q.private_download_url(obj.get('goods_img4'), expires=3600) imgs.append(private_url) # 获取商品用户头像 product_username = obj.get('username') try: product_user = User.objects.get(username__exact=product_username) except User.DoesNotExist: product_user = None if product_user.user_image_url: product_avatar_url = q.private_download_url( product_user.user_image_url, expires=3600) else: product_avatar_url = None # 获取用户的收藏状态 try: c = Collection.objects.get(username=str(request.user), product_id=obj.get('id')) except Collection.DoesNotExist: c = None if c: collect_state = True else: collect_state = False # 对时间字符串进行整理 date = obj.get('c_time').split(".")[0] year = date.split("T")[0] time = date.split("T")[1] new_obj = { 'product_id': obj.get('id'), 'user_id': product_user.id, 'username': obj.get('username'), 'avatar_url': product_avatar_url, 'goods_price': obj.get('goods_price'), 'goods_img_url': imgs, 'collect_state': collect_state, 'title': obj.get('title'), 'description': obj.get('description'), 'time': year + " " + time } new_data.append(new_obj) msg = {'stateCode': 200, 'list': new_data} return msg