def edit(news_id=None): if not ModuleAPI.can_write('news'): return abort(403) if news_id: news_item = News.query.get_or_404(news_id) else: news_item = News() form = NewsForm(request.form, news_item) if request.method == 'POST': if form.validate_on_submit(): # fill the news_item with the form entries. form.populate_obj(news_item) # Only set writer if post is brand new if not news_item.id: news_item.user_id = current_user.id db.session.add(news_item) db.session.commit() news_id = news_item.id flash(_('News item saved'), 'success') return redirect(url_for('news.view', news_id=news_id)) else: flash_form_errors(form) return render_template('news/edit.htm', form=form, news_id=news_id)
def test_news_json(self): now = datetime.now() news = News(title="fake-title", teaser="fake-teaser", text="fake-text", date=now) news.save() response = self.client.get("/ajax/news/") self.assertEqual(response.status_code, 200) self.assertContains(response, "fake-title") self.assertContains(response, "fake-teaser") self.assertContains(response, "fake-text")
def pub_news(request): news_content = request.POST['mycontent'] news_title = request.POST['pubnews_title'] dateString = request.POST['nowtime'] first_type = int(request.POST['selType']) second_type = int(request.POST['selType2']) print first_type, second_type admin = Admin.objects.get(admin_id=1) new_news = News( news_title=news_title, news_type=first_type*10 + second_type, news_content=news_content, news_update_time=dateString, news_author=admin, ) new_news.save() return HttpResponseRedirect('/wsn_admin/admin_news')
def test_all(self): all_news = News.all() self.assertEqual(len(all_news), 4) # Checks the order by clause self.assertEqual(all_news[0].news_dt, datetime.date(2011, 8, 15)) self.assertEqual(all_news[1].news_dt, datetime.date(2011, 7, 13)) self.assertEqual(all_news[2].news_dt, datetime.date(2011, 6, 10)) self.assertEqual(all_news[3].news_dt, datetime.date(2011, 5, 27))
def __init__(self): # Grid initialization super(EditNewsGrid, self).__init__(News, News.all()) # Creation of a customized date field to edit the news' date self.append(create_date_field("formatted_news_dt", "news_dt", DT_FORMAT)) # Grid configuration inc = [FORMATTED_DT(self.formatted_news_dt), NEWS(self.news)] self.configure(include=inc)
def GET(self): open_polls = filter(lambda poll: not poll.expired, Poll.all()) return config.views.layout( config.views.index( pending_tournaments(), open_polls, News.all() ) )
def get_news(): now = datetime.now() strnow = now.strftime('%Y-%m-%d %H:%M:%S') print(strnow) # First next run time ''' period = timedelta(days=day, hours=hour, minutes=min, seconds=second) next_time = now + period strnext_time = next_time.strftime('%Y-%m-%d %H:%M:%S') print(strnext_time) ''' pasttime = now - timedelta(days=1) paststr = pasttime.strftime('%Y-%m-%d %H:%M:%S') print(paststr) df = pro.news(src='sina', start_date=strnow, end_date=paststr) print(df) for index, row in df.iterrows(): news = News() news.timestamp = index news.title = row['title'] news.body = row['content'] news.author = 'default' db.session.add(news) db.session.commit() newss = News.query.all() for n in newss: print(n.id, n.body)
def add_news(): title = request.json['title'] description = request.json['description'] status = 0 new_news = News(title, description, status) db.session.add(new_news) db.session.commit() return jsonify(id=new_news.id, title=new_news.title, description=new_news.description)
def _get_news(): id = request.values['id'] news = News.get_by_id(id) comm = news.get_comments_list() res = { 'id': news.news_id, 'title': news.title, 'author': news.get_author().author_name, 'text': news.full_text, 'comments': comm, 'date': news.creation_date } return jsonify(res)
def news_creator(): form = NewsCreatorForm(0) if form.validate_on_submit(): news = News(title=form.title.data, body=form.body.data, img=form.img.data) db.session.add(news) db.session.commit() flash('Новость опубликована') redirect(url_for('news_creator')) #elif request.method == 'GET': #form.username.data = current_user.username #form.about_me.data = current_user.about_me""" return render_template('news.html', title='Редактор новостей', form=form)
def edit_news(id): """ Returns: """ form = NewsEditorForm() news = News() if id > 0: news.set_id(id) news.load() if request.method == "GET": form.init_values(news) if request.method == "POST": if form.validate_on_submit(): news.prepare_form_input(request.form) news.save() else: form.get_error_messages() return render_template("content/news/edit_news.html", form=form)
def add_SOP(): form = SOPForm() if form.validate_on_submit(): sop = SOPPost(title=form.title.data, body=form.body.data) db.session.add(sop) db.session.commit() if form.add_to_news.data is True: news = News(body=_('Added new SOP:') + '<br><a href="' + url_for('SOP.SOP', SOP_id=sop.id, _external=True) + '">' + form.title.data + '</a>', source='SOP-' + str(sop.id)) db.session.add(news) db.session.commit() return redirect(url_for('SOP.SOP_list')) return render_template('add_SOP.html', title=_('Add SOP'), form=form)
def add_law(): form = LawForm() if form.validate_on_submit(): law = LawPost(title=form.title.data, body=form.body.data) db.session.add(law) db.session.commit() if form.add_to_news.data is True: news = News(body=_('Added new order') + '<br><a href="' + url_for('orders.law', law_id=law.id, _external=True) + '">' + form.title.data + '</a>', source='ORD-' + str(law.id)) db.session.add(news) db.session.commit() return redirect(url_for('orders.orders')) return render_template('add_law.html', title=_('Add law'), form=form)
def test_news_database(self): ''' test additions to news database ''' ''' check that we find the correct news by category ''' new1 = News(date='13062020', outlet = 'BBC', category = 'politics', title = 'Boris Johnson test', link = 'link_test', summary = 'this is a test summary', story = 'this is a test story', pic_link = 'this is a test pic_link', langauge = 'en') db.session.add(new1) db.session.commit() allnew = News.query.filter(News.category == politics).all() assert new1 in allnew db.session.delete(new1) db.session.commit()
def update_news(): top_headlines = newsapi.get_top_headlines(q='covid-19', language='en', country='in') articles = top_headlines["articles"] News.query.delete() for article in articles: title = article["title"] description = article["description"] new_news = News(Title=title, Description=description) db.session.add(new_news) db.session.commit()
def search_topics(topic, category): '''function to search through the topics and give news articles using elasticsearch''' #print(f'search topic is: {topic}') news, total = News.search(topic) new_list = [] bbc_list = [] ger_list = [] abc_list = [] guardian_list = [] titles = [] # print("now is test") for new in list(news): link = new.link title = new.title summary = new.summary language = new.language category_database = new.category if len(summary) > 300: summary = summary[0:300] + '...' ## to deal with cartoons ## elif 'cartoon' in title: title = title.replace('- cartoon', '') summary = f'Cartoon: {title}' outlet = new.outlet pic_link = new.pic_link if pic_link == None: pic_link = "https://static.bbc.co.uk/news/1.312.03569/img/brand/generated/news-light.png" if outlet == 'theguardian': outlet = 'The Guardian' summary = title if outlet == 'Ger': outlet = 'Der Spiegel' if outlet == 'Spa': outlet = 'El Pais' if title not in titles: titles.append(title) if category.lower() == category_database.lower(): topic = topic.lower() new_list.append( (link, title, summary, outlet, language, new, pic_link)) return render_template('search_topics.html', title=_('Search'), topic=topic, news=new_list)
def create_news(author, title, content, tags): """ Util function for creating a News object. Function will take parameters passed in from the form and create a News along with the event object. """ news = News(author=author, title=title, content=content, tags=tags) create_object(news) # Create news_created Event event = Events(post_id=news.id, user_id=author, type="news_created", previous_value={}, new_value=news.val_for_events) create_object(event) return news.id
def edit_SOP(SOP_id): edit_SOP_id = SOPPost.query.filter_by(id=SOP_id).first() form = SOPForm(title=edit_SOP_id.title, body=edit_SOP_id.body) if form.validate_on_submit(): edit_SOP_id.title = form.title.data edit_SOP_id.body = form.body.data db.session.commit() if form.add_to_news.data is True: news = News( body=_('SOP is updated') + '<br><a href="' + url_for('SOP.SOP', SOP_id=edit_SOP_id.id, _external=True) + '">' + form.title.data + '</a>', source='SOP-' + str(edit_SOP_id.id)) db.session.add(news) db.session.commit() return redirect(url_for('SOP.SOP_list')) return render_template('add_SOP.html', title=_('Edit SOP'), form=form)
def edit_law(law_id): edit_law_id = LawPost.query.filter_by(id=law_id).first() form = LawForm(title=edit_law_id.title, body=edit_law_id.body) if form.validate_on_submit(): edit_law_id.title = form.title.data edit_law_id.body = form.body.data db.session.commit() if form.add_to_news.data is True: news = News( body=_('Order updated:') + '<br><a href="' + url_for('orders.law', law_id=edit_law_id.id, _external=True) + '">' + form.title.data + '</a>', source='ORD-' + str(edit_law_id.id)) db.session.add(news) db.session.commit() return redirect(url_for('orders.orders')) return render_template('add_law.html', form=form)
def post_news(): form = PostNewsForm() if form.validate_on_submit(): news = News() news.body = form.body.data news.private = form.private.data news.title = form.title.data news.user_id = current_user.id db.session.add(news) db.session.commit() return redirect(url_for('.all_news')) return render_template('main/post_news.html', form=form)
def pub_news(): if request.method == 'GET': return render_template('pub_news.html') else: form = AddNewsForm(request.form) if form.validate(): title = request.form.get('title') select = request.form.get('select') content = request.form.get('ckeditor') author_id = current_user.id news = News(title=title, type=int(select), content=content, author_id=author_id) db.session.add(news) db.session.commit() return redirect(url_for('news')) else: return render_template('pub_news.html', form=form)
def main(): news_sites = {'m24.ru': M24_accidents, 'mosday.ru': Mosday_accidents, 'vm.ru': VM_accidents} # Инициализируем Наташу morph_vocab = MorphVocab() extractor = AddrExtractor(morph_vocab) # Ищем новости, проверяем на наличие адресов, загружаем # во временное хранилище news_list = [] for key in news_sites.keys(): try: ScrapeClass = news_sites.get(key) source = ScrapeClass() rec = get_news(source, extractor) news_list += rec except (TypeError): print("Источник {} недоступен.".format(key)) for item in news_list: published = item['time'] + ' ' + item['date'] published = datetime.strptime(published, '%H:%M %d.%m.%Y') record = News( title=item['title'], link=item['link'], date_and_time=published, text=item['text'], address=item['location']['address'], street=item['location']['street'], lat=item['location']['coordinates'][0], lon=item['location']['coordinates'][1] ) record_in_db = News.query.filter_by(link=item['link']).first() if record_in_db: continue else: db.session.add(record) db.session.commit() '''
def post_news(): form = PostNewsForm() if form.validate_on_submit(): print('1') new = News() new.body = form.body.data new.private = form.private.data new.title = form.title.data new.user_id = current_user.id db.session.add(new) db.session.commit() print('2') return redirect(url_for('main.user_info', id=current_user.id)) return render_template('main/post_news.html', form=form)
def parserNews(self, index): url = 'http://news.ccut.edu.cn/sort.php/1/%s' % index print url try: html = urllib2.urlopen(url).read().decode("gb2312") soup = BeautifulSoup(html, fromEncoding="gb2312") for content in soup.findAll(colspan='2'): title = content.find(target='_blank').string newURL = content.a["href"] time = content.find(color='#666666').string new = News(title=title, url=newURL, datetime=time) db.session.add(new) db.session.commit() print('Success save to database. ---- %s' % title) except Exception, e: print("Error:%s" % e) db.session.rollback() return
def process_sources(news_list): ''' Function that processes the news sources and transform them to a list of Objects Args: news_list: A list of dictionaries that contain news details Returns: news_sources: A list of news objects ''' news_sources = [] for news_item in news_list: name = news_item.get('name') url = news_item.get('url') if name: new_object = News(name,url) news_sources.append(new_object) return news_sources
def add_news(): news_form = NewsForm() if news_form.validate_on_submit(): data = news_form.data news_count = News.query.filter_by(title=data['title']).count() if news_count >= 1: flash('此条资讯已经存在!', 'error') return redirect(url_for('admin.add_news')) else: news = News(cate_id=data['newsCateId'], title=data['title'], content=data['content'], source=data['source'], copyrightNotice=data['copyrightNotice'], publisher=data['publisher']) db.session.add(news) db.session.commit() flash("添加资讯成功!", "okey") redirect(url_for("admin.add_news")) return render_template("admin/addNews.html", form=news_form)
def add_news(): """ Returns: """ form = NewsEditorForm() news = News() news.init_default() news.save() return render_template("content/news/add_news.html", form=form, form_object=news)
def process_results(news_list): # ''' # Function that processes the movie result and transform them to a list of Objects # Args: # news_list = A list of news items with descriptions # Returns: # news_results = a list of news objects # ,,, news_results = [] for news_item in news_list: id = news_item.get('id') title = news_item.get('name') overview = news_item.get('description') news_object = News(id, title, overview) news_results.append(news_object) return news_results
def parse_antara(url): """Return = None Parameter: url = url page yang ingin di scrape Melakukan scraping thd page indeks antaranews.com """ htree = request_page(url) title = htree.xpath( '//article[@class="simple-post simple-big clearfix"]/header/h3/a/@title' ) link = htree.xpath( '//article[@class="simple-post simple-big clearfix"]/header/h3/a/@href' ) for i, l in enumerate(link): if check_duplicate(l): htree = request_page(l) article_ts = htree.xpath('//span[@class="article-date"]/text()') # XPath bagian kiri untuk page berita biasa Antara # XPath bagian kanan untuk page berita Video Antara content = htree.xpath( '//div[@class="post-content clearfix"]/text()|//div[@class="margin-top-15 margin-bottom-15 clearfix"]/text()|//div[@class="flex-caption"]/text()' ) db.session.add( News(title=title[i], url=l, summary=content if isinstance(content, str) else content[0], content=join_string(content), article_ts=convert_date(article_ts[0], unix=True, antara=True), published_date=convert_date(article_ts[0], antara=True)))
def setUp(self): ''' Set up method that will run before every Test ''' self.new_news = News('title', 'Corona virus vaccines have been found', '"http://www.bbc.co.uk/news', 'image')
def setUp(self): self.new_source = News('bbc', 'image', 'testing image')
def user_news_release(): from app.models import Category, News from app.utils.qiniu.image_storage import storage from app import db if request.method == "GET": # 加载新闻分类数据 categories = [] try: categories = Category.query.all() except Exception as e: current_app.logger.error(e) category_dict_li = [] for category in categories: category_dict_li.append(category.to_dict()) # 移除最新的分类 category_dict_li.pop(0) return render_template('news/user_news_release.html', data={"categories": category_dict_li}) # 取参数 user_id = session["user_id"] # 1. 取到请求参数 source = "个人发布" # 来源 title = request.form.get("title") category_id = request.form.get("category_id") digest = request.form.get("digest") index_image = request.files.get("index_image") content = request.form.get("content") # 参数转换 try: category_id = int(category_id) except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg="参数有误") # 上传七牛云 try: index_image_data = index_image.read() key = storage(index_image_data) except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg="参数有误") # 2. 判断参数 if not all([title, category_id, digest, index_image, content]): return jsonify(errno=RET.PARAMERR, errmsg="参数错误") new = News() new.title = title new.digest = digest new.source = source new.content = content new.index_image_url = "http://pv875q204.bkt.clouddn.com/" + key new.category_id = category_id new.user_id = user_id # 1代表待审核状态 new.status = 1 try: # 插入提交数据 db.session.add(new) db.session.commit() except Exception as e: # 数据库回滚 db.session.rollback() current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg="数据库操作有误") return jsonify(errno=RET.OK, errmsg="OK")
def setUp(self): ''' Set up method that will run before every test ''' self.new_news = News('usa-today','USA Today','Former Balch Springs Officer Roy Oliver, 38, was convicted of fatally shooting Jordan Edwards, 15, after responding to the party.','https://www.usatoday.com/story/news/nation-now/2018/08/29/dallas-area-cop-sentenced-15-years-fatal-shooting-black-teen/1141400002/','general', 'us')
def edit(request): print request.method if request.method == "POST": title = request.POST.get("title", None) content = request.POST.get("content", None) news_id = request.POST.get("news_id", None) delete_tag = request.POST.get("delete_tag", None) print title print content print news_id print delete_tag if delete_tag is None: if title is None or content is None: return HttpResponse("error") # 新建news if news_id is None or news_id == "": print news_id try: news = News() news.title = title news.content = content news.author = "zhaolong" news.read_count = 0 news.save() return HttpResponse("success") except: return HttpResponse("error") # 更新news 内容 else: try: news = News.objects.get(news_id=int(news_id)) news.title = title news.content = content news.author = "zhaolong" news.save() return HttpResponse("success") except: return HttpResponse("error") # 删除news else: if news_id is None: return HttpResponse("error") try: news = News.objects.get(news_id=int(news_id)) news.delete() return HttpResponse("success") except: return HttpResponse("error") elif request.method == "GET": try: news_id = int(request.GET.get("nid", -1)) news=News.objects.get(pk=news_id) form = UEditorTestModelForm(instance= news) except Exception, e: print str(e) form = TestUEditorForm( initial={'content': '请在此输入文字'} ) news = None return render(request, "adminer/editNews.html", { 'form': form, 'news': news, })
def setUp(self): ''' set up method that will run before every test ''' self.new_news = News("corona", "corona is a virus",'"https://fivethirtyeight.com/features/jamal-murray-isnt-the-new-steph-curry-but-he-might-be-close/', ' "https://fivethirtyeight.com/wp-content/uploads/2020/09/GettyImages-1094743508-e1600742098339.jpg?w=575','2020-09-22T18:00:00Z')
def setUp(self): ''' Set up method that will run before every Test ''' self.new_news = News("Bitstop","Bitstop dévoile la nouvelle génération de distributeurs automatiques de bitcoin, lors de la conférence EuroCIS","MIAMI, 18 février 2019 /PRNewswire/ -- Bitstop, pionnier en matière de distributeurs automatiques de bitcoin (Bitcoin ATM), s'est associé avec KIOSK Information Systems (KIOSK), le plus important fournisseur de solutions personnalisées en libre-service en Amé…","https://www.prnewswire.com/news-releases/bitstop-devoile-la-nouvelle-generation-de-distributeurs-automatiques-de-bitcoin-lors-de-la-conference-eurocis-844172769.html","https://mma.prnewswire.com/media/628363/Bitstop_Logo.jpg?p=facebook", "2019-02-18T06:03:00Z","La plateforme logicielle des distributeurs automatiques de bitcoin Bitstop, associée au matériel de pointe de KIOSK, établit une nouvelle référence dans le secteur des distributeurs automatiques de bitcoin qui, parti de rien, est passé à plus de 4000 distribu… [+2955 chars]")
def setUp(self): ''' Set up method that will run before every Test '''" self.new_news = News(id,"name","description","url","category","country","language")