def get(self, request, author_id): try: author = UserProfile.objects.get(username=author_id) except Exception as e: result = {'code': 10305, 'error': 'author_id is error'} return JsonResponse(result) #访问者是游客还是自己 visitor_name = get_user_by_request(request) category = request.GET.get('category') filter_category = False if category in ['tec', 'no-tec']: filter_category = True if visitor_name == author_id: if filter_category: author_topics = Topic.objects.filter(user_profile_id=author_id) else: author_topics = Topic.objects.filter(user_profile_id=author_id, limit='public') res = self.make_topics_res(author, author_topics) return JsonResponse(res)
def get(self, request, author_id): #/v1/topics/guoxiaonao # /v1/topics/guoxiaonao?category=tec|no-tec #获取用户guoxiaonao的文章列表 #1,访问者 visitor #2, 博主 author try: author = UserProfile.objects.get(username=author_id) except Exception as e: result = {'code':10305, 'error':'The author id is error'} return JsonResponse(result) #尝试获取访问者的身份 visitor_username = get_user_by_request(request) category = request.GET.get('category') filter_category = False if category in ['tec', 'no-tec']: filter_category = True if visitor_username == author_id: #博主访问自己的博客 if filter_category: author_topics = Topic.objects.filter(user_profile_id=author_id, category=category) else: author_topics = Topic.objects.filter(user_profile_id=author_id) else: #非博主访问博主的博客 if filter_category: author_topics = Topic.objects.filter(user_profile_id=author_id, limit='public', category=category) else: author_topics = Topic.objects.filter(user_profile_id=author_id, limit='public') res = self.make_topics_res(author, author_topics) return JsonResponse(res)
def get(self, request, author_id): # /v1/topics/haha # 获取博客列表 # 1.访问者 -visitor # 2.博主 - author try: author = UserProfile.objects.get(username=author_id) except Exception as e: result = {'code': 10303, 'error': 'The author is not existed!'} return JsonResponse(result) visitor_username = get_user_by_request(request) category = request.GET.get('category') filter_category = False if category in ['tec', 'no-tec']: # 判断是否符合要求 filter_category = True if visitor_username == author.username: # 博主访问自己的博客,可获取 公开+私人的内容 if filter_category: author_topics = Topic.objects.filter(author_id=author_id, category=category) else: author_topics = Topic.objects.filter(author_id=author_id) else: # 非博主访问,只获取公开内容 if filter_category: author_topics = Topic.objects.filter(author_id=author_id, limit='public', category=category) else: author_topics = Topic.objects.filter(author_id=author_id, limit='public') res = self.make_topics_res(author, author_topics) return JsonResponse(res)
def get(self,request,author_id): print('----------------------------in') try: author = UserProfile.objects.get(username=author_id) except Exception as e : result={'code':10305,'error':'The author id is error'} return JsonResponse(result) visitor_username = get_user_by_request(request) t_id=request.GET.get('t_id') is_self=False if t_id: if visitor_username == author_id: is_self=True try: author_topics = Topic.objects.get(id=t_id,user_profile_id=author_id) except Exception as e: result={'code':5555,'error':'wrong'} return JsonResponse(result) else: try: author_topics = Topic.objects.get(id=t_id, user_profile_id=author_id,limit='public') except Exception as e: result = {'code': 5555, 'error': 'wrong'} return JsonResponse(result) res=self.make_topic_res(author,author_topics,is_self) return JsonResponse(res) else: category = request.GET.get('category') filter_category = False if category in ['tec', 'no-tec']: filter_category = True if visitor_username == author_id: # 博主访问自己的博客 if filter_category: author_topics = Topic.objects.filter(user_profile_id=author_id, category=category) else: # 非博主访问博主的博客 author_topics = Topic.objects.filter(user_profile_id=author_id, limit='public', category=category) else: # 博主访问自己的博客 if filter_category: author_topics = Topic.objects.filter(user_profile_id=author_id) else: # 非博主访问博主的博客 author_topics = Topic.objects.filter(user_profile_id=author_id, limit='public') res = self.make_topics_res(author, author_topics) return JsonResponse(res)
def edit(request, id): if request.method == "POST": try: data = ArticlePost.objects.get(id=id) except Exception as e: return HttpResponse('未查到此内容') visitor_name = get_user_by_request(request) if visitor_name == data.author.username: return JsonResponse({'code': 200, "id": data.id}) else: return JsonResponse({"code": 404})
def get(self, request, author_id): print('----topic get view in') #/v1/topics/guoxiaonao #/v1/topics/guoxiaonao?category=tec|no-tec #获取用户guoxiaonao的文章列表 #1,访问者 visitor #2, 博主 author try: author = UserProfile.objects.get(username=author_id) except Exception as e: result = {'code':10305, 'error':'The author id is error'} return JsonResponse(result) #尝试获取访问者的身份 visitor_username = get_user_by_request(request) t_id = request.GET.get('t_id') is_self = False if t_id: #/v1/topics/guoxiaonao?t_id=xxx #获取指定文章数据 [文章详情页] if visitor_username == author_id: is_self = True #博主访问自己 try: author_topic = Topic.objects.get(id=t_id, user_profile_id=author_id) except Exception as e: result = {'code':10310, 'error': 'The topic id is error'} return JsonResponse(result) else: #非博主访问自己-只能看 public 文章 try: author_topic = Topic.objects.get(id=t_id, user_profile_id=author_id, limit='public') except Exception as e: result = {'code':10310, 'error': 'The topic id is error'} return JsonResponse(result) res = self.make_topic_res(author, author_topic, is_self) return JsonResponse(res) else: # /v1/topics/guoxiaonao # /v1/topics/guoxiaonao?category=tec|no-tec #获取用户批量文章数据 [文章列表页] category = request.GET.get('category') filter_category = False if category in ['tec', 'no-tec']: filter_category = True if visitor_username == author_id: #博主访问自己的博客 if filter_category: author_topics = Topic.objects.filter(user_profile_id=author_id, category=category) else: author_topics = Topic.objects.filter(user_profile_id=author_id) else: #非博主访问博主的博客 if filter_category: author_topics = Topic.objects.filter(user_profile_id=author_id, limit='public', category=category) else: author_topics = Topic.objects.filter(user_profile_id=author_id, limit='public') res = self.make_topics_res(author, author_topics) return JsonResponse(res)
def get(self, request, author_id): """ self: 对象自己 :param request: 请求对象 :param author_id: 作者对象 :return: 所有文章 """ # /v1/topic/username # 访问者 visitor # 博主 print('views_in ......') try: author = UserProfile.objects.get(username=author_id, is_active=True) except Exception as e: result = {'code': 10302, 'error': '作者不存在!'} return JsonResponse(result) # 访问者,没有登录 # 博主,登录 公开文章 + 个人文章 # 不是博主,登录 公开文章 visitor = get_user_by_request(request) visitor_username = None if visitor: visitor_username = visitor.username # 获取文章编号 id t_id = request.GET.get('t_id') if t_id: # 获取指定文章列表 # v1/topics/username/t_id t_id = int(t_id) is_self = False if visitor_username == author_id: # 获取博主自己文章 is_self = True try: author_topic = Topic.objects.get(id=t_id, author_id=author_id, is_active=False) except Exception as e: result = {'code': 10303, 'error': 'not is topic'} return JsonResponse(result) else: # 访客获取文章 try: author_topic = Topic.objects.get(id=t_id, author_id=author_id, is_active=False, limit='public') except Exception as e: result = {'code': 10303, 'error': 'not is topic'} return JsonResponse(result) # 构建具体博客的响应格式 res = self.make_topic_res(author, author_topic, is_self) return JsonResponse(res) else: # 获取博客文章列表 # 查看有无查询字符串 category = request.GET.get('category') if category in ['tec', 'no-tec']: if visitor_username == author_id: # 博主自己访问自己的博客 author_topics = Topic.objects.filter(author_id=author_id, is_active=False, category=category) else: author_topics = Topic.objects.filter(author_id=author_id, is_active=False, limit='public', category=category) else: if visitor_username == author_id: # 博主自己访问自己的博客 author_topics = Topic.objects.filter(author_id=author_id, is_active=False) else: author_topics = Topic.objects.filter(author_id=author_id, is_active=False, limit='public') # 加载响应格式 res = self.make_topics_res(author, author_topics) return JsonResponse(res)