def secondary(request): """ 二手信息 :param request: :return: """ resp = {'code': 200, 'msg': '操作成功~', 'data': {}} page_size = 20 p = int(request.GET.get("p")) article_info = models.Article.objects.filter(status=1, category=3).values('nid', 'title', 'desc', 'create_time', 'comm_count', 'up_count', 'category__title', 'user__avatar', 'user__username').order_by( "-weight", "-create_time")[page_size * p:page_size * p + page_size] for item in article_info: date = datetime.datetime.strftime(item.get('create_time'), "%Y-%m-%d %H:%M") item["create_time"] = date image = models.Article.objects.filter(nid=item.get("nid")).values("img__image").first() if image.get("img__image") and image.get("img__image") != "undefined": item["img__image"] = build_url('images/' + image.get("img__image")) resp['data']['has_more'] = 0 if len(article_info) < page_size else 1 resp['data']['article'] = list(article_info) return JsonResponse(resp)
def advertise(request): """ 首页 :param request: :return: """ resp = {'code': 200, 'msg': '操作成功~', 'data': {}} user_id = request.META.get('HTTP_AUTHORIZATION') openid = request.META.get("HTTP_IDMD5") if request.method == "POST": nid = request.POST.get("nid") ad_detail = models.Advertise.objects.filter(status=1, nid=nid).values("nid", "title", "content").first() ad_detail_image = models.AdImage.objects.filter(status=1, ad=ad_detail.get("nid")).values("image") ad_image = [] for item in ad_detail_image: ad_image.append(build_url('advertise/' + item.get("image"))) resp['data']['ad_detail'] = ad_detail resp["data"]["ad_image"] = ad_image return JsonResponse(resp) ad_info = models.Advertise.objects.filter(status=1).values('nid', 'title', 'image', 'url', "place", "update_time", 'desc', 'content').order_by("-weight") Inform = models.Inform.objects.filter(status=1).values("content").first() if user_id != '0' and openid != '0': user_info = models.Customer.objects.filter(status=1, openid=openid, nid=user_id).values("telephone", "qq", "wechat", "email" ).first() if user_info.get("telephone") or user_info.get("qq") or user_info.get("wechat"): resp['data']['phone'] = True else: resp['data']['phone'] = False ad_head = [] ad_center = [] for item in ad_info: item["pic_url"] = build_url('advertise/' + item.get('image')) item['update_time'] = datetime.datetime.strftime(item.get('update_time'), "%Y-%m-%d %H:%M") if item.get("place") == "0": ad_head.append(item) elif item.get("place") == "1": ad_center.append(item) resp['data']['advertise'] = ad_head resp['data']["ad_center"] = ad_center resp["data"]["info"] = Inform return JsonResponse(resp)
def second(request): """ 查看二手详情 :param request: :return: """ resp = {'code': 200, 'msg': '操作成功', 'data': {}} if request.method == "POST": # 查看回复评论 comment_list = [] cid = request.POST.get("cid") comment_queryset = models.Comment.objects.filter(status=1, ancestor=cid).values('nid', 'user_id', "user__openid", 'user__username', 'user__avatar', 'content', 'create_time', 'parent_comment', 'like_count', 'reply_count', 'ancestor', 'parent_comment__user__openid', 'parent_comment__user__username', 'parent_comment__user__avatar') for item in comment_queryset: item["create_time"] = datetime.datetime.strftime(item.get('create_time'), "%Y-%m-%d %H:%M") comment_list.append(item) resp['data'] = comment_list return JsonResponse(resp) article_id = request.GET.get('article_id') user_id = request.META.get('HTTP_AUTHORIZATION') t = threading.Thread(target=read_update, args=(article_id,)) t.start() article = models.Article.objects.filter(status=1, nid=article_id).values('nid', 'title', 'content', 'create_time', 'img__image', 'comm_count', 'up_count', 'read_count', 'user__username', 'user__openid',"valid", 'user__avatar', 'category__title', 'category_id', 'type', 'study_url__gold') study_Link = models.Buy.objects.filter(user_id=user_id, study=article_id).values( 'study__study_url__study_url').first() likeUp = models.ArticleUpDown.objects.filter(user_id=user_id, article_id=article_id).values("is_up").first() likeUp = False if not likeUp or not likeUp.get("is_up") else True resp['data']['likeUp'] = likeUp if study_Link: resp['data']['study_url'] = study_Link.get("study__study_url__study_url") else: resp['data']['study_url'] = None collect = models.Trove.objects.filter(user_id=user_id, article_id=article_id).values("pk").first() resp['data']['collect'] = collect comment = models.Comment.objects.filter(article_id=article_id, status=1, parent_comment_id__isnull=True).order_by( "-pk").values('nid', 'user_id', 'user__openid', 'user__username', 'user__avatar', 'parent_comment', 'reply_count', "ancestor", 'content', 'create_time') commentId = [i.get("nid") for i in comment] commentImage = models.CommentImage.objects.filter(commentId__in=commentId).values("commentId", "image__image") commentImage_dic = {} for item in commentImage: url = build_url('images/' + item.get('image__image') + '/') if item.get("commentId") in commentImage_dic: commentImage_dic[item.get("commentId")].append(url) else: commentImage_dic[item.get("commentId")] = [url] comment_list = [] for item in comment: item['commentImage'] = commentImage_dic.get(item.get("nid")) item['create_time'] = datetime.datetime.strftime(item.get('create_time'), "%Y-%m-%d %H:%M") comment_list.append(item) images = ['images/' + i['img__image'] + '/' for i in article if i['img__image']] images = list(map(build_url, images)) article = article.first() date = datetime.datetime.strftime(article.get('create_time'), "%Y-%m-%d %H:%M") resp['data']['article'] = article resp['data']['article']['create_time'] = date resp['data']['article']['img'] = images resp['data']['comment'] = comment_list return JsonResponse(resp)
def change_user(request): """ 更改用户资料 :param request: :return: """ resp = {'code': 200, 'msg': '操作成功', 'data': {}} user_id = request.META.get('HTTP_AUTHORIZATION') openid = request.META.get("HTTP_IDMD5") oid = request.GET.get("oid") if request.method == "GET": if oid: user = models.Customer.objects.filter(openid=oid).values("username", "avatar", "sex", "telephone", "email", "qq", 'wechat').first() else: user = models.Customer.objects.filter(nid=user_id, openid=openid).values("username", "avatar", "sex", "telephone", "email", "qq", 'wechat').first() resp["data"] = user return JsonResponse(resp) # return JsonResponse(resp) username = request.POST.get("username") sex = request.POST.get("sex") telephone = request.POST.get("telephone") qq = request.POST.get("qq") wechat = request.POST.get("wechat") email = request.POST.get("email") img = request.FILES.get('avatar') check = SecCheck(request) if not check: resp["code"] = 405 resp['msg'] = "内容具有敏感信息" userinfo = models.Customer.objects.filter(openid=openid, status=1).values("nid", "openid", "username", "avatar", "sex", "province", "city", "email", "asset").first() return JsonResponse(resp) if img: path = os.path.join(settings.MEDIA_ROOT, "avatars", img.name) with open(path, 'wb') as f: for line in img: f.write(line) image = "avatars/" + img.name avatar = build_url(image) models.Customer.objects.filter(nid=user_id, openid=openid).update(avatar=avatar, username=username, sex=sex, telephone=telephone, qq=qq, wechat=wechat, email=email) else: models.Customer.objects.filter(nid=user_id, openid=openid).update(username=username, sex=sex, telephone=telephone, qq=qq, wechat=wechat, email=email) userinfo = models.Customer.objects.filter(openid=openid, status=1).values("nid", "openid", "username", "avatar", "sex", "province", "city", "email", "asset").first() resp['data'] = userinfo return JsonResponse(resp)
def index(request): """ 首页 :param request: :return: """ info_list = [] article_list = [] resp = {'code': 200, 'msg': '操作成功~', 'data': {}} page_size = 20 if request.method == 'POST': category_id = request.POST.get('cat_id') p = int(request.POST.get('p')) if not category_id or category_id == '1': article_info = models.Article.objects.filter(status=1).values( 'nid', 'title', 'desc', 'create_time', 'comm_count', 'up_count', 'category__title', 'user__avatar', 'user__username').order_by( "-weight", "-create_time")[page_size * (p + 1):page_size * (p + 1) + page_size] else: article_info = models.Article.objects.filter( status=1, category=category_id).values( 'nid', 'title', 'desc', 'create_time', 'comm_count', 'up_count', 'category__title', 'user__avatar', 'user__username').order_by( "-weight", "-nid")[page_size * (p + 1):page_size * (p + 1) + page_size] # p=0 n=2 [2,4] p=1 [4, 6] # p=0 n=5 [5, 10] p=1 [10:15] # 总结规律:每页取n个,取第p页则, [n*(p+1), n*(p+1)+n] for item in article_info: date = datetime.datetime.strftime(item.get('create_time'), "%Y-%m-%d %H:%M") item["create_time"] = date image = models.Article.objects.filter( nid=item.get("nid")).values("img__image").first() if image.get( "img__image") and image.get("img__image") != "undefined": item["img__image"] = build_url('images/' + image.get("img__image")) resp['data']['article'] = list(article_info) resp['data']['has_more'] = 0 if len(article_list) < page_size else 1 return JsonResponse(resp) category_id = request.GET.get('cat_id') if not category_id or category_id == '1': ad_info = models.Advertise.objects.filter( status=1, place='0').all().values('nid', 'title', 'images', 'url', 'content') article_info = models.Article.objects.filter(status=1).values( 'nid', 'title', 'desc', 'create_time', 'comm_count', 'up_count', 'category__title', 'user__avatar', 'user__username').order_by("-weight", "-nid")[0:page_size] for item in ad_info: item["pic_url"] = build_url('advertise/' + item.get('images')) resp['data']['advertise'] = list(ad_info) category_list = list(models.Category.objects.all().values( 'nid', 'title')) resp['data']['category'] = category_list else: t1 = threading.Thread(target=saveFormId, args=(request, )) t1.start() article_info = models.Article.objects.filter( status=1, category=category_id).values( 'nid', 'title', 'desc', 'create_time', 'comm_count', 'up_count', 'category__title', 'user__avatar', 'user__username').order_by("-weight", "-create_time")[0:page_size] for item in article_info: date = datetime.datetime.strftime(item.get('create_time'), "%Y-%m-%d %H:%M") item["create_time"] = date image = models.Article.objects.filter( nid=item.get("nid")).values("img__image").first() if image.get("img__image") and image.get("img__image") != "undefined": item["img__image"] = build_url('images/' + image.get("img__image")) resp['data']['article'] = list(article_info) return JsonResponse(resp)
def jou(request): resp = {'code': 200, 'msg': '操作成功', 'data': {}} openid = request.META.get("HTTP_IDMD5") all_cookie_dict = {} try: if request.method == "GET": user_obj = models.Customer.objects.filter(openid=openid).values("student_number").first() auth = request.GET.get("auth") if not user_obj.get("student_number") and auth == "auth": resp["code"] = 401 return JsonResponse(resp) elif user_obj.get("student_number") and auth == "auth": return JsonResponse(resp) proxy = ip() index = requests.get( url="http://zfxk.hhit.edu.cn/(ltlvfe554be2ev45xntowi35)/default2.aspx", timeout=10, headers={ "Host": "zfxk.hhit.edu.cn", "Upgrade-Insecure-Requests": "1", "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36" }, proxies=proxy ) all_cookie_dict.update(index.cookies.get_dict()) print(index.text) html = PyQuery(index.text) form1 = html('#form1 input').attr('value') print(form1) # 获得验证码 CheckCode = requests.get( url="http://zfxk.hhit.edu.cn/(ltlvfe554be2ev45xntowi35)/CheckCode.aspx", timeout=10, headers={ "Host": "zfxk.hhit.edu.cn", "Referer": "http://zfxk.hhit.edu.cn/(ltlvfe554be2ev45xntowi35)/default2.aspx", "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36" }, cookies=all_cookie_dict, proxies=proxy ) path = os.path.join(settings.MEDIA_ROOT, "code", "%s.jpg" % openid) with open(path, 'wb') as f: f.write(CheckCode.content) url_path = build_url('code/%s.jpg' % openid) resp['data']['rsa'] = form1 resp['data']["code"] = url_path return JsonResponse(resp) user = request.POST.get("user") password = request.POST.get("pwd") code = request.POST.get("code") form1 = request.POST.get("rsa") t1 = threading.Thread(target=saveFormId, args=(request,)) t1.start() proxy = ip() login = requests.post( url="http://zfxk.hhit.edu.cn/(ltlvfe554be2ev45xntowi35)/default2.aspx", timeout=10, data={ "__VIEWSTATE": form1, "txtUserName": user, "Textbox1": "", "TextBox2": password, "txtSecretCode": code, "RadioButtonList1": "(unable to decode value)", "Button1": "", "lbLanguage": "", "hidPdrs": "", "hidsc": "", }, headers={ "Content-Type": "application/x-www-form-urlencoded", "Host": "zfxk.hhit.edu.cn", "Origin": "http://zfxk.hhit.edu.cn", "Referer": "http://zfxk.hhit.edu.cn/(ltlvfe554be2ev45xntowi35)/default2.aspx", "Upgrade-Insecure-Requests": "1", "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Safari/537.36 Core/1.70.3704.400 QQBrowser/10.4.3587.400" }, allow_redirects=False, proxies=proxy ) login_redirect = requests.get( url="http://zfxk.hhit.edu.cn/(ltlvfe554be2ev45xntowi35)/xs_main.aspx?xh=%s" % user, timeout=10, headers={ "Host": "zfxk.hhit.edu.cn", "Referer": "http://zfxk.hhit.edu.cn/(ltlvfe554be2ev45xntowi35)/default2.aspx", "Upgrade-Insecure-Requests": "1", "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Safari/537.36 Core/1.70.3704.400 QQBrowser/10.4.3587.400" }, proxies=proxy ) print(login.text, "***") print(login_redirect.text) print(proxy) login_html = PyQuery(login_redirect.text) Label3 = login_html("#Label3").text() username = login_html("#xhxm").text() if Label3 == "欢迎您:": print("登录成功", resp) username = username.replace("同学", "") models.Customer.objects.filter(openid=openid).update(student_number=user, name=username) return JsonResponse(resp) else: resp['code'] = 400 return JsonResponse(resp) except requests.exceptions.ReadTimeout as e: print(e) # getIp() return jou(request) except requests.exceptions.ConnectTimeout as e: print(e) # getIp() return jou(request)