def test_add(self): # Create an Article. a5 = Article(id=None, headline='Django lets you reate Web apps easily') # You can't associate it with a Publication until it's been saved. self.assertRaises(ValueError, getattr, a5, 'publications') # Save it! a5.save() # Associate the Article with a Publication. a5.publications.add(self.p1) self.assertQuerysetEqual(a5.publications.all(), ['<Publication: The Python Journal>']) # Create another Article, and set it to appear in both Publications. a6 = Article(id=None, headline='ESA uses Python') a6.save() a6.publications.add(self.p1, self.p2) a6.publications.add(self.p3) # Adding a second time is OK a6.publications.add(self.p3) self.assertQuerysetEqual(a6.publications.all(), [ '<Publication: Science News>', '<Publication: Science Weekly>', '<Publication: The Python Journal>', ]) # Adding an object of the wrong type raises TypeError self.assertRaises(TypeError, a6.publications.add, a5) # Add a Publication directly via publications.add by using keyword arguments. p4 = a6.publications.create(title='Highlights for Adults') self.assertQuerysetEqual(a6.publications.all(), [ '<Publication: Highlights for Adults>', '<Publication: Science News>', '<Publication: Science Weekly>', '<Publication: The Python Journal>', ])
def newarticle(request): if not request.user.is_authenticated(): return redirect('/Login/?next=%s' % request.path) if request.method=='POST': title=request.POST['title'] tags=request.POST['tag'] cat=request.POST['cat'] content=request.POST['content'] tags=tags.split(',') tagm=[] print tags for i in tags: a=Tag.objects.filter(tag=i) if a: tagm.append(a[0]) else: t=Tag(tag=i) t.save() tagm.append(t) if cat.find('mc')>0: maincat=MainCat.objects.get(id=cat[:cat.find('m')]) ar=Article(user=request.user,title=title,content=content,maincat=maincat) elif cat.find('-')==-1: dcat=DetailCat.objects.get(id=cat) maincat=dcat.maincat ar=Article(user=request.user,title=title,content=content,detailcat=dcat,maincat=maincat) else: ar=Article(user=request.user,title=title,content=content) ar.save() ar.tag=tagm ar.save() return redirect(getarticle,ar.id) maincat=MainCat.objects.all() nart,cart,rart,cm,tg=getthree() #日历 today=today=datetime.datetime.now() s=calendar.HTMLCalendar(6) cals=list(s.itermonthdays2(today.year,today.month)) tdarts=Article.objects.values('id','createtime').filter(createtime__year=today.year,createtime__month=today.month).order_by('createtime') #列表字典[{'createtime': datetime.datetime(2014, 4, 6, 4, 36, 32, 896000, tzinfo=<UTC>)}, tdart=set([i['createtime'].day for i in tdarts]) tmpq=Article.objects.exclude(createtime__year=today.year,createtime__month=today.month) premon=tmpq.filter(createtime__lt=today).order_by('-createtime')[:1] aftmon=tmpq.filter(createtime__gt=today).order_by('createtime')[:1] tt=[] for i in cals: tt.append(list(i)) ttt=[] for a in tt: for i in tdart: if a[0] == i: a.append(1) if len(a)==2: a.append(0) ttt.append(a) return render_to_response('new.htm',locals(),context_instance=RequestContext(request))
def post(self): """Edit Artilce.""" data = self.request.data(title='', tag='', category='', \ raw_content='', old_tag='', created_at='') created_at = data.created_at title = data.title old_tag = data.old_tag tag = data.tag category = data.category raw_content = data.raw_content content = (markdown2.markdown(raw_content)).encode('utf-8') u_id = self.get_current_user() admin = User.get_by_primary_key(u_id) created_at_now = int(time.time()) # new article if not created_at: article = Article(title=title,content=content,\ raw_content=raw_content,category=category, \ author=admin.name, created_at=created_at_now) article.insert() new_article = Article.select_one('created_at=?', created_at_now) article_id = new_article.id # tag tag_d = Tag.select_one('tag_name=?', tag) # tag doesn't exists if not tag_d: new_tag = Tag(tag_name=tag) new_tag.insert() get_tag = Tag.select_one('tag_name=?', tag) tag_id = get_tag.id # tag exists else: tag_id = tag_d.id article_tag = ArticleTag(article_id=article_id, tag_id=tag_id) article_tag.insert() # update old article else: article = Article(title=title, content=content, \ raw_content=raw_content, author=admin.name, \ category=category, created_at=created_at_now) article.update('created_at=?', created_at) new_article = Article.select_one('created_at=?', created_at_now) article_id = new_article.id old_tag = Tag.select_one('tag_name=?', old_tag) new_tag = Tag(tag_name=tag) other_article_tag = ArticleTag.select_all('tag_id=?', old_tag.id) # other article doesn't have this article if len(other_article_tag) >= 1: tag_id = old_tag.id new_tag.update('id=?', tag_id) # other article has this tag else: new_tag.insert() get_tag = Tag.select('tag_name=?', tag) tag_id = get_tag.id article_tag = ArticleTag(tag_id=tag_id) article_tag.update('article_id=?', article_id) self.redirect('/admin/articles')
def test_create(self): # You can also instantiate an Article by passing the Reporter's ID # instead of a Reporter object. a3 = Article(id=None, headline="Third article", pub_date=datetime(2005, 7, 27), reporter_id=self.r.id) a3.save() self.assertEqual(a3.reporter.id, self.r.id) # Similarly, the reporter ID can be a string. a4 = Article(id=None, headline="Fourth article", pub_date=datetime(2005, 7, 27), reporter_id=str(self.r.id)) a4.save() self.assertEqual(repr(a4.reporter), "<Reporter: John Smith>")
def getArticles(): articles = Article.query() if articles.count() == 0: hello = Article() hello.title = "Hello World" hello.content = "Lorem ipsum dolor sit amet, consectetur adipisicing elit. Consequuntur, voluptatum. Provident accusamus sit, commodi corrupti illo, veniam possimus minima rerum itaque. Magni, beatae, facere." hello.put() bye = Article() bye.title = "Bye World" bye.content = "Iraqi PM Nouri Maliki steps aside, ending political deadlock in Baghdad as the government struggles against insurgents in the north." bye.put() del hello, bye articles = Article.query() return serialize(list(articles.order(Article.date).fetch(10)))
def setUp(self): # Create a Reporter. self.r = Reporter(name='John Smith') self.r.save() # Create an Article. self.a = Article(headline="First", reporter=self.r) self.a.save() # Create an Article via the Reporter object. self.a2 = self.r.article_set.create(headline="Second") # Create an Article with no Reporter by passing "reporter=None". self.a3 = Article(headline="Third", reporter=None) self.a3.save() # Create another article and reporter self.r2 = Reporter(name='Paul Jones') self.r2.save() self.a4 = self.r2.article_set.create(headline='Fourth')
def art_add(): form = ArticleAddForm() if form.validate_on_submit(): data = form.data # 上传logo file = secure_filename(form.logo.data.filename) #编译安全文件名称 logo = change_name(file) if not os.path.exists(app.config["uploads"]): os.makedirs(app.config["uploads"]) # 保存文件 form.logo.data.save(app.config["uploads"] + "/" + logo) # 获取用户ID user = User.query.filter_by(account=session["user"]).first() user_id = user.id # 保存数据,Article article = Article( title=data["title"], category=data["category"], user_id=user_id, logo=logo, content=data["content"], add_time=datetime.now().strftime("%Y-%m-%d %H:%M:%S"), ) db.session.add(article) db.session.commit() flash(u"发布文章成功", "ok") return render_template("art_add.html", title="发布文章", form=form)
def art_add(): articleform=ArticleForm() if articleform.validate_on_submit(): data=articleform.data #update picture datas=secure_filename(articleform.logo.data.filename) logo=change_filename(datas) if not os.path.exists(app.config["UP"]): os.makedirs(app.config["UP"]) #save file articleform.logo.data.save(app.config["UP"]+"/"+logo) #get userID user=User.query.filter_by(username=session["user"]).first() user_id=user.id #save data articale =Article( title=data["title"], category=data["category"], user_id=user_id, logo=logo, content=data["content"], addTime=datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") ) db.session.add(articale) db.session.commit() flash(u"Add review successfully!","ok") return render_template("artadd.html",form=articleform)
def article_edit(): if request.method == 'GET': artic_id = request.args.get('artic_id') tag_all = Material.query.with_entities(Material.tag.distinct().label("tag")).filter(Material.user_id==session['user_id']) if artic_id: article = Article.query.filter(Article.artic_id==int(artic_id),Article.user_id==session['user_id']).first() return render_template('article_edit.html', article=article,tags=tag_all) else: return render_template('article_edit.html',tags=tag_all) else: artic_id = request.form['artic_id'] title = request.form['title'] content = request.form['content'] edit_time = datetime.utcnow() user_id = session['user_id'] # print(mater_id,title,content,edit_time,user_id) if artic_id: article = Article.query.filter(Article.artic_id==int(artic_id),Article.user_id==session['user_id']).first() article.title = title article.content = content article.edit_time = edit_time db.session.add(article) db.session.commit() else: article = Article(title=title,content=content,user_id=user_id,edit_time=edit_time) db.session.add(article) db.session.commit() # return u"提交成功" return redirect(url_for('index'))
def test_country_4(self): c = Country(name='Cowland') a = Article(title='President Cowy McMoo to Ban Strawberry Milk') c.articles.append(a) db.session.add(c) first = Country.query.first() self.assertEqual(len(first.articles), 1)
def save(self, username,article=None): cd = self.cleaned_data title = cd['title'] title_zh = title now = datetime.datetime.now() content_md = markdown.markdown(cd['content']) url = '/article/%s' % (title) tags = cd['tags'] if article: article.URL=url article.title=title article.title_zh=title_zh article.content=content_md article.tags=tags article.updated=now else: article = Article( URL=url, title=title, title_zh=title_zh, author=username, content=content_md, tags=tags, views=0, created=now, updated=now ) article.save()
def to_Article(self): return Article(title=self.title.data, author=self.author.data, highlight=self.highlight.data, subject=self.subject.data, date=datetime.datetime.now(), email=self.email.data)
def update_articles(data): source_id = data['source_id'] or 'NA' author = data['author'] title = data['title'] description = data['description'] url = data['url'] img_url = data['img_url'] published_at = data['published_at'] content = data['content'] boards = data['board_id'] article = Article(source_id=source_id, author=author, title=title, description=description, url=url, img_url=img_url, published_at=published_at, content=content) db.session.add(article) db.session.commit() for board in boards: board_id = board article_id = article.id article_board = ArticleBoard(board_id=board_id, article_id=article_id) db.session.add(article_board) db.session.commit() return
def parse(self, response): # grab list of article URLs on the page urls = response.xpath( '//*[@id="content_wrapper"]/div/div[2]/div[2]/div[2]/ul/*/div/a/@href' ).extract() for url in urls: # make URL requestable full_url = self.base_url + url article_id = int(url.split('/')[2].split('-')[0]) # check if URL is in the database if db.session.query(Article).get(article_id) is not None: print('Article {} is already in the database, pass.'.format( str(article_id))) pass # add it to the database if the article_id is not in already else: article = {} article['article_id'] = int(article_id) article['article_url'] = full_url db.session.add(Article(**article)) db.session.commit() print('Article {} has been added to the database.'.format( str(article_id)))
def api_data_api(): data = request.get_json() msg = data.get('message') if msg != "success": return wrap_false(msg="获取数据错误") article_list = data.get('data') count = len(article_list) if count == 0: return wrap_false(msg="获取到的数据为空") allow_num = 0 for article in article_list: if Article(article).pop(): allow_num += 1 print('符合筛选要求,成功添加') else: print('未符合筛选要求') print("allow_num:{},count:{}".format(allow_num, count)) point = allow_num / count print('总共:{} 成功{} 成功率{}'.format(count, allow_num, point)) if point < 0.5: data = {"status": "end"} return wrap_true(msg="上传成功", data=data) data = { "status": "keep", "max_behot_time": data.get('next').get('max_behot_time') } return wrap_true(msg="继续上传", data=data)
def create_article(request, block_id): block_id = int(block_id) block = Block.objects.get(id=block_id) # get方法要求只返回一个结果 if request.method == "GET": return render_to_response("article_create.html", {"b": block}, context_instance=RequestContext(request)) else: # POST title = request.POST["title"].strip() content = request.POST["content"].strip() if not title or not content: # pass messages.add_message(request, messages.ERROR, u'标题和内容不允许为空') return render_to_response( "article_create.html", # {"b": block, "title": title, "content": content},#可以不需要这些参数只为传入到html时候用的 context_instance=RequestContext(request)) # 停留在当前页面 else: owner = User.objects.all()[0] # TODO news_article = Article(block=block, owner=request.user, content=content, title=title) news_article.save() messages.add_message(request, messages.INFO, u'文章发布成功') return redirect(reverse("article_list", args=[ block.id ])) # 根据url名称反查url地址(返回一个字符串) 等同于html {% url 'URL名称' 位置参数 关键字参数 %}
def article_post(): form = set_article_form() if form.validate_on_submit(): cate_id = form.data['cate'] title = form.data['title'] thumb = form.data['thumb'] intro = form.data['intro'] content = form.data['content'] article = Article( cate_id=cate_id, title=title, thumb=thumb, intro=intro, content=content, author=session['user'] ) try: db.session.add(article) db.session.commit() except Exception as e: print(str(e)) message = {'message': '文章发布失败'} else: message = {"message": "文章发布成功"} return jsonify(message) return render_template("member/article/article_post.html", form=form)
def add_art(): if account() < 0: if request.method == 'POST': cate_id = request.form.get('cate_id') art_name = request.form.get('art_name') describe = request.form.get('describe') content = request.form.get('content') article = Article(cate_id=cate_id, art_name=art_name, describe=describe, content=content, author=session.get('user_id')) db.session.add(article) db.session.commit() return 'ok' else: cate = [] categories = Category.query.all() for c in categories: if c.parent_id == 0: c.cate_name = '|--' + c.cate_name cate.append(c) for s in categories: if s.parent_id == c.id: s.cate_name = '|------' + s.cate_name cate.append(s) return render_template('admin/add_art.html', cate=cate) else: return redirect(url_for('login'))
def new_article(): """ add new article """ if not authenticated(): return redirect(url_for('login')) if request.method == 'GET': topics, _articles = base_query(db_session) return render_template('article_form.html', is_authenticated=authenticated, topics=topics, article=None, action='new') else: form = dict(request.form) article = Article(title=form['article_title'][0], url=form['article_url'][0], date_added=date.today(), description=form['article_description'][0], topic_id=form['article_topic_id'][0], adder_id=session['user_id']) db_session.add(article) db_session.commit() return redirect(url_for('index'))
def run(self): try: urls_article = [ 'https://mp.weixin.qq.com/s?src=11×tamp=1541559601&ver=1229&signature=ixTsG-RvK8H58t6D-CpW6olWI8hA52Wz-FRb12ZcrNG-lxR20YutoyLYUr-RB3w8WHjE1petjDcbbxZVxTChvPWM27qszWu0Z3zonjx8SEQB5mmgm1O9Eu*5qsFhnBCH&new=1' ] entity = None backpack_list = [] ftp_list = [] ftp_info = None for page_count, url in enumerate(urls_article): # if page_count < 15: # continue html = requests.get(url) # 确定account信息 name = pq(html.text)('#js_name').text() account_name = pq( html.text)('.profile_meta_value').eq(0).text() log('---{}---{}---'.format(name, account_name)) account = Account() account.name = name account.account = account_name account.get_account_id() article = Article() try: article.create(url, account) except RuntimeError as run_error: log('找不到浏览器 {}'.format(run_error)) log('第{}条 文章标题: {}'.format(page_count, article.title)) log("当前文章url: {}".format(url)) entity = JsonEntity(article, account) log('当前文章ID: {}'.format(entity.id)) # if entity.id in ids: # log('当前文章已存在,跳过') # continue backpack = Backpack() backpack.create(entity) backpack_list.append(backpack.create_backpack()) # self.save_to_mysql(entity) self.save_to_mongo(entity.to_dict()) # ftp包 ftp_info = Ftp(entity) name_xml = ftp_info.hash_md5(ftp_info.url) log('当前文章xml: {}'.format(name_xml)) self.create_xml(ftp_info.ftp_dict(), name_xml) ftp_list.append(name_xml) # if page_count >= 3: # break log("发包") # todo 发包超时,修改MTU if ftp_info is not None: entity.uploads_ftp(ftp_info, ftp_list) if entity: # entity.uploads(backpack_list) entity.uploads_datacenter_relay(backpack_list) entity.uploads_datacenter_unity(backpack_list) log("发包完成") except Exception as e: log("解析公众号错误 {}".format(e)) if 'chrome not reachable' in str(e): raise RuntimeError('chrome not reachable')
def frontpage_cms_new(): page = 'CMS' if request.method == "POST": title = request.json['articleTitle'] category = request.json['articleCategory'] summary = request.json['articleSummary'] content = request.json['articleContent'] status = request.json['articleStatus'] if status == 'Published': published_date = datetime.date.today() added_date = datetime.date.today() elif status == 'Draft': published_date = None added_date = datetime.date.today() article = Article(title=title, content=content, author_id=current_user.id, category=category, published_date=published_date, added_date=added_date, summary=summary, status=status) db.session.add(article) db.session.commit() return jsonify(**request.json) else: return render_template('employee_site/front-page/new_article.html', page=page)
def test_add(self): # Create an Article via the Reporter object. new_article = self.r.article_set.create(headline="John's second story", pub_date=datetime(2005, 7, 29)) self.assertEqual(repr(new_article), "<Article: John's second story>") self.assertEqual(new_article.reporter.id, self.r.id) # Create a new article, and add it to the article set. new_article2 = Article(headline="Paul's story", pub_date=datetime(2006, 1, 17)) self.r.article_set.add(new_article2) self.assertEqual(new_article2.reporter.id, self.r.id) self.assertQuerysetEqual(self.r.article_set.all(), [ "<Article: John's second story>", "<Article: Paul's story>", "<Article: This is a test>", ]) # Add the same article to a different article set - check that it moves. self.r2.article_set.add(new_article2) self.assertEqual(new_article2.reporter.id, self.r2.id) self.assertQuerysetEqual(self.r2.article_set.all(), ["<Article: Paul's story>"]) # Adding an object of the wrong type raises TypeError. self.assertRaises(TypeError, self.r.article_set.add, self.r2) self.assertQuerysetEqual(self.r.article_set.all(), [ "<Article: John's second story>", "<Article: This is a test>", ])
def setup(self): """ Set up method that will run before every Test """ self.new_article = Article("nana", "TestTitle", "TestDescription", "TestUrl", "TestUrlToImage", "TestPublishedAt", "TestContent")
def parseAndStoreDoc(self): for e in self.doc.entries: title = jinja2.Markup(e.title).unescape() link = e.links[0]["href"] soup = BeautifulSoup(e.description) try: description = jinja2.Markup(soup.findAll("p")[-1]).unescape() except IndexError as e: description = jinja2.Markup(soup).unescape() # try: # imgLink = soup.findAll("img")[0]["src"] # except IndexError as e: imgLink = GQ.gqLogo srcName = self.srcName labelName = self.srcName.split("_")[1] article = Article(title=title, description=description, imgLink=imgLink, link=link, srcName=srcName, labelName=labelName) article.put() logging.info("Storing data from %s" % srcName)
def editor(article_id): if request.method == 'POST': title = request.form.get("title") content = request.form.get("content") filename = None if 'main_image' in request.files: file = request.files['main_image'] if file and allowed_file(file.filename): filename = secure_filename(file.filename) file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) if article_id: article = Article.get(article_id) if filename: #change filename only if new file uploaded article.edit(article_id, title=title, content=content, main_image=filename) else: article.edit(article_id, title=title, content=content) return redirect('/editor') else: article = Article(title=title, content=content, main_image=filename) article.create() return redirect('/editor') articles = Article.fetch() if article_id: article = Article.get(article_id) else: article = None return render_template('editor.html', articles=articles, article=article)
def parseAndStoreDoc(self): for e in self.doc.entries: title = jinja2.Markup(e.title).unescape() soup = BeautifulSoup(e.description) description = jinja2.Markup(soup.getText()).unescape() #truncate the length of if len(description) > 500: description = description[:500] link = e.links[0]["href"] srcName = self.srcName labelName = self.srcName.split("_")[1] # if no image in entry, use the source logo instead try: imgLink = e.media_content[0]["url"] except AttributeError as e: imgLink = NewYorkTimes.logo article = Article(title=title, description=description, imgLink=imgLink, link=link, srcName=srcName, labelName=labelName) article.put() logging.info("Storing data to database!(from %s)" % srcName)
def post(self, request, *args, **kwargs): if request.session.get('account_id', None) is None: return HttpResponseRedirect('/account/login') try: article_id = kwargs['id'] d = request.POST.dict() i = Article.objects.get(id=article_id) tag = d.get('tag', '') if tag != '': if tag[-1] == ';': tag = tag[:-1] i.tag = tag.split(';') i.category = int(d['category']) i.head_image = d.get('head_image', None) i.title = d['title'] i.content = d['content'] i.status = 0 i.save() except: d = request.POST.dict() i = Article(**d) account_id = request.session['account_id'] i.account_id = str(account_id) i.username = request.session['username'] tag = d.get('tag', None) if tag != '': if tag[-1] == ';': tag = tag[:-1] i.tag = tag.split(';') i.status = 0 i.save() return HttpResponse(i.id)
def save(self, username, article=None): cd = self.cleaned_data title = cd['title'] title_zh = title now = datetime.datetime.now() content_md = cd['content'] content_html = markdown.markdown(cd['content']) re_title = '<h\d>(.+)</h\d' data = content_html.split('\n') for line in data: title_info = re.findall(re_title, line) if title_info: title_zh = title_info[0] break url = '/article/%s' % (title) tags = cd['tags'] article = Article( url=url, title=title, title_zh=title_zh, author=username, content_md=content_md, content_html=content_html, tags=tags, views=0, created=now, updated=now, ) article.save()
def test_reverse_add(self): # Adding via the 'other' end of an m2m a5 = Article(headline='NASA finds intelligent life on Mars') a5.save() self.p2.article_set.add(a5) self.assertQuerysetEqual(self.p2.article_set.all(), [ '<Article: NASA finds intelligent life on Earth>', '<Article: NASA finds intelligent life on Mars>', '<Article: NASA uses Python>', '<Article: Oxygen-free diet works wonders>', ]) self.assertQuerysetEqual(a5.publications.all(), ['<Publication: Science News>']) # Adding via the other end using keywords new_article = self.p2.article_set.create( headline='Carbon-free diet works wonders') self.assertQuerysetEqual(self.p2.article_set.all(), [ '<Article: Carbon-free diet works wonders>', '<Article: NASA finds intelligent life on Earth>', '<Article: NASA finds intelligent life on Mars>', '<Article: NASA uses Python>', '<Article: Oxygen-free diet works wonders>', ]) a6 = self.p2.article_set.all()[3] self.assertQuerysetEqual(a6.publications.all(), [ '<Publication: Highlights for Children>', '<Publication: Science News>', '<Publication: Science Weekly>', '<Publication: The Python Journal>', ])
def scrape_homepage(self, **kwargs): """ Scrape! """ logger.info('Scraping homepage (start time: %s)' % self.run_time) if not kwargs: response = requests.get(self.url) page = PyQuery(response.content) else: page = PyQuery(**kwargs) article_elements = page('.stories-wrap article') slot = 0 articles = [] for el in article_elements: element = PyQuery(el) article = Article(element, self.run_time) if not article.story_id and not article.is_apps_project: continue if not element.hasClass('attachment'): slot += 1 article.slot = slot articles.append(article) logger.info('Scraped %s from homepage (%s)' % (article.story_id, article.headline)) return articles