def article_detail(request, pk): """ Get, update or delete article :param request :param pk """ try: article = Article.objects.get(pk=pk) except Article.DoesNotExist: return Response(status=status.HTTP_404_NOT_FOUND) if request.method == 'GET': serializer = ArticleSerializer(article) return Response(serializer.data) elif request.method == 'PUT': serializer = ArticleSerializer(article, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data) else: return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) elif request.method == 'DELETE': article.delete() return Response(status=status.HTTP_204_NO_CONTENT)
def index(request): user = request.user items = user.userfeeditem_set.all() serializer = ArticleSerializer(data=items, many=True, context={"request": request}) serializer.is_valid() articles = JSONRenderer().render(serializer.data) return render(request, "index.html", {"articles": articles})
def post(self, request): start_date = request.data['start_date'] end_date = request.data['end_date'] keyword = request.data['keyword'] if len(keyword.strip()) == 0: article = Article.objects.filter( time__range=(start_date + " 00:00:00", end_date + " 23:59:59")).order_by('-time') serializer = ArticleSerializer(article, many=True) elif len(start_date.strip()) == 0 or len(end_date.strip()) == 0: article = Article.objects.filter( Q(title__icontains=keyword) | Q(text__icontains=keyword)).order_by('-time') serializer = ArticleSerializer(article, many=True) else: article = Article.objects.filter( Q(title__icontains=keyword) | Q(text__icontains=keyword), time__range=(start_date + " 00:00:00", end_date + " 23:59:59")).order_by('-time') serializer = ArticleSerializer(article, many=True) response = { 'code': 1, 'data': serializer.data, } return Response(response)
def patch(self, request, pk): article = self.get_object(pk) serializer = ArticleSerializer(article, data=request.data, partial=True) if not serializer.is_valid(): return Response(serializer.errors, status=400) serializer.update(article, request.data) return Response(serializer.data, status=200)
def put(self, request, pk=None, article_id=None): anime = get_anime(pk) serializer = ArticleSerializer(instance=self.get_article( article_id, anime), data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors)
def post(self, request): serializer = ArticleSerializer(data=request.data) if serializer.is_valid(): serializer.save() return Response({'Article': serializer.data}) return Response(serializer.errors)
def post(self, request, format=None) : if request.user.is_authenticated : serializer = ArticleSerializer(data=request.data) if serializer.is_valid() : serializer.save(writer=request.user) jsonString = {} jsonString['id'] = int(serializer.data['id']) return HttpResponse(json.dumps(jsonString), content_type="application/json", status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) return Response("",status=status.HTTP_400_BAD_REQUEST)
def patch(self, request, pk, formoat=None) : article = self.get_object(pk) serializer = ArticleSerializer(article, data=request.data) if str(getattr(article, 'writer')) == request.user.username :#인증 if serializer.is_valid() : serializer.save() return Response(serializer.data) else : return Response("This is not your article.", status=status.HTTP_400_BAD_REQUEST) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def post(self, request): serializer = ArticleSerializer(data=request.data) try: author_id = request.data['author_id'] except: return Response(serializer.errors, status=400) if not serializer.is_valid(): return Response(serializer.errors, status=400) serializer.create(request.data, author_id) return Response(serializer.data, status=201)
def articles(request): try: category = request.query_params['category'] articles = Article.objects.filter(category=category) except: articles = Article.objects.all() if valid_token(request): data = ArticleSerializer(articles, many=True, anonymous=False).data return Response(data) else: data = ArticleSerializer(articles, many=True, anonymous=True).data return Response(data)
def post(self, request): print(request.data) serializer = ArticleSerializer(data=request.data) if serializer.is_valid(): article = serializer.save() response = { 'code': 1, 'data': serializer.data, } return Response(response) else: return Response(serializer.errors)
def get(self, request, pk, format=None) : article = self.get_object(pk) serializer = ArticleSerializer(article) if str(getattr(article, 'writer')) == request.user.username :#인증 return Response(serializer.data) else : return Response("This is not your article.", status=status.HTTP_400_BAD_REQUEST)
def post(self, request, pk=None): character = get_character(pk) data = json.loads(request.body) article = CharacterArticle.objects.create(character=character, name=data['name'], content=data['content']) serializer = ArticleSerializer(article) return Response(serializer.data)
def articles_list(request): """ List all Article, or Create a new Article :param request """ if request.method == 'GET': articles = Article.objects.all() serializer = ArticleSerializer(articles, many=True) return Response(serializer.data) elif request.method == "POST": serializer = ArticleSerializer(data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) else: return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def post(self, request, pk=None): anime = get_anime(pk) data = json.loads(request.body) article = AnimeArticle.objects.create(anime=anime, name=data['name'], content=data['content']) serializer = ArticleSerializer(article) return Response(serializer.data)
def get(self, request): article = Article.objects.all().order_by('-time') serializer = ArticleSerializer(article, many=True) response = { 'code': 1, 'data': serializer.data, } return Response(response)
def get(self, request): articles = Article.objects.all() serializer = ArticleSerializer(articles, many=True) return Response({ 'Articles': serializer.data, })
def post(self, request): uid = request.data['id'] article = Article.objects.get(id=uid) serializer = ArticleSerializer(article) if serializer: response = { 'code': 1, 'data': serializer.data, } return Response(response) else: return Response(serializer.errors)
def put(self, request): uid = request.data['id'] try: article = Article.objects.get(id=uid) except: response = { 'code': 0, 'data': [], } return Response(response) else: serializer = ArticleSerializer(data=request.data, instance=article) if serializer.is_valid(): serializer.save() response = { 'code': 1, 'data': serializer.data, } return Response(response) else: return Response(serializer.errors)
def search(self, request): query = request.data.get('query') if not query: return Response('query was not satisfied', status=status.HTTP_400_BAD_REQUEST) articles = Article.objects.filter(text__icontains=query) serializer = ArticleSerializer(articles, many=True) return Response(serializer.data)
def function_based_views_article_list(request): if request.method == 'GET': articles = Article.objects.all() serializer = ArticleSerializer(articles, many=True) return JsonResponse(serializer.data, safe=False) elif request.method == 'POST': data = JSONParser().parse(request) serializer = ArticleSerializer(data=data) if serializer.is_valid(): serializer.save() return JsonResponse(serializer.data, status=status.HTTP_201_CREATED) return JsonResponse(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def test_display_serializer(self): display_serializer = ArticleSerializer(self.article) article_data = display_serializer.data self.assertEquals(article_data['title'], self.article_data['title']) self.assertEquals(article_data['url'], self.article_data['url']) self.assertEquals(article_data['summary'], self.article_data['summary']) self.assertEquals(article_data['published_date'], self.article_data['published_date']) self.assertEquals(article_data['time_to_read'], self.article_data['time_to_read']) self.assertEquals(article_data['likes'], self.article_data['likes']) self.assertEquals(article_data['clicks'], self.article_data['clicks']) self.assertEquals(article_data['shares'], self.article_data['shares']) self.assertEquals(article_data['views'], self.article_data['views']) self.assertEquals(article_data['source_site'], self.article_data['source_site'])
def post(self, request): if 'article_id' in request.data.keys(): article_id = request.data['article_id'] if 'type' in request.data.keys(): type = request.data['type'] article = Article.objects.get(id=article_id) serializer = ArticleSerializer(article) if serializer: if type == 'text': response = {'code': 1, 'data': serializer.data} return Response(response) elif type == 'html': base_url = get_host(request) response = { 'code': 1, 'data': base_url + serializer.data['filepath'] } return Response(response) else: return Response(serializer.errors)
def list(self, request: Request, *args: Any, **kwargs: Any) -> Response: """ List of book. Args: request: request sent by the client. args: Variable length argument list. options: Arbitrary keyword arguments. Returns: Response from the server. """ if request.query_params: search = request.query_params self.queryset = self.queryset.filter( Q(title__contains=search["search"]) | Q(content__contains=search["search"]) | Q(category=search["search"])) serializer = ArticleSerializer(self.queryset, many=True) return Response(serializer.data, status=200) return super().list(request, *args, **kwargs)
def post(self, request): if 'page' in request.data.keys(): page = request.data['page'] if 'size' in request.data.keys(): size = request.data['size'] if 'keyword_id' in request.data.keys(): keyword_id = request.data['keyword_id'] article_id = [] queryset = KeywordArticle.objects.filter(keyword_id=keyword_id) serializer = KeywordArticleSerializer(queryset, many=True) for row in serializer.data: article_id.append(row['article_id']) article = Article.objects.filter(id__in=article_id).values( 'id', 'target', 'title', 'time').order_by('-time')[(page - 1) * size:page * size] count = Article.objects.filter(id__in=article_id).count() serializer = ArticleSerializer(article, many=True) response = {'code': 1, 'data': serializer.data, 'count': count} return Response(response)
def function_based_api_views_article_single(request, pk): ''' Article display, update, delete ''' try: article = Article.objects.get(pk=pk) except Article.DoesNotExist: return HttpResponse(status = status.HTTP_404_NOT_FOUND) if request.method == 'GET': serializer = ArticleSerializer(article) return Response(serializer.data) elif request.method == 'PUT': serializer = ArticleSerializer(article, data= request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors, status=HTTP_400_BAD_REQUEST) elif request.method == 'DELETE': article.delete() return Response(status=status.HTTP_204_NO_CONTENT)
def save_html(response, target): html = etree.HTML(response.text) title = html.xpath('//meta[@property="og:title"]/@content')[0] contexts = html.xpath('//*[@id="page-content"]') text = contexts[0].xpath('string(.)').strip() keyword = get_keyword() saved_flag = False for word in keyword: if word['keyword'] in title or word['keyword'] in text: if not saved_flag: dir_name = n_digits_random(4) first = os.path.join(BASE_DIR, 'local') second = os.path.join(first, target) third = os.path.join(second, article_time[:10]) target_dir = os.path.join(third, dir_name + '/') if not os.path.exists(first): # 不存在则创建路径 os.mkdir(first) if not os.path.exists(second): os.mkdir(second) if not os.path.exists(third): os.mkdir(third) if not os.path.exists(target_dir): os.mkdir(target_dir) filepath = target_dir + dir_name + '.html' relative_dir = 'local/' + target + '/' + article_time[:10] + '/' + dir_name + '/' relative_filepath = relative_dir + dir_name + '.html' filepath_bak = target_dir + dir_name + '_bak.html' article = { "target": target, "url": article_url, "title": title, "time": article_time, "text": text, "filepath": relative_filepath } serializer = ArticleSerializer(data=article) if serializer.is_valid(): serializer.save() add_scrapedUrls(target, article_time) saved_flag = True weixin_log.info("successfully saved to database") else: weixin_log.error("failed save to database") try: f = open(filepath, "wb") f.write(response.content) f.close() obj = BeautifulSoup(response.content, 'lxml') # 后面是指定使用lxml解析,lxml解析速度比较快,容错高。 imgs = obj.find_all('img') urls = [] for img in imgs: if 'data-src' in str(img): urls.append(img['data-src']) # 遍历所有图片链接,将图片保存到本地指定文件夹,图片名字用0,1,2... i = 0 suffix = '' for url in urls: if url.endswith('png'): suffix = '.png' elif url.endswith('jpeg'): suffix = '.jpg' elif url.endswith('gif'): suffix = '.gif' r = requests.get(url) t = os.path.join(target_dir, str(i) + suffix) t_relative = os.path.join(relative_dir, str(i) + suffix) fw = open(t, 'wb') # 指定绝对路径 fw.write(r.content) # 保存图片到本地指定目录 i += 1 with open(filepath, encoding='utf-8') as f, open(filepath_bak, 'w', encoding='utf-8') as fw: # 打开两个文件,原始文件用来读,另一个文件将修改的内容写入 old_url = 'data-src="' + url + '"' new_url = 'src="/' + t_relative + '"' for line in f: # 遍历每行,用replace 方法替换 new_line = line.replace(old_url, new_url) # 逐行替换 fw.write(new_line) # 写入新文件 os.remove(filepath) # 删除原始文件 os.rename(filepath_bak, filepath) # 修改新文件名, old -> new fw.close() except: weixin_log.info("failed save to local file") else: weixin_log.info("successfully saved to local file") keywordarticle = { "keyword_id": word['keyword_id'], "article_id": serializer.data['id'] } keywordarticle_serializer = KeywordArticleSerializer(data=keywordarticle) if keywordarticle_serializer.is_valid(): keywordarticle_serializer.save() else: weixin_log.error("keyword-article relationship built failed!") if not saved_flag: weixin_log.info(target + " not contains any of the keywords, pass")
def get(self, request): objs = Article.objects.all() data = ArticleSerializer(objs, many=True).data return Response(data)
def get(self, request, pk): article = self.get_object(pk) serializer = ArticleSerializer(article) return Response(serializer.data)
def test_valid(self): serializer = ArticleSerializer( data=self.article_data | {'diseases': [self.disease_1.pk, self.disease_2.pk]}) self.assertTrue(serializer.is_valid())
def get(self, request): articles = Article.objects.all()[:20] data = ArticleSerializer(articles, many=True).data return Response(data)
def get(self, request, pk): article = get_object_or_404(Article, pk=pk) data = ArticleSerializer(article).data return Response(data)
def get(self, request, pk=None): anime = get_anime(pk) articles_id = anime.articles.all() articles = [article for article in articles_id] serializer = ArticleSerializer(articles, many=True) return Response(serializer.data)