def make_test_article(self): article = Article() article.title = 'Article on something.' article.lead_text_trevor = put_text_in_trevor('abc') article.main_text_trevor = put_text_in_trevor('abc') article.author = self.user return article
def _get_rmrb_article(self, content): medium = Medium.objects.get(pk=1081) article = Article() article.medium = medium article.title = content.find('h1').text.strip() for author_name in content.find_all('div', {'class' : 'summary'})[0].find_all('a'): try: author = Journalist.objects.get(medium=medium, name=author_name.text.strip()) except: pass else: article.author = author break for li in content.find_all('div', {'class' : 'summary'})[-1].find_all('li'): if li.text.find(u'报纸日期') != -1: p = re.compile('(\d+)-(\d+)-(\d+)') publication_date = p.search(li.text).group() if li.text.find(u'版名') != -1: page = li.text.replace('\n','').replace(u'【版名】', '').replace(' ', '') else: page = '头版' article.issue = self._get_issue_from_date(publication_date, 'rmrb') article.page = page article.publication_date = datetime.datetime.strptime(publication_date, '%Y-%m-%d') article, created = Article.objects.get_or_create(medium=article.medium, title=article.title, issue=article.issue, publication_date=article.publication_date) print article.title return article
def _get_nfzm_article(self, url, date, issue): medium = Medium.objects.get(pk=951) article = Article() article.medium = medium article.issue = issue article.url = url article.publication_date = date r = requests.get(url, cookies={'PHPSESSID': 'l19dgbf6ticijmo9ka9osvufk0'}) content = bs4.BeautifulSoup(r.content) article.title = content.title.string.split('-')[-1].strip() article.content = content.find('section', {'id' : 'articleContent'}).text author = content.find('span', {'class' : 'author'}).find_all('em') if author[1].text.find(u'南方周末记者') != -1: author, created = Journalist.objects.get_or_create(medium=medium, name=author[2].text.strip()) if not created: article.author = author elif author[1].text.find(u'南方周末特约撰稿') != -1: article.author_name = author[2].text.strip() elif author[1].text.find(u'南方周末编辑部') != -1: article.author_name = u'南方周末编辑部' print article.author or article.author_name return article
def post(self, request): article = Article() article.author = request.user form = ArticleForm(request.POST, request.FILES, instance=article) if form.is_valid(): article = form.save() return redirect('user_articles_detail.html', self.id)
def addArticle(request): article = Article() article.title = request.POST.get('title', '') article.abstract = request.POST.get('abstract', '') article.category_id = request.POST.get('categorie'); article.content = request.POST.get('content', '') article.author = User.objects.get(id=request.user.id) try: article.save() return HttpResponseRedirect('/articles/read') except: return HttpResponseRedirect('/')
def create_article(request): params = request.POST number = params.get('number') name = params.get('name') firstname, lastname = params.get('author').split(' ') author = get_object_or_404(Author, firstname=firstname, lastname=lastname) new_article = Article() new_article.author = author new_article.name = name new_article.number = number new_article.save() return article(request, 42)
def post(self, request): article_name = request.POST.get("article_name", "") desc = request.POST.get("desc", "") detail = request.POST.get("detail", "") art_len = request.POST.get("art_len", "dp") read_times = request.POST.get("read_times", "") category = request.POST.get("category", "") tag = request.POST.get("tag", "") author = request.POST.get("author", "") youneed_know = request.POST.get("youneed_know", "") content = request.POST.get("content", "") lesson = request.POST.get("lesson", "") article_org = request.POST.get("article_org", 10) # image_form = ArticleImageForm(request.POST or None, request.FILES or None) image = request.POST.get("image", "/articles/201804/IMG_1084.JPG") if content: post_article = Article() post_article.desc = desc post_article.detail = detail post_article.art_len = art_len post_article.name = article_name post_article.read_times = read_times post_article.category = category post_article.tag = tag author = Author.objects.get(id=int(author)) post_article.author = author post_article.youneed_know = youneed_know article_org = ArticleOrg.objects.get(id=article_org) post_article.article_org = article_org post_article.image = image post_article.save() post_content = Content() post_content.name = article_name post_content.art_content = content lesson = Lesson.objects.get(id=int(lesson)) post_content.lesson = lesson post_content.save() return HttpResponse('{"status":"success","msg":"提交成功"}', content_type='application/json') else: return HttpResponse('{"status":"fail","msg":"提交失败"}', content_type='application/json')
def form(request, action, id='-1'): form = ArticleForm try: article = Article.objects.get(id=int(id)) frm = form( initial={ 'author': article.author, 'title': article.title, 'body': article.body, 'preview': article.preview, 'tags_string': article.tags_string, }) except Article.DoesNotExist: article = Article() frm = form() if request.method == 'POST': frm = form(request.POST) if frm.is_valid(): cd = frm.cleaned_data article.author = cd['author'] article.title = cd['title'] article.body = cd['body'] article.preview = cd['preview'] article.tags_string = cd['tags_string'] if action == 'add': article.created = datetime.now() article.modified = datetime.now() article.save() return HttpResponseRedirect('/articles') #return render_to_response('works/result.html', {'post': work }) return direct_to_template(request, 'articles/form.html', { 'form': frm, 'article': article, 'action': action })
def changeBD(request): items_news = [] db = MySQLdb.connect(host="localhost", user="******", passwd="1(8Ik<7YgV", db="giyyan_prasby", charset='utf8') cursor = db.cursor() # запрос к БД sql = """SELECT author, title, preview, body, created FROM `articles_article_old`""" # выполняем запрос cursor.execute(sql) # получаем результат выполнения запроса data = cursor.fetchall() # перебираем записи for rec in data: # извлекаем данные из записей - в том же порядке, как и в SQL-запросе author, title, preview, body, created = rec # добавляем item = Article() item.author = author item.title = title item.title_by = title item.preview = preview item.preview_by = preview item.body = body item.body_by = body item.save() items_news.append(item) # закрываем соединение с БД db.close() return direct_to_template(request, 'tools/change.html', {'items_news': items_news})
def setUp(self): self.client = Client() Page.objects.create(title='home', slug='home', order=1, visible_in_menu=True) for item in range(12): news_item = NewsItem() news_item.title = f'newsitem_title {item}' news_item.description = f'news description {item}' news_item.author = Author.objects.create( last_name='last_name', first_name='news first_name') news_item.published = True news_item.publication_date = timezone.now() news_item.save() for item in range(6): event = Event() event.title = f'event_title {item}' event.date = timezone.now() + timedelta(days=1) event.short_description = f'desciption {item}' event.save() for item in range(6): gallery = Gallery() gallery.title = f'gallery_title {item}' gallery.author = GalleryAuthor.objects.create( last_name='last_name', first_name='gallery first_name') gallery.date = timezone.now() gallery.save() for item in range(6): article = Article() article.title = f'article_title {item}' article.date = timezone.now() article.category = Category.objects.create(title='category', slug='slug-0') article.author = ArticleAuthor.objects.create( last_name='last_name', first_name='article first_name') article.save()
def handle(self, *args, **options): companies = open("dictionaries/companies").readlines() fnames = open("dictionaries/fnames").readlines() lnames = open("dictionaries/lnames").readlines() topics = open("dictionaries/topics").readlines() #titles = ["thi", "tha"]#open("dictionaries/titles").readlines() titles = open("dictionaries/titles").readlines() random.shuffle(titles) titles = titles[1:100] for title in titles: a = Article() a.customer = random.choice(companies).rstrip() a.author = random.choice(fnames).rstrip() + " " + random.choice(lnames).rstrip() a.topic = random.choice(topics).rstrip() a.pub_date = timezone.now() a.title = title.rstrip() a.save() print(str(len(titles)) + " articles added")
def form(request, action, id='-1'): form = ArticleForm try: article = Article.objects.get(id=int(id)) frm = form(initial={ 'author': article.author, 'title': article.title, 'body': article.body, 'preview': article.preview, 'tags_string': article.tags_string, }) except Article.DoesNotExist: article = Article() frm = form() if request.method == 'POST': frm = form(request.POST) if frm.is_valid(): cd = frm.cleaned_data article.author = cd['author'] article.title = cd['title'] article.body = cd['body'] article.preview = cd['preview'] article.tags_string = cd['tags_string'] if action == 'add': article.created = datetime.now() article.modified = datetime.now() article.save() return HttpResponseRedirect('/articles' ) #return render_to_response('works/result.html', {'post': work }) return direct_to_template(request, 'articles/form.html', {'form': frm, 'article': article, 'action': action})
def changeBD(request): items_news = [] db = MySQLdb.connect(host="localhost", user="******", passwd="1(8Ik<7YgV", db="giyyan_prasby", charset="utf8") cursor = db.cursor() # запрос к БД sql = """SELECT author, title, preview, body, created FROM `articles_article_old`""" # выполняем запрос cursor.execute(sql) # получаем результат выполнения запроса data = cursor.fetchall() # перебираем записи for rec in data: # извлекаем данные из записей - в том же порядке, как и в SQL-запросе author, title, preview, body, created = rec # добавляем item = Article() item.author = author item.title = title item.title_by = title item.preview = preview item.preview_by = preview item.body = body item.body_by = body item.save() items_news.append(item) # закрываем соединение с БД db.close() return direct_to_template(request, "tools/change.html", {"items_news": items_news})