Exemple #1
0
 def get(self, direction = 'next', page = '2', base_id = '1'):
     if page == '1':
         self.redirect(BASE_URL)
         return
     objs = Article.get_page_posts(direction, page, base_id)
     if objs:
         if direction == 'prev':
             objs.reverse()            
         fromid = objs[0].id
         endid = objs[-1].id
     else:
         fromid = endid = ''
     
     allpost =  Article.count_all_post()
     allpage = allpost/EACH_PAGE_POST_NUM
     if allpost%EACH_PAGE_POST_NUM:
         allpage += 1
     output = self.render('index.html', {
         'title': "%s - %s | Part %s"%(SITE_TITLE,SITE_SUB_TITLE, page),
         'keywords':KEYWORDS,
         'description':SITE_DECR,
         'objs': objs,
         'cats': Category.get_all_cat_name(),
         'tags': Tag.get_hot_tag_name(),
         'page': int(page),
         'allpage': allpage,
         'listtype': 'index',
         'fromid': fromid,
         'endid': endid,
         'comments': Comment.get_recent_comments(),
         'links':Link.get_all_links(),
     },layout='_layout.html')
     self.write(output)
     return output
Exemple #2
0
def create_article(request, block_id):
    block_id = int(block_id)
    block = Block.objects.get(id=block_id)
    if request.method == 'GET':
        return render_to_response('article_create.html', {'blk': block},
                                  context_instance=RequestContext(request))
    else:  # request.method == 'POST'
        title = request.POST['title'].strip()
        content = request.POST['content'].strip()
        if not title or not content:
            messages.add_message(request, messages.ERROR, u'标题和内容均不能为空')
            # 这是Django的消息系统,所有消息都是从后端添加的,这里表示把消息放到用户的请求里面去
            return render_to_response('article_create.html',
                                      {'blk': block, 'title': title, 'content': content},
                                      # 如果没有标题或者文章没有内容,这里又把刚才创建文章的页面渲染了一遍,把原来用户填的内容又渲染回去了,
                                      # 在页面上重新把内容填到对应的输入框里,免得用户还要再填一遍。
                                      context_instance=RequestContext(request))
            # 这里把用户请求的环境再传到模板中,上面已经传入错误信息的请求就返回给页面了,由页面来控制消息显示
            # Django的消息其实是存在数据库里面的,这里传给页面后页面如果不展示,消息会一直保留
            # 直到某个页面写了展示消息的代码,这个时候会把所有保留的消息一下展示出来
        # owner = User.objects.all()[0]
        # 这里因为还没有用户体系,先指定所有文章的作者为管理员
        # 井号+空格+TODO空格+事项  有些IDE在你写了这种格式之后都可以调出来一个待办事项列表,TODO后边如果写了什么都会
        # 出现在待办事项列表里,每次项目快结题之前都看一下待办事项列表里面的事是不是都做完了
        new_article = Article(block=block, owner=request.user, title=title, content=content)
        # 这里实例化了一个Article,Article是models.py里定义的数据模型
        # block是通过block_id取到的板块,owner是指定的管理员,title和content都是POST传进来的值
        new_article.save()
        # .save()方法是把这个对象保存,如果是新增对象,对应数据库里的操作就是新增一条记录,
        # 如果是通过get方法的到的一个旧有的对象,对应数据库里的就是update操作。
        messages.add_message(request, messages.INFO, u'成功发表文章.')
        return redirect(reverse('article_list', args=[block.id]))
Exemple #3
0
def get_article(id, user=None):
    """Gets a single article (formatted by json_data) from redis or postgresql
    And performs some pre-filters for the current user
    Args:
        id (int): id
        user (User): the current user
    Returns:
        None if the article is not existed or a dict of article data
    """
    id = int(id)
    key = Article.cache_key(id)
    redis_client = get_client(key)

    article = None
    if not redis_client.exists(key):
        article = Article.find_by_pk(id)
        if article:
            data = article.json_data()
            redis_client.set(key, json.dumps(data))
            article = data
    else:
        if redis_client.type(key) == 'hash':
            print redis_client
        article = json.loads(redis_client.get(key))

    article['user_vote'] = Article.check_vote(user, article['id'])

    return article
Exemple #4
0
def sec_list(request,domain=None):
    """.. :py:method::
    webanan的各下属分类的views方法
    """
    domains_list = []
    domain_list = Domain.objects().all()
    for do in domain_list:
        domains_list.append(do.name)
    title = u'{}分类文章数量'.format(domain)
    per_dict = {}
    domain_obj = Domain.objects(name=domain).first()
    if domain_obj:
        categories = Category.objects(belong_Domain=domain_obj).all()
        article_all = 0
        for cate in categories:
            article_all = article_all + Article.objects(belong_cate=cate).count()
        for cate in categories:
            article_num = Article.objects(belong_cate=cate).count()
            per_dict[cate.name] = float(u"%.2f" % (float(article_num)/article_all))
        per_dict = OrderedDict(sorted(per_dict.items(), key=lambda t: t[1]))
    else:
        raise Http404
    return render_to_response('webanan_list.html',{
                'title': title,
                'per_dict':per_dict,
                'domains':domains_list,
                })
Exemple #5
0
    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
Exemple #6
0
    def get(self):
        auth_level = self.is_user_authorized()
        if auth_level == models.AUTH_LEVEL_REGISTERED_USER:
            self.redirect('/account/activate/reminder/')
        elif auth_level == models.AUTH_LEVEL_ACTIVATED_USER:
            today = datetime.utcnow()

            request_year = self.request.get('year')
            request_month = self.request.get('month')

            if not request_year or not request_month:
                year = today.year
                month = today.month
                articles = Article.get_latest_published_student()
            else:
                year = dec(request_year)
                month = dec(request_month)
                articles = Article.get_all_published_student_articles_for_month(year, month)
            books = Book.get_latest(count=10)
            response = render_template('blog_students.html',
                year_list=models.BLOG_YEAR_LIST,
                month_list=models.MONTH_LIST,
                today=today,
                requested_year=year,
                requested_month=month,
                articles=articles,
                books=books)
            self.response.out.write(response)
        else:
            response = render_template('index.html')
            self.response.out.write(response)
Exemple #7
0
def create_new_content(idx):
    author = users[random.randrange(0, len(users) - 1)]
    created_at = datetime.datetime.utcnow() - datetime.timedelta(hours=3 * idx)
    type = (
        CONTENT_TYPE.ARTICLE
        if author["type"] == "public"
        else random.choice([CONTENT_TYPE.ARTICLE, CONTENT_TYPE.PROJECT])
    )

    loc = choose_loc()
    doc = {
        "type": type,
        "title": unicode(titles[random.randrange(0, len(titles) - 1)]),
        "content": unicode(contents[random.randrange(0, len(contents) - 1)]),
        "tags": choose_tags(),
        "created_at": created_at,
        "lat": loc[0],
        "lng": loc[1],
    }
    if doc["type"] == CONTENT_TYPE.ARTICLE:
        doc["excerpt"] = titles[random.randrange(0, len(titles) - 1)]
        new_content = Article()
    else:
        new_content = Project()
        doc["need_donation"] = True
        doc["goal"] = unicode(titles[random.randrange(0, len(titles) - 1)])

    if random.randrange(1, 30) == 9:
        doc["featured"] = True
        print("X", end="")
    new_content.save(doc, author)
Exemple #8
0
def article_save():
    mid = request.form.get('id')
    title = request.form.get('title', '')
    content = request.form.get('content', '')
    
    if (mid == None or mid == ''):
        article = Article(title=title, content=content, add_time=datetime.now(), edit_time=datetime.now())
        for cid in request.form.getlist("tags"):
            article.tags.add(Tag.query.get(cid))
        for cid in request.form.getlist("categories"):
            article.categories.add(Category.query.get(cid))
        db.session.add(article)
    else:
        article = Article.query.get(int(mid))
        article.title=title
        article.content=content
        article.edit_time=datetime.now()
        article.categories.clear()
        article.tags.clear()
        for cid in request.form.getlist("tags"):
            article.tags.add(Tag.query.get(cid))
        for cid in request.form.getlist("categories"):
            article.categories.add(Category.query.get(cid))
            
    db.session.commit()
    flash('保存成功!', 'success')
    return redirect(url_for('.article',page=1))
Exemple #9
0
    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>',
            ])
Exemple #10
0
    def store(self, topic_name, article_entry_list):
        """Stores the article entries in the database.

        :param topic_name The name of the topic
        :param article_entry_list The list of entries of this topic
        """

        try:
            # Try to retrieve the topic to see if it exists already
            topic = Topic.get(Topic.name == topic_name)
        except Topic.DoesNotExist:
            # If not, create it
            topic = Topic.create(name=topic_name)

        # Go through all the articles in this topic
        for article_entry in article_entry_list:
            article_name = article_entry.subject

            # If there is no subject, it means the article is corrupted
            # therefore we skip it
            if not article_name:
                continue

            # Create the files with the content
            # Overwrite existing files
            try:
                Article.create(topic=topic, subject=article_name,
                               body=article_entry.body)
            except Article.DoesNotExist:
                pass
Exemple #11
0
def post_article(request,
                 success_url = "/",
                 template="articles/post.html"):
    """ Post new article """
    if request.method == 'POST':
        form = ArticleForm(request.POST)
        if form.is_valid():
            title = form.cleaned_data['title']
            content = form.cleaned_data['content']
            tags = form.cleaned_data['tags']
            # new article
            art = Article(title=title,content=content,slug=slugify(title),author=request.user)
            art.save()
            # add tags
            for tag in tags:
                try:
                    t = Tag.objects.get(name=tag)
                except Tag.DoesNotExist:
                    t = None
                # if t doesnt exist
                if t is None:
                    t = Tag(name=tag,slug=slugify(tag))
                    t.save()
                # add the tag to the article
                art.tags.add(t)
            # set meta
            art.set_meta()
            # done here
            return HttpResponseRedirect(success_url)
    else:
        form = ArticleForm()
    
    return render_to_response(template,
                              {'form':form},
                              context_instance=RequestContext(request))
Exemple #12
0
    def createArticle(self, request):
        """Create new Article object, returning ArticleForm/request."""
        
        for required in ['title', 'content']:
            if not getattr(request, required):
                raise endpoints.BadRequestException("Article '%s' field required" % required)

        # copy ArticleForm/ProtoRPC Message into dict
        data = {field.name: getattr(request, field.name) for field in request.all_fields()}

        if data['view'] == None:
            del data['view']
        else:
            data['view'] = str(data['view'])

        author = self._getAuthorFromUser()
        data['authorName'] = author.displayName

        article_id = Article.allocate_ids(size=1, parent=author.key)[0]
        article_key = ndb.Key(Article, article_id, parent=author.key)
        data['key'] = article_key

        # create Article
        article_key = Article(**data).put()

        # send email to author confirming creation of Article
        taskqueue.add(params={'email': author.mainEmail,
            'ArticleInfo': repr(request)},
            url='/tasks/send_confirmation_email'
        )

        return self._copyArticleToForm(article_key.get(), author=author)
    def post(self):
        d = json.loads(self.request.body)
        if d['secret'] == '16jutges16':
            if 'key' in d and len(d['key']) > 10: #Keys are looong.
                article = ndb.Key(urlsafe = d['key']).get()
                logging.info('Modifying entry {}'.format(d['key']))
            else:
                article = Article()

            article.slug     = d['slug']
            article.title    = d['title']
            article.text     = d['text']
            if 'when' in d:
                # Matches isoformat into datetime object. Found in stackoverflow
                article.when = datetime.datetime(
                    *map(int, re.split('[^\d]', d['when'])[:-1])
                )
            else:
                article.when = datetime.datetime.now()

            try:
                article.keywords = string.split(d['keywords'])
            except AttributeError:
                article.keywords = d['keywords']
            
            article.put()
Exemple #14
0
def create():
    if 'email' not in session:
        return redirect(url_for('index'))
    person = Person.query.filter_by(email=session['email']).first()
    if person:
        article = Article()
        print 'Article created'
        form = ArticleCreateForm()
        print 'Form created'
        name = person.firstname
        print 'name assigned'
        form.person_name.data = person.firstname
        if form.validate_on_submit():
            print 'inside article post'
            form.populate_obj(article)
            url = form.url.data
            print url
            if url:
                arch_local = image(url, person.firstname)
                article.arch_local = arch_local
            db.session.add(article)
            db.session.commit()
            return redirect(url_for('index', name=name))
        return render_template('create.html', form=form, person=person, name=name)
    return redirect(url_for('index'))
def list():
    is_active = request.args.get('is_active', 1);   
    is_active = int(is_active) 
    is_active = is_active == 1

    user = g.user

    if not user:
        is_active = True
    elif user and not user.has_permission('approve_article'):
        is_active = True

    limit = int(request.args.get('limit', 10))
    offset = int(request.args.get('offset', 0))

    order = request.args.get('order', '-date_created')

    if is_active:
        articles = cache.get_articles(user=user, sort_by=order, limit=limit, offset=offset)
    else:
        order_strings = process_order_input(order)
        articles = Article.list(is_active=is_active, limit=limit, offset=offset, order=order_strings, json=True)
    
    pagination = {
        "total": Article.count(),
        "offset": int(offset),
        "limit": int(limit)
    }

    return jsonify(dict(data=articles, pagination=pagination))
Exemple #16
0
def article_save(request):
    '''
        save article which be sent from article_record page
    '''
    #import time
    #now = time.strftime('%Y-%m-%d %H:%M')
    c={}
    try:
        
        article = Article(title=request.POST.get('title'),author=request.POST.get('author'),\
                    content=request.POST.get('content'))
        article.save()
    
        c = {
             'result':'保存成功!',
             'return_code':0,
        }
    
    except:
        c = {
             'result':'Oops!!!好像出了点差错!点书正在努力反省中~~~',
             'return_code':1,
        }
        
        return render_to_response('article_record/article_save.html',c)

    return render_to_response('article_record/article_save.html',c)
Exemple #17
0
def all_list(request):
    """.. :py:method::
    webanan的主页views方法
    """
    domains_list = []
    domain_list = Domain.objects().all()
    for do in domain_list:
        domains_list.append(do.name)
    title = u'各主机文章数量'
    per_dict = {}
    domains = Domain.objects().all()
    article_all = Article.objects().count()
    for domain in domains:
        categories = Category.objects(belong_Domain=domain).all()
        domain_count = 0
        
        for cate in categories:
            article_num = Article.objects(belong_cate=cate).count()
            domain_count = domain_count + article_num
        per_dict[domain.name] = float(u"%.2f" % (float(domain_count)/article_all))
    per_dict = OrderedDict(sorted(per_dict.items(), key=lambda t: t[1]))
    return render_to_response('webanan_list.html',{
        'title': title,
        'per_dict':per_dict,
        'domains':domains_list,
        })
Exemple #18
0
 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()
Exemple #19
0
    def test_field_defaults(self):
        a = Article()
        now = datetime.now()
        a.save()

        self.assertTrue(isinstance(a.id, (int, long)))
        self.assertEqual(a.headline, "Default headline")
        self.assertTrue((now - a.pub_date).seconds < 5)
Exemple #20
0
 def get(self):
     posts = Article.get_post_for_homepage()
     output = self.render('index.xml', {
                 'posts':posts,
                 'site_updated':Article.get_last_post_add_time(),
             })
     self.set_header('Content-Type','application/atom+xml')
     self.write(output)        
Exemple #21
0
  def post(self):
    article = Article(title=self.request.POST['article-title'], content=self.request.POST['article-content'])
    article_key = article.put()

    # Invalidate the article list in memcache, It will get rebuilt next time the front page is loaded
    memcache.delete("articles_list")

    return self.redirect('/article/%d' % article_key.id(), body="Thanks for your submission!")
Exemple #22
0
def loaddir(directory, clear=False):
    if clear:
        Article.objects.all().delete()

    queue = os.listdir(directory)
    urls = set()
    while queue:
        artfile = queue.pop()
        if artfile[0] == '.': continue
        if artfile in ('template', 'template.rst', 'template.txt'): continue
        artfile = path.join(directory, artfile)
        if path.isdir(artfile):
            queue.extend([
                path.join(artfile,f) for f in os.listdir(artfile)
                ])
            continue

        input = file(artfile)
        header = {}
        linenr = 0
        while True:
            line = input.readline().strip()
            linenr += 1
            if line in ('---', '..'): break
            if line.find(':') < 0:
                raise IOError('gitcms.pages.load: In file %s, line %s. No \':\' found!' % (artfile, linenr))
            tag,value = line.split(':',1)
            value = value.strip()
            header[tag] = value
        blank = input.readline()
        linenr += 1
        if blank.strip():
            raise IOError, 'Blank line expected while processing file (%s:%s)\nGot "%s"' % (artfile, linenr,blank)
        content = input.read()
        content = preprocess_rst_content(content)

        url = header['url']
        if url and url[-1] == '/':
            import warning
            warning.warn('''\
gitcms.pages.loaddir: Removing / at end of url (%s)

(Both versions will work for accessing the page.)
''' % url)
            url = url[:-1]

        if url in urls:
            raise IOError('gitcms.pages.loaddir: repeated URL detected (%s)' % url)

        taglist = []
        for c in header.get('categories','').split():
            taglist.append(tag_for(c))
        # if we got so far, implies that our article is safe to store.
        urls.add(url)
        A = Article(title=header['title'], url=url, author=header.get('author', ''), content=content)
        A.save()
        for c in taglist:
            A.tags.add(c)
Exemple #23
0
def success(request):
	name=request.session.get('author_name')
	at = Article(author_name=name, article_title=request.GET['title'], article_content=request.GET['content'])
	at.save()
	fp = open('demo/templates/success.html')
	t = Template(fp.read)
	fp.close()
	html = t.render(Context({"title":at.article_title}))
	return HttpResponse(html)
Exemple #24
0
 def test_unicode_data(self):
     # Unicode data works, too.
     a = Article(
         headline=u'\u6797\u539f \u3081\u3050\u307f',
         pub_date=datetime(2005, 7, 28),
     )
     a.save()
     self.assertEqual(Article.objects.get(pk=a.id).headline,
         u'\u6797\u539f \u3081\u3050\u307f')
Exemple #25
0
 def test_microsecond_precision(self):
     # In PostgreSQL, microsecond-level precision is available.
     a9 = Article(
         headline='Article 9',
         pub_date=datetime(2005, 7, 31, 12, 30, 45, 180),
     )
     a9.save()
     self.assertEqual(Article.objects.get(pk=a9.pk).pub_date,
         datetime(2005, 7, 31, 12, 30, 45, 180))
Exemple #26
0
 def test_manually_specify_primary_key(self):
     # You can manually specify the primary key when creating a new object.
     a101 = Article(
         id=101,
         headline='Article 101',
         pub_date=datetime(2005, 7, 31, 12, 30, 45),
     )
     a101.save()
     a101 = Article.objects.get(pk=101)
     self.assertEqual(a101.headline, u'Article 101')
Exemple #27
0
 def test_microsecond_precision_not_supported(self):
     # In MySQL, microsecond-level precision isn't available. You'll lose
     # microsecond-level precision once the data is saved.
     a9 = Article(
         headline='Article 9',
         pub_date=datetime(2005, 7, 31, 12, 30, 45, 180),
     )
     a9.save()
     self.assertEqual(Article.objects.get(id__exact=a9.id).pub_date,
         datetime(2005, 7, 31, 12, 30, 45))
Exemple #28
0
    def test_article_defaults(self):
        # No articles are in the system yet.
        self.assertEqual(len(Article.objects.all()), 0)
        
        # Create an Article.
        a = Article(id=None)

        # Grab the current datetime it should be very close to the
        # default that just got saved as a.pub_date
        now = datetime.now()

        # Save it into the database. You have to call save() explicitly.
        a.save()

        # Now it has an ID. Note it's a long integer, as designated by
        # the trailing "L".
        self.assertEqual(a.id, 1L)

        # Access database columns via Python attributes.
        self.assertEqual(a.headline, u'Default headline')

        # make sure the two dates are sufficiently close
        self.assertAlmostEqual(now, a.pub_date, delta=timedelta(5))

        # make sure that SafeString/SafeUnicode fields work
        a.headline = SafeUnicode(u'Iñtërnâtiônàlizætiøn1')
        a.save()
        a.headline = SafeString(u'Iñtërnâtiônàlizætiøn1'.encode('utf-8'))
        a.save()
Exemple #29
0
def submitarticle(request):
	error_msg = u"No POST data sent."	
	if request.method == "POST":
		post = request.POST.copy()
		if post.has_key('tags') and post.has_key('title') and post.has_key('url'):
			try:
				url = post['url']
				art = Article()
				art.tags = post['tags'].strip().split(',')
				art.author = str(request.user)
				art.title = post['title']
				art.votecount = 0
				art.viewcount = 0
				if not url.startswith('http://'):
					url = 'http://'+url
				art.url = url
				if art.url and art.title and art.author:
					try:
						person = getPerson(request)
						person.timesArticle = person.timesArticle + 1
						person.lastActive = datetime.datetime.now()
						rating = Score.objects(type='submitarticle')[0].value
						person.currentRating = person.currentRating + rating
						person.save()
						incrementStat('articles',1)
						art.save()
					except Exception as inst:
						print inst
				return HttpResponseRedirect('/')
			except:
				return HttpResponseServerError('wowza! an error occurred, sorry!')
		else:
			error_msg = u"Insufficient POST data (need 'content' and 'title'!)"
	return HttpResponseServerError(error_msg)
Exemple #30
0
def unsubscribe(rss_id):
    '''Logic to unsubscribe a user from an rss feed, and all articles from that feed'''
    try:
        feed_to_be_removed = Feed.objects.get(id = rss_id)
        #atomically remove feed subscription from current user
        User.objects(id = current_user.id).update_one(pull__subscriptions__feed_id = feed_to_be_removed.id)
        #atomically remove articles from unsubscribed feed for current user
        Article.objects(feed_id = feed_to_be_removed.id).update(pull__readers__user_id = current_user.id)
        return jsonify(dict(status = 'Success'))
    except DoesNotExist:
        return jsonify(dict(status = 'Error', message = 'Given feed does not exist'))
Exemple #31
0
class ManyToOneTests(TestCase):
    def setUp(self):
        # Create a few Reporters.
        self.r = Reporter(first_name='John', last_name='Smith', email='*****@*****.**')
        self.r.save()
        self.r2 = Reporter(first_name='Paul', last_name='Jones', email='*****@*****.**')
        self.r2.save()
        # Create an Article.
        self.a = Article(id=None, headline="This is a test",
                         pub_date=datetime(2005, 7, 27), reporter=self.r)
        self.a.save()

    def test_get(self):
        # Article objects have access to their related Reporter objects.
        r = self.a.reporter
        self.assertEqual(r.id, self.r.id)
        # These are strings instead of unicode strings because that's what was used in
        # the creation of this reporter (and we haven't refreshed the data from the
        # database, which always returns unicode strings).
        self.assertEqual((r.first_name, self.r.last_name), ('John', 'Smith'))

    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 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 test_assign(self):
        new_article = self.r.article_set.create(headline="John's second story",
                                                pub_date=datetime(2005, 7, 29))
        new_article2 = self.r2.article_set.create(headline="Paul's story",
                                                  pub_date=datetime(2006, 1, 17))
        # Assign the article to the reporter directly using the descriptor.
        new_article2.reporter = self.r
        new_article2.save()
        self.assertEqual(repr(new_article2.reporter), "<Reporter: John Smith>")
        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>",
        ])
        self.assertQuerysetEqual(self.r2.article_set.all(), [])
        # Set the article back again using set descriptor.
        self.r2.article_set = [new_article, new_article2]
        self.assertQuerysetEqual(self.r.article_set.all(), ["<Article: This is a test>"])
        self.assertQuerysetEqual(self.r2.article_set.all(),
            [
                "<Article: John's second story>",
                "<Article: Paul's story>",
            ])

        # Funny case - assignment notation can only go so far; because the
        # ForeignKey cannot be null, existing members of the set must remain.
        self.r.article_set = [new_article]
        self.assertQuerysetEqual(self.r.article_set.all(),
            [
                "<Article: John's second story>",
                "<Article: This is a test>",
            ])
        self.assertQuerysetEqual(self.r2.article_set.all(), ["<Article: Paul's story>"])
        # Reporter cannot be null - there should not be a clear or remove method
        self.assertFalse(hasattr(self.r2.article_set, 'remove'))
        self.assertFalse(hasattr(self.r2.article_set, 'clear'))

    def test_selects(self):
        new_article = self.r.article_set.create(headline="John's second story",
                                                pub_date=datetime(2005, 7, 29))
        new_article2 = self.r2.article_set.create(headline="Paul's story",
                                                  pub_date=datetime(2006, 1, 17))
        # Reporter objects have access to their related Article objects.
        self.assertQuerysetEqual(self.r.article_set.all(), [
            "<Article: John's second story>",
            "<Article: This is a test>",
        ])
        self.assertQuerysetEqual(self.r.article_set.filter(headline__startswith='This'),
                                 ["<Article: This is a test>"])
        self.assertEqual(self.r.article_set.count(), 2)
        self.assertEqual(self.r2.article_set.count(), 1)
        # Get articles by id
        self.assertQuerysetEqual(Article.objects.filter(id__exact=self.a.id),
                                 ["<Article: This is a test>"])
        self.assertQuerysetEqual(Article.objects.filter(pk=self.a.id),
                                 ["<Article: This is a test>"])
        # Query on an article property
        self.assertQuerysetEqual(Article.objects.filter(headline__startswith='This'),
                                 ["<Article: This is a test>"])
        # The API automatically follows relationships as far as you need.
        # Use double underscores to separate relationships.
        # This works as many levels deep as you want. There's no limit.
        # Find all Articles for any Reporter whose first name is "John".
        self.assertQuerysetEqual(Article.objects.filter(reporter__first_name__exact='John'),
            [
                "<Article: John's second story>",
                "<Article: This is a test>",
            ])
        # Check that implied __exact also works
        self.assertQuerysetEqual(Article.objects.filter(reporter__first_name='John'),
            [
                "<Article: John's second story>",
                "<Article: This is a test>",
            ])
        # Query twice over the related field.
        self.assertQuerysetEqual(
            Article.objects.filter(reporter__first_name__exact='John',
                                   reporter__last_name__exact='Smith'),
            [
                "<Article: John's second story>",
                "<Article: This is a test>",
            ])
        # The underlying query only makes one join when a related table is referenced twice.
        queryset = Article.objects.filter(reporter__first_name__exact='John',
                                       reporter__last_name__exact='Smith')
        self.assertNumQueries(1, list, queryset)
        self.assertEqual(queryset.query.get_compiler(queryset.db).as_sql()[0].count('INNER JOIN'), 1)

        # The automatically joined table has a predictable name.
        self.assertQuerysetEqual(
            Article.objects.filter(reporter__first_name__exact='John').extra(
                where=["many_to_one_reporter.last_name='Smith'"]),
            [
                "<Article: John's second story>",
                "<Article: This is a test>",
            ])
        # ... and should work fine with the unicode that comes out of forms.Form.cleaned_data
        self.assertQuerysetEqual(
            Article.objects.filter(reporter__first_name__exact='John'
                                  ).extra(where=["many_to_one_reporter.last_name='%s'" % u'Smith']),
            [
                "<Article: John's second story>",
                "<Article: This is a test>",
            ])
        # Find all Articles for a Reporter.
        # Use direct ID check, pk check, and object comparison
        self.assertQuerysetEqual(
            Article.objects.filter(reporter__id__exact=self.r.id),
            [
                "<Article: John's second story>",
                "<Article: This is a test>",
            ])
        self.assertQuerysetEqual(
            Article.objects.filter(reporter__pk=self.r.id),
            [
                "<Article: John's second story>",
                "<Article: This is a test>",
            ])
        self.assertQuerysetEqual(
            Article.objects.filter(reporter=self.r.id),
            [
                "<Article: John's second story>",
                "<Article: This is a test>",
            ])
        self.assertQuerysetEqual(
            Article.objects.filter(reporter=self.r),
            [
                "<Article: John's second story>",
                "<Article: This is a test>",
            ])
        self.assertQuerysetEqual(
            Article.objects.filter(reporter__in=[self.r.id,self.r2.id]).distinct(),
            [
                    "<Article: John's second story>",
                    "<Article: Paul's story>",
                    "<Article: This is a test>",
            ])
        self.assertQuerysetEqual(
            Article.objects.filter(reporter__in=[self.r,self.r2]).distinct(),
            [
                "<Article: John's second story>",
                "<Article: Paul's story>",
                "<Article: This is a test>",
            ])
        # You can also use a queryset instead of a literal list of instances.
        # The queryset must be reduced to a list of values using values(),
        # then converted into a query
        self.assertQuerysetEqual(
            Article.objects.filter(
                        reporter__in=Reporter.objects.filter(first_name='John').values('pk').query
                    ).distinct(),
            [
                "<Article: John's second story>",
                "<Article: This is a test>",
            ])

    def test_reverse_selects(self):
        a3 = Article.objects.create(id=None, headline="Third article",
                                    pub_date=datetime(2005, 7, 27), reporter_id=self.r.id)
        a4 = Article.objects.create(id=None, headline="Fourth article",
                                    pub_date=datetime(2005, 7, 27), reporter_id=str(self.r.id))
        # Reporters can be queried
        self.assertQuerysetEqual(Reporter.objects.filter(id__exact=self.r.id),
                                 ["<Reporter: John Smith>"])
        self.assertQuerysetEqual(Reporter.objects.filter(pk=self.r.id),
                                 ["<Reporter: John Smith>"])
        self.assertQuerysetEqual(Reporter.objects.filter(first_name__startswith='John'),
                                 ["<Reporter: John Smith>"])
        # Reporters can query in opposite direction of ForeignKey definition
        self.assertQuerysetEqual(Reporter.objects.filter(article__id__exact=self.a.id),
                                 ["<Reporter: John Smith>"])
        self.assertQuerysetEqual(Reporter.objects.filter(article__pk=self.a.id),
                                 ["<Reporter: John Smith>"])
        self.assertQuerysetEqual(Reporter.objects.filter(article=self.a.id),
                                 ["<Reporter: John Smith>"])
        self.assertQuerysetEqual(Reporter.objects.filter(article=self.a),
                                 ["<Reporter: John Smith>"])
        self.assertQuerysetEqual(
            Reporter.objects.filter(article__in=[self.a.id,a3.id]).distinct(),
            ["<Reporter: John Smith>"])
        self.assertQuerysetEqual(
            Reporter.objects.filter(article__in=[self.a.id,a3]).distinct(),
            ["<Reporter: John Smith>"])
        self.assertQuerysetEqual(
            Reporter.objects.filter(article__in=[self.a,a3]).distinct(),
            ["<Reporter: John Smith>"])
        self.assertQuerysetEqual(
            Reporter.objects.filter(article__headline__startswith='T'),
            ["<Reporter: John Smith>", "<Reporter: John Smith>"])
        self.assertQuerysetEqual(
            Reporter.objects.filter(article__headline__startswith='T').distinct(),
            ["<Reporter: John Smith>"])

        # Counting in the opposite direction works in conjunction with distinct()
        self.assertEqual(
            Reporter.objects.filter(article__headline__startswith='T').count(), 2)
        self.assertEqual(
            Reporter.objects.filter(article__headline__startswith='T').distinct().count(), 1)

        # Queries can go round in circles.
        self.assertQuerysetEqual(
            Reporter.objects.filter(article__reporter__first_name__startswith='John'),
            [
                "<Reporter: John Smith>",
                "<Reporter: John Smith>",
                "<Reporter: John Smith>",
            ])
        self.assertQuerysetEqual(
            Reporter.objects.filter(article__reporter__first_name__startswith='John').distinct(),
            ["<Reporter: John Smith>"])
        self.assertQuerysetEqual(
            Reporter.objects.filter(article__reporter__exact=self.r).distinct(),
            ["<Reporter: John Smith>"])

        # Check that implied __exact also works.
        self.assertQuerysetEqual(
            Reporter.objects.filter(article__reporter=self.r).distinct(),
            ["<Reporter: John Smith>"])

        # It's possible to use values() calls across many-to-one relations.
        # (Note, too, that we clear the ordering here so as not to drag the
        # 'headline' field into the columns being used to determine uniqueness)
        d = {'reporter__first_name': u'John', 'reporter__last_name': u'Smith'}
        self.assertEqual([d],
            list(Article.objects.filter(reporter=self.r).distinct().order_by()
                 .values('reporter__first_name', 'reporter__last_name')))

    def test_select_related(self):
        # Check that Article.objects.select_related().dates() works properly when
        # there are multiple Articles with the same date but different foreign-key
        # objects (Reporters).
        r1 = Reporter.objects.create(first_name='Mike', last_name='Royko', email='*****@*****.**')
        r2 = Reporter.objects.create(first_name='John', last_name='Kass', email='*****@*****.**')
        a1 = Article.objects.create(headline='First', pub_date=datetime(1980, 4, 23), reporter=r1)
        a2 = Article.objects.create(headline='Second', pub_date=datetime(1980, 4, 23), reporter=r2)
        self.assertEqual(list(Article.objects.select_related().dates('pub_date', 'day')),
            [
                datetime(1980, 4, 23, 0, 0),
                datetime(2005, 7, 27, 0, 0),
            ])
        self.assertEqual(list(Article.objects.select_related().dates('pub_date', 'month')),
            [
                datetime(1980, 4, 1, 0, 0),
                datetime(2005, 7, 1, 0, 0),
            ])
        self.assertEqual(list(Article.objects.select_related().dates('pub_date', 'year')),
            [
                datetime(1980, 1, 1, 0, 0),
                datetime(2005, 1, 1, 0, 0),
            ])

    def test_delete(self):
        new_article = self.r.article_set.create(headline="John's second story",
                                                pub_date=datetime(2005, 7, 29))
        new_article2 = self.r2.article_set.create(headline="Paul's story",
                                                  pub_date=datetime(2006, 1, 17))
        a3 = Article.objects.create(id=None, headline="Third article",
                                    pub_date=datetime(2005, 7, 27), reporter_id=self.r.id)
        a4 = Article.objects.create(id=None, headline="Fourth article",
                                    pub_date=datetime(2005, 7, 27), reporter_id=str(self.r.id))
        # If you delete a reporter, his articles will be deleted.
        self.assertQuerysetEqual(Article.objects.all(),
            [
                "<Article: Fourth article>",
                "<Article: John's second story>",
                "<Article: Paul's story>",
                "<Article: Third article>",
                "<Article: This is a test>",
            ])
        self.assertQuerysetEqual(Reporter.objects.order_by('first_name'),
            [
                "<Reporter: John Smith>",
                "<Reporter: Paul Jones>",
            ])
        self.r2.delete()
        self.assertQuerysetEqual(Article.objects.all(),
            [
                "<Article: Fourth article>",
                "<Article: John's second story>",
                "<Article: Third article>",
                "<Article: This is a test>",
            ])
        self.assertQuerysetEqual(Reporter.objects.order_by('first_name'),
                                 ["<Reporter: John Smith>"])
        # You can delete using a JOIN in the query.
        Reporter.objects.filter(article__headline__startswith='This').delete()
        self.assertQuerysetEqual(Reporter.objects.all(), [])
        self.assertQuerysetEqual(Article.objects.all(), [])

    def test_regression_12876(self):
        # Regression for #12876 -- Model methods that include queries that
        # recursive don't cause recursion depth problems under deepcopy.
        self.r.cached_query = Article.objects.filter(reporter=self.r)
        self.assertEqual(repr(deepcopy(self.r)), "<Reporter: John Smith>")

    def test_explicit_fk(self):
        # Create a new Article with get_or_create using an explicit value
        # for a ForeignKey.
        a2, created = Article.objects.get_or_create(id=None,
                                                    headline="John's second test",
                                                    pub_date=datetime(2011, 5, 7),
                                                    reporter_id=self.r.id)
        self.assertTrue(created)
        self.assertEqual(a2.reporter.id, self.r.id)

        # You can specify filters containing the explicit FK value.
        self.assertQuerysetEqual(
            Article.objects.filter(reporter_id__exact=self.r.id),
            [
                "<Article: John's second test>",
                "<Article: This is a test>",
            ])

        # Create an Article by Paul for the same date.
        a3 = Article.objects.create(id=None, headline="Paul's commentary",
                                    pub_date=datetime(2011, 5, 7),
                                    reporter_id=self.r2.id)
        self.assertEqual(a3.reporter.id, self.r2.id)

        # Get should respect explicit foreign keys as well.
        self.assertRaises(MultipleObjectsReturned,
                          Article.objects.get, reporter_id=self.r.id)
        self.assertEqual(repr(a3),
                         repr(Article.objects.get(reporter_id=self.r2.id,
                                             pub_date=datetime(2011, 5, 7))))
Exemple #32
0
 def get(self):
     tech_post = Article.query().filter(Article.category == 'Tech').fetch()
     tech_template = jinja_env.get_template('templates/tech.html')
     self.response.out.write(tech_template.render())
     self.response.write(tech_post)
Exemple #33
0
 def test_escaping(self):
     # Underscores, percent signs and backslashes have special
     # meaning in the underlying SQL code, but Django handles
     # the quoting of them automatically.
     a8 = Article(headline='Article_ with underscore',
                  pub_date=datetime(2005, 11, 20))
     a8.save()
     self.assertQuerysetEqual(
         Article.objects.filter(headline__startswith='Article'), [
             '<Article: Article_ with underscore>',
             '<Article: Article 5>',
             '<Article: Article 6>',
             '<Article: Article 4>',
             '<Article: Article 2>',
             '<Article: Article 3>',
             '<Article: Article 7>',
             '<Article: Article 1>',
         ])
     self.assertQuerysetEqual(
         Article.objects.filter(headline__startswith='Article_'),
         ['<Article: Article_ with underscore>'])
     a9 = Article(headline='Article% with percent sign',
                  pub_date=datetime(2005, 11, 21))
     a9.save()
     self.assertQuerysetEqual(
         Article.objects.filter(headline__startswith='Article'), [
             '<Article: Article% with percent sign>',
             '<Article: Article_ with underscore>',
             '<Article: Article 5>',
             '<Article: Article 6>',
             '<Article: Article 4>',
             '<Article: Article 2>',
             '<Article: Article 3>',
             '<Article: Article 7>',
             '<Article: Article 1>',
         ])
     self.assertQuerysetEqual(
         Article.objects.filter(headline__startswith='Article%'),
         ['<Article: Article% with percent sign>'])
     a10 = Article(headline='Article with \\ backslash',
                   pub_date=datetime(2005, 11, 22))
     a10.save()
     self.assertQuerysetEqual(
         Article.objects.filter(headline__contains='\\'),
         ['<Article: Article with \ backslash>'])
Exemple #34
0
 def get(self):
     education_post = Article.query().filter(Article.category == 'Education').fetch()
     education_template = jinja_env.get_template('templates/education.html')
     self.response.out.write(education_template.render())
     self.response.write(education_post)
Exemple #35
0
 def get(self):
     caleb_post = Article.query().filter(Article.category == 'Caleb').fetch()
     caleb_template = jinja_env.get_template('templates/celeb.html')
     self.response.out.write(caleb_template.render())
     self.response.write(caleb_post)
Exemple #36
0
def hello_world():
    articel1 = Article(title='zzzz')
    db.session.add(articel1)
    db.session.commit()
    return 'Hello World!'
Exemple #37
0
def create():
    csrf_token = request.form.get('csrf_token')
    upload_folder = os.path.join(app.config['UPLOAD_FOLDER'], mod.name,
                                 csrf_token, 'files')

    form = RegistrationForm()

    if not os.path.exists(upload_folder):
        flash(u'Selecione o arquivo do artigo para enviá-lo.', 'danger')
        return render_template('scholar/new.html', form=form)

    if form.validate() is False:
        form.set_choices(approved_articles_keywords())
        return render_template('scholar/new.html', form=form)
    else:
        article = Article()
        article.title = form.title.data
        article.theme = form.theme.data
        article.abstract = form.abstract.data
        article.postage_date = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
        article.approval_status = 0

        author_input_list = form.authors.data.replace(', ', ',').split(',')
        for author_input in author_input_list:
            article.authors.append(AuthorScholar(author_input))

        for keyword_input in form.keywords.data:
            keyword = KeyWord.query.filter_by(name=keyword_input).first()
            if not keyword:
                article.keywords.append(KeyWord(keyword_input))
            else:
                article.keywords.append(keyword)

        db.session.add(article)
        db.session.flush()

        if os.path.exists(upload_folder):

            file_name = [file for file in os.listdir(upload_folder)][0]

            article.file_url = upload_helper.upload_s3_file(
                os.path.join(upload_folder, file_name),
                os.path.join('scholar/', str(article.id), 'files/', 'article'),
                {
                    'ContentType':
                    "application/pdf",
                    'ContentDisposition':
                    'attachment; filename=dataviva-article-' +
                    str(article.id) + '.pdf'
                })

            shutil.rmtree(os.path.split(upload_folder)[0])

        db.session.commit()
        upload_helper.log_operation(module=mod.name,
                                    operation='create',
                                    user=(g.user.id, g.user.email),
                                    objs=[(article.id, article.title)])
        new_article_advise(article, request.url_root)
        message = dictionary()["article_submission"]
        flash(message, 'success')
        return redirect(url_for('scholar.index'))
Exemple #38
0
    def copyArticlesKind(self, article, author):
        """Create new Article and Comment objects from old Articles object, returning True if success."""

        article_id = Article.allocate_ids(size=1, parent=author.key)[0]
        article_key = ndb.Key(Article, article_id, parent=author.key)
        a = article_key.get()
        if a:
            return

        # copy ArticleForm/ProtoRPC Message into dict
        data = db.to_dict(article)
        data['key'] = article_key

        if 'comments' in data:
            for comment in data['comments']:
                #Create new Comment object
                comment_author_email = str(loads(str(comment))[1])
                a_key = ndb.Key(Author, comment_author_email or 'unknown')
                comment_author = a_key.get()
                # create new Author if not there
                if not comment_author:
                    comment_author = Author(
                        key=a_key,
                        authorID=str(Author.allocate_ids(size=1)[0]),
                        displayName=comment_author_email.split('@')[0],
                        mainEmail=comment_author_email,
                    )
                    comment_author.put()

                comment_data = {
                    'comment':
                    loads(str(comment))[0],
                    'authorName':
                    comment_author.displayName
                    if comment_author else 'unknown',
                    'authorID':
                    comment_author.authorID if comment_author else 'unknown',
                    'dateCreated':
                    loads(str(comment))[2]
                }

                comment_id = Comment.allocate_ids(size=1,
                                                  parent=article_key)[0]
                comment_key = ndb.Key(Comment, comment_id, parent=article_key)
                comment_data['key'] = comment_key

                # create Comment
                Comment(**comment_data).put()

            del data['comments']

        if 'tags' in data:
            #del data['tags']
            try:
                data['tags'] = str(data['tags']).split(', ')
            except UnicodeEncodeError:
                del data['tags']
        if 'tags' in data and data['tags'] == [""]:
            del data['tags']

        if 'id' in data:
            del data['id']

        if data['view'] == None:
            del data['view']
        else:
            data['view'] = {
                'Publish': 'PUBLISHED',
                'Preview': 'NOT_PUBLISHED',
                'Retract': 'RETRACTED'
            }[str(data['view'])]

        data['legacyID'] = str(article.key().id())

        data['authorName'] = author.displayName
        del data['author']
        data['dateCreated'] = data['date']
        del data['date']

        # create Article
        Article(**data).put()
Exemple #39
0
class SerializersTestBase(object):
    @staticmethod
    def _comparison_value(value):
        return value

    def setUp(self):
        sports = Category.objects.create(name="Sports")
        music = Category.objects.create(name="Music")
        op_ed = Category.objects.create(name="Op-Ed")

        self.joe = Author.objects.create(name="Joe")
        self.jane = Author.objects.create(name="Jane")

        self.a1 = Article(
            author=self.jane,
            headline="Poker has no place on ESPN",
            pub_date=datetime(2006, 6, 16, 11, 00)
        )
        self.a1.save()
        self.a1.categories = [sports, op_ed]

        self.a2 = Article(
            author=self.joe,
            headline="Time to reform copyright",
            pub_date=datetime(2006, 6, 16, 13, 00, 11, 345)
        )
        self.a2.save()
        self.a2.categories = [music, op_ed]

    def test_serialize(self):
        """Tests that basic serialization works."""
        serial_str = serializers.serialize(self.serializer_name,
                                           Article.objects.all())
        self.assertTrue(self._validate_output(serial_str))

    def test_serializer_roundtrip(self):
        """Tests that serialized content can be deserialized."""
        serial_str = serializers.serialize(self.serializer_name,
                                           Article.objects.all())
        models = list(serializers.deserialize(self.serializer_name, serial_str))
        self.assertEqual(len(models), 2)

    def test_altering_serialized_output(self):
        """
        Tests the ability to create new objects by
        modifying serialized content.
        """
        old_headline = "Poker has no place on ESPN"
        new_headline = "Poker has no place on television"
        serial_str = serializers.serialize(self.serializer_name,
                                           Article.objects.all())
        serial_str = serial_str.replace(old_headline, new_headline)
        models = list(serializers.deserialize(self.serializer_name, serial_str))

        # Prior to saving, old headline is in place
        self.assertTrue(Article.objects.filter(headline=old_headline))
        self.assertFalse(Article.objects.filter(headline=new_headline))

        for model in models:
            model.save()

        # After saving, new headline is in place
        self.assertTrue(Article.objects.filter(headline=new_headline))
        self.assertFalse(Article.objects.filter(headline=old_headline))

    def test_one_to_one_as_pk(self):
        """
        Tests that if you use your own primary key field
        (such as a OneToOneField), it doesn't appear in the
        serialized field list - it replaces the pk identifier.
        """
        profile = AuthorProfile(author=self.joe,
                                date_of_birth=datetime(1970,1,1))
        profile.save()
        serial_str = serializers.serialize(self.serializer_name,
                                           AuthorProfile.objects.all())
        self.assertFalse(self._get_field_values(serial_str, 'author'))

        for obj in serializers.deserialize(self.serializer_name, serial_str):
            self.assertEqual(obj.object.pk, self._comparison_value(self.joe.pk))

    def test_serialize_field_subset(self):
        """Tests that output can be restricted to a subset of fields"""
        valid_fields = ('headline','pub_date')
        invalid_fields = ("author", "categories")
        serial_str = serializers.serialize(self.serializer_name,
                                    Article.objects.all(),
                                    fields=valid_fields)
        for field_name in invalid_fields:
            self.assertFalse(self._get_field_values(serial_str, field_name))

        for field_name in valid_fields:
            self.assertTrue(self._get_field_values(serial_str, field_name))

    def test_serialize_unicode(self):
        """Tests that unicode makes the roundtrip intact"""
        actor_name = u"Za\u017c\u00f3\u0142\u0107"
        movie_title = u'G\u0119\u015bl\u0105 ja\u017a\u0144'
        ac = Actor(name=actor_name)
        mv = Movie(title=movie_title, actor=ac)
        ac.save()
        mv.save()

        serial_str = serializers.serialize(self.serializer_name, [mv])
        self.assertEqual(self._get_field_values(serial_str, "title")[0], movie_title)
        self.assertEqual(self._get_field_values(serial_str, "actor")[0], actor_name)

        obj_list = list(serializers.deserialize(self.serializer_name, serial_str))
        mv_obj = obj_list[0].object
        self.assertEqual(mv_obj.title, movie_title)

    def test_serialize_with_null_pk(self):
        """
        Tests that serialized data with no primary key results
        in a model instance with no id
        """
        category = Category(name="Reference")
        serial_str = serializers.serialize(self.serializer_name, [category])
        pk_value = self._get_pk_values(serial_str)[0]
        self.assertFalse(pk_value)

        cat_obj = list(serializers.deserialize(self.serializer_name,
                                               serial_str))[0].object
        self.assertEqual(cat_obj.id, None)

    def test_float_serialization(self):
        """Tests that float values serialize and deserialize intact"""
        sc = Score(score=3.4)
        sc.save()
        serial_str = serializers.serialize(self.serializer_name, [sc])
        deserial_objs = list(serializers.deserialize(self.serializer_name,
                                                serial_str))
        self.assertEqual(deserial_objs[0].object.score, Approximate(3.4, places=1))

    def test_custom_field_serialization(self):
        """Tests that custom fields serialize and deserialize intact"""
        team_str = "Spartak Moskva"
        player = Player()
        player.name = "Soslan Djanaev"
        player.rank = 1
        player.team = Team(team_str)
        player.save()
        serial_str = serializers.serialize(self.serializer_name,
                                           Player.objects.all())
        team = self._get_field_values(serial_str, "team")
        self.assertTrue(team)
        self.assertEqual(team[0], team_str)

        deserial_objs = list(serializers.deserialize(self.serializer_name, serial_str))
        self.assertEqual(deserial_objs[0].object.team.to_string(),
                         player.team.to_string())

    def test_pre_1000ad_date(self):
        """Tests that year values before 1000AD are properly formatted"""
        # Regression for #12524 -- dates before 1000AD get prefixed
        # 0's on the year
        a = Article.objects.create(
        author = self.jane,
        headline = "Nobody remembers the early years",
        pub_date = datetime(1, 2, 3, 4, 5, 6))

        serial_str = serializers.serialize(self.serializer_name, [a])
        date_values = self._get_field_values(serial_str, "pub_date")
        self.assertEqual(date_values[0], "0001-02-03 04:05:06")

    def test_pkless_serialized_strings(self):
        """
        Tests that serialized strings without PKs
        can be turned into models
        """
        deserial_objs = list(serializers.deserialize(self.serializer_name,
                                                     self.pkless_str))
        for obj in deserial_objs:
            self.assertFalse(obj.object.id)
            obj.save()
        self.assertEqual(Category.objects.all().count(), 4)
Exemple #40
0
def cleanup():
    Article.drop_collection()
    Publisher.drop_collection()
Exemple #41
0
def getter():
    session = SessionMaker()
    result = session.query(Article).all()
    return jsonify({"articles": [Article.get_fields(r) for r in result]})
Exemple #42
0
 def process_item(self, item, spider):
     model = Article(id=uuid.uuid4(), student=self.student_id, **item)
     model.save(force_insert=True)
     return item
Exemple #43
0
class LookupTests(TestCase):
    def setUp(self):
        # Create a few Authors.
        self.au1 = Author(name='Author 1')
        self.au1.save()
        self.au2 = Author(name='Author 2')
        self.au2.save()
        # Create a couple of Articles.
        self.a1 = Article(headline='Article 1',
                          pub_date=datetime(2005, 7, 26),
                          author=self.au1)
        self.a1.save()
        self.a2 = Article(headline='Article 2',
                          pub_date=datetime(2005, 7, 27),
                          author=self.au1)
        self.a2.save()
        self.a3 = Article(headline='Article 3',
                          pub_date=datetime(2005, 7, 27),
                          author=self.au1)
        self.a3.save()
        self.a4 = Article(headline='Article 4',
                          pub_date=datetime(2005, 7, 28),
                          author=self.au1)
        self.a4.save()
        self.a5 = Article(headline='Article 5',
                          pub_date=datetime(2005, 8, 1, 9, 0),
                          author=self.au2)
        self.a5.save()
        self.a6 = Article(headline='Article 6',
                          pub_date=datetime(2005, 8, 1, 8, 0),
                          author=self.au2)
        self.a6.save()
        self.a7 = Article(headline='Article 7',
                          pub_date=datetime(2005, 7, 27),
                          author=self.au2)
        self.a7.save()
        # Create a few Tags.
        self.t1 = Tag(name='Tag 1')
        self.t1.save()
        self.t1.articles.add(self.a1, self.a2, self.a3)
        self.t2 = Tag(name='Tag 2')
        self.t2.save()
        self.t2.articles.add(self.a3, self.a4, self.a5)
        self.t3 = Tag(name='Tag 3')
        self.t3.save()
        self.t3.articles.add(self.a5, self.a6, self.a7)

    def test_exists(self):
        # We can use .exists() to check that there are some.
        self.assertTrue(Article.objects.exists())
        for a in Article.objects.all():
            a.delete()
        # There should be none now!
        self.assertFalse(Article.objects.exists())

    @skipUnlessDBFeature('supports_date_lookup_using_string')
    def test_lookup_date_as_str(self):
        # A date lookup can be performed using a string search.
        self.assertQuerysetEqual(
            Article.objects.filter(pub_date__startswith='2005'), [
                '<Article: Article 5>',
                '<Article: Article 6>',
                '<Article: Article 4>',
                '<Article: Article 2>',
                '<Article: Article 3>',
                '<Article: Article 7>',
                '<Article: Article 1>',
            ])

    def test_iterator(self):
        # Each QuerySet gets iterator(), which is a generator that
        # "lazily" returns results using database-level iteration.
        self.assertQuerysetEqual(Article.objects.iterator(), [
            'Article 5',
            'Article 6',
            'Article 4',
            'Article 2',
            'Article 3',
            'Article 7',
            'Article 1',
        ],
                                 transform=attrgetter('headline'))
        # iterator() can be used on any QuerySet.
        self.assertQuerysetEqual(
            Article.objects.filter(headline__endswith='4').iterator(),
            ['Article 4'],
            transform=attrgetter('headline'))

    def test_count(self):
        # count() returns the number of objects matching search criteria.
        self.assertEqual(Article.objects.count(), 7)
        self.assertEqual(
            Article.objects.filter(
                pub_date__exact=datetime(2005, 7, 27)).count(), 3)
        self.assertEqual(
            Article.objects.filter(headline__startswith='Blah blah').count(),
            0)

        # count() should respect sliced query sets.
        articles = Article.objects.all()
        self.assertEqual(articles.count(), 7)
        self.assertEqual(articles[:4].count(), 4)
        self.assertEqual(articles[1:100].count(), 6)
        self.assertEqual(articles[10:100].count(), 0)

        # Date and date/time lookups can also be done with strings.
        self.assertEqual(
            Article.objects.filter(
                pub_date__exact='2005-07-27 00:00:00').count(), 3)

    def test_in_bulk(self):
        # in_bulk() takes a list of IDs and returns a dictionary
        # mapping IDs to objects.
        arts = Article.objects.in_bulk([self.a1.id, self.a2.id])
        self.assertEqual(arts[self.a1.id], self.a1)
        self.assertEqual(arts[self.a2.id], self.a2)
        self.assertEqual(Article.objects.in_bulk([self.a3.id]),
                         {self.a3.id: self.a3})
        self.assertEqual(Article.objects.in_bulk(set([self.a3.id])),
                         {self.a3.id: self.a3})
        self.assertEqual(Article.objects.in_bulk(frozenset([self.a3.id])),
                         {self.a3.id: self.a3})
        self.assertEqual(Article.objects.in_bulk((self.a3.id, )),
                         {self.a3.id: self.a3})
        self.assertEqual(Article.objects.in_bulk([ObjectId()]), {})
        self.assertEqual(Article.objects.in_bulk([]), {})
        self.assertRaises(DatabaseError, Article.objects.in_bulk, 'foo')
        self.assertRaises(TypeError, Article.objects.in_bulk)
        self.assertRaises(TypeError,
                          Article.objects.in_bulk,
                          headline__startswith='Blah')

    def test_values(self):
        # values() returns a list of dictionaries instead of object
        # instances -- and you can specify which fields you want to
        # retrieve.
        identity = lambda x: x
        self.assertQuerysetEqual(Article.objects.values('headline'), [
            {
                'headline': u'Article 5'
            },
            {
                'headline': u'Article 6'
            },
            {
                'headline': u'Article 4'
            },
            {
                'headline': u'Article 2'
            },
            {
                'headline': u'Article 3'
            },
            {
                'headline': u'Article 7'
            },
            {
                'headline': u'Article 1'
            },
        ],
                                 transform=identity)
        self.assertQuerysetEqual(Article.objects.filter(
            pub_date__exact=datetime(2005, 7, 27)).values('id'),
                                 [{
                                     'id': self.a2.id
                                 }, {
                                     'id': self.a3.id
                                 }, {
                                     'id': self.a7.id
                                 }],
                                 transform=identity)
        self.assertQuerysetEqual(Article.objects.values('id', 'headline'), [
            {
                'id': self.a5.id,
                'headline': 'Article 5'
            },
            {
                'id': self.a6.id,
                'headline': 'Article 6'
            },
            {
                'id': self.a4.id,
                'headline': 'Article 4'
            },
            {
                'id': self.a2.id,
                'headline': 'Article 2'
            },
            {
                'id': self.a3.id,
                'headline': 'Article 3'
            },
            {
                'id': self.a7.id,
                'headline': 'Article 7'
            },
            {
                'id': self.a1.id,
                'headline': 'Article 1'
            },
        ],
                                 transform=identity)
        # You can use values() with iterator() for memory savings,
        # because iterator() uses database-level iteration.
        self.assertQuerysetEqual(Article.objects.values('id',
                                                        'headline').iterator(),
                                 [
                                     {
                                         'headline': u'Article 5',
                                         'id': self.a5.id
                                     },
                                     {
                                         'headline': u'Article 6',
                                         'id': self.a6.id
                                     },
                                     {
                                         'headline': u'Article 4',
                                         'id': self.a4.id
                                     },
                                     {
                                         'headline': u'Article 2',
                                         'id': self.a2.id
                                     },
                                     {
                                         'headline': u'Article 3',
                                         'id': self.a3.id
                                     },
                                     {
                                         'headline': u'Article 7',
                                         'id': self.a7.id
                                     },
                                     {
                                         'headline': u'Article 1',
                                         'id': self.a1.id
                                     },
                                 ],
                                 transform=identity)

    def test_values_list(self):
        # values_list() is similar to values(), except that the results
        # are returned as a list of tuples, rather than a list of
        # dictionaries. Within each tuple, the order of the elements is
        # the same as the order of fields in the values_list() call.
        identity = lambda x: x
        self.assertQuerysetEqual(Article.objects.values_list('headline'), [
            (u'Article 5', ),
            (u'Article 6', ),
            (u'Article 4', ),
            (u'Article 2', ),
            (u'Article 3', ),
            (u'Article 7', ),
            (u'Article 1', ),
        ],
                                 transform=identity)
        self.assertQuerysetEqual(
            Article.objects.values_list('id').order_by('id'), [(self.a1.id, ),
                                                               (self.a2.id, ),
                                                               (self.a3.id, ),
                                                               (self.a4.id, ),
                                                               (self.a5.id, ),
                                                               (self.a6.id, ),
                                                               (self.a7.id, )],
            transform=identity)
        self.assertQuerysetEqual(Article.objects.values_list(
            'id', flat=True).order_by('id'), [
                self.a1.id, self.a2.id, self.a3.id, self.a4.id, self.a5.id,
                self.a6.id, self.a7.id
            ],
                                 transform=identity)
        self.assertRaises(TypeError,
                          Article.objects.values_list,
                          'id',
                          'headline',
                          flat=True)

    def test_get_next_previous_by(self):
        # Every DateField and DateTimeField creates get_next_by_FOO()
        # and get_previous_by_FOO() methods. In the case of identical
        # date values, these methods will use the ID as a fallback
        # check. This guarantees that no records are skipped or
        # duplicated.
        self.assertEqual(repr(self.a1.get_next_by_pub_date()),
                         '<Article: Article 2>')
        self.assertEqual(repr(self.a2.get_next_by_pub_date()),
                         '<Article: Article 3>')
        self.assertEqual(
            repr(self.a2.get_next_by_pub_date(headline__endswith='6')),
            '<Article: Article 6>')
        self.assertEqual(repr(self.a3.get_next_by_pub_date()),
                         '<Article: Article 7>')
        self.assertEqual(repr(self.a4.get_next_by_pub_date()),
                         '<Article: Article 6>')
        self.assertRaises(Article.DoesNotExist, self.a5.get_next_by_pub_date)
        self.assertEqual(repr(self.a6.get_next_by_pub_date()),
                         '<Article: Article 5>')
        self.assertEqual(repr(self.a7.get_next_by_pub_date()),
                         '<Article: Article 4>')

        self.assertEqual(repr(self.a7.get_previous_by_pub_date()),
                         '<Article: Article 3>')
        self.assertEqual(repr(self.a6.get_previous_by_pub_date()),
                         '<Article: Article 4>')
        self.assertEqual(repr(self.a5.get_previous_by_pub_date()),
                         '<Article: Article 6>')
        self.assertEqual(repr(self.a4.get_previous_by_pub_date()),
                         '<Article: Article 7>')
        self.assertEqual(repr(self.a3.get_previous_by_pub_date()),
                         '<Article: Article 2>')
        self.assertEqual(repr(self.a2.get_previous_by_pub_date()),
                         '<Article: Article 1>')

    def test_escaping(self):
        # Underscores, percent signs and backslashes have special
        # meaning in the underlying SQL code, but Django handles
        # the quoting of them automatically.
        a8 = Article(headline='Article_ with underscore',
                     pub_date=datetime(2005, 11, 20))
        a8.save()
        self.assertQuerysetEqual(
            Article.objects.filter(headline__startswith='Article'), [
                '<Article: Article_ with underscore>',
                '<Article: Article 5>',
                '<Article: Article 6>',
                '<Article: Article 4>',
                '<Article: Article 2>',
                '<Article: Article 3>',
                '<Article: Article 7>',
                '<Article: Article 1>',
            ])
        self.assertQuerysetEqual(
            Article.objects.filter(headline__startswith='Article_'),
            ['<Article: Article_ with underscore>'])
        a9 = Article(headline='Article% with percent sign',
                     pub_date=datetime(2005, 11, 21))
        a9.save()
        self.assertQuerysetEqual(
            Article.objects.filter(headline__startswith='Article'), [
                '<Article: Article% with percent sign>',
                '<Article: Article_ with underscore>',
                '<Article: Article 5>',
                '<Article: Article 6>',
                '<Article: Article 4>',
                '<Article: Article 2>',
                '<Article: Article 3>',
                '<Article: Article 7>',
                '<Article: Article 1>',
            ])
        self.assertQuerysetEqual(
            Article.objects.filter(headline__startswith='Article%'),
            ['<Article: Article% with percent sign>'])
        a10 = Article(headline='Article with \\ backslash',
                      pub_date=datetime(2005, 11, 22))
        a10.save()
        self.assertQuerysetEqual(
            Article.objects.filter(headline__contains='\\'),
            ['<Article: Article with \ backslash>'])

    def test_exclude(self):
        a8 = Article.objects.create(headline='Article_ with underscore',
                                    pub_date=datetime(2005, 11, 20))
        a9 = Article.objects.create(headline='Article% with percent sign',
                                    pub_date=datetime(2005, 11, 21))
        a10 = Article.objects.create(headline='Article with \\ backslash',
                                     pub_date=datetime(2005, 11, 22))

        # exclude() is the opposite of filter() when doing lookups:
        self.assertQuerysetEqual(
            Article.objects.filter(headline__contains='Article').exclude(
                headline__contains='with'), [
                    '<Article: Article 5>',
                    '<Article: Article 6>',
                    '<Article: Article 4>',
                    '<Article: Article 2>',
                    '<Article: Article 3>',
                    '<Article: Article 7>',
                    '<Article: Article 1>',
                ])
        self.assertQuerysetEqual(
            Article.objects.exclude(headline__startswith='Article_'), [
                '<Article: Article with \\ backslash>',
                '<Article: Article% with percent sign>',
                '<Article: Article 5>',
                '<Article: Article 6>',
                '<Article: Article 4>',
                '<Article: Article 2>',
                '<Article: Article 3>',
                '<Article: Article 7>',
                '<Article: Article 1>',
            ])
        self.assertQuerysetEqual(Article.objects.exclude(headline='Article 7'),
                                 [
                                     '<Article: Article with \\ backslash>',
                                     '<Article: Article% with percent sign>',
                                     '<Article: Article_ with underscore>',
                                     '<Article: Article 5>',
                                     '<Article: Article 6>',
                                     '<Article: Article 4>',
                                     '<Article: Article 2>',
                                     '<Article: Article 3>',
                                     '<Article: Article 1>',
                                 ])

    def test_none(self):
        # none() returns an EmptyQuerySet that behaves like any other
        # QuerySet object.
        self.assertQuerysetEqual(Article.objects.none(), [])
        self.assertQuerysetEqual(
            Article.objects.none().filter(headline__startswith='Article'), [])
        self.assertQuerysetEqual(
            Article.objects.filter(headline__startswith='Article').none(), [])
        self.assertEqual(Article.objects.none().count(), 0)
        self.assertEqual(
            Article.objects.none().update(
                headline="This should not take effect"), 0)
        self.assertQuerysetEqual(
            [article for article in Article.objects.none().iterator()], [])

    def test_in(self):
        # using __in with an empty list should return an
        # empty query set.
        self.assertQuerysetEqual(Article.objects.filter(id__in=[]), [])
        self.assertQuerysetEqual(Article.objects.exclude(id__in=[]), [
            '<Article: Article 5>',
            '<Article: Article 6>',
            '<Article: Article 4>',
            '<Article: Article 2>',
            '<Article: Article 3>',
            '<Article: Article 7>',
            '<Article: Article 1>',
        ])

    def test_error_messages(self):
        # Programming errors are pointed out with nice error messages.
        try:
            Article.objects.filter(pub_date_year='2005').count()
            self.fail("FieldError not raised.")
        except FieldError, ex:
            self.assertEqual(
                str(ex), "Cannot resolve keyword 'pub_date_year' into field. "
                "Choices are: author, headline, id, pub_date, tag")
        try:
            Article.objects.filter(headline__starts='Article')
            self.fail("FieldError not raised.")
        except FieldError, ex:
            self.assertEqual(
                str(ex), "Join on field 'headline' not permitted. "
                "Did you misspell 'starts' for the lookup type?")
Exemple #44
0
 def setUp(self):
     # Create a few Authors.
     self.au1 = Author(name='Author 1')
     self.au1.save()
     self.au2 = Author(name='Author 2')
     self.au2.save()
     # Create a couple of Articles.
     self.a1 = Article(headline='Article 1',
                       pub_date=datetime(2005, 7, 26),
                       author=self.au1)
     self.a1.save()
     self.a2 = Article(headline='Article 2',
                       pub_date=datetime(2005, 7, 27),
                       author=self.au1)
     self.a2.save()
     self.a3 = Article(headline='Article 3',
                       pub_date=datetime(2005, 7, 27),
                       author=self.au1)
     self.a3.save()
     self.a4 = Article(headline='Article 4',
                       pub_date=datetime(2005, 7, 28),
                       author=self.au1)
     self.a4.save()
     self.a5 = Article(headline='Article 5',
                       pub_date=datetime(2005, 8, 1, 9, 0),
                       author=self.au2)
     self.a5.save()
     self.a6 = Article(headline='Article 6',
                       pub_date=datetime(2005, 8, 1, 8, 0),
                       author=self.au2)
     self.a6.save()
     self.a7 = Article(headline='Article 7',
                       pub_date=datetime(2005, 7, 27),
                       author=self.au2)
     self.a7.save()
     # Create a few Tags.
     self.t1 = Tag(name='Tag 1')
     self.t1.save()
     self.t1.articles.add(self.a1, self.a2, self.a3)
     self.t2 = Tag(name='Tag 2')
     self.t2.save()
     self.t2.articles.add(self.a3, self.a4, self.a5)
     self.t3 = Tag(name='Tag 3')
     self.t3.save()
     self.t3.articles.add(self.a5, self.a6, self.a7)
Exemple #45
0
session.add(category_six)
session.commit()

article_one = Article(parent_id=1,
                      title="Cucumber Automation",
                      article_text="""### Introduction to Cucumber<br> Cucumber
 is a software tool used by <strong>computer programmers and testers</strong>
 for testing software.<br>
 It runs automated acceptance tests written
 in a <strong>behavior-driven development (BDD)</strong>
 style.<br> Central to the Cucumber BDD
 approach is its plain language parser called Gherkin.
 It allows expected software behaviors to be specified
 in a logical language that customers can understand.
 As such, Cucumber allows the execution of feature documentation
 written in business-facing text. <br>Cucumber
 was originally written in the Ruby programming language and
 was originally used exclusively for Ruby testing as a complement to
 the RSpec BDD framework. Cucumber now supports a
 variety of different programming languages through various
 implementations, including Java. For example, Cuke4php
 and Cuke4Lua are software bridges that enable testing
 of PHP and Lua projects, respectively. Other
 implementations may simply leverage the Gherkin parser while
 implementing the rest of the testing framework
 in the target language.<br>#### Usage<br> Cucumber can be used
 by Developers or Testers to either Unit
     Test code or test the UI.<br>""",
                      owner_id='admin')
session.add(article_one)
session.commit()
Exemple #46
0
    def post(self, id='', title=''):
        action = self.get_argument("act")

        if action == 'inputpw':
            wrn = self.get_cookie("wrpw", '0')
            if int(wrn) >= 10:
                self.write('403')
                return

            pw = self.get_argument("pw", '')
            pobj = Article.get_article_by_id_simple(id)
            wr = False
            if pw:
                if pobj.password == pw:
                    self.set_cookie("rp%s" % id,
                                    pobj.password,
                                    path="/",
                                    expires_days=1)
                else:
                    wr = True
            else:
                wr = True
            if wr:
                wrn = self.get_cookie("wrpw", '0')
                self.set_cookie("wrpw",
                                str(int(wrn) + 1),
                                path="/",
                                expires_days=1)

            self.redirect('%s/topic/%d/%s' % (BASE_URL, pobj.id, pobj.title))
            return

        self.set_header('Content-Type', 'application/json')
        rspd = {'status': 201, 'msg': 'ok'}

        if action == 'readmorecomment':
            fromid = self.get_argument("fromid", '')
            allnum = int(self.get_argument("allnum", 0))
            showednum = int(
                self.get_argument("showednum", EACH_PAGE_COMMENT_NUM))
            if fromid:
                rspd['status'] = 200
                if (allnum - showednum) >= EACH_PAGE_COMMENT_NUM:
                    limit = EACH_PAGE_COMMENT_NUM
                else:
                    limit = allnum - showednum
                cobjs = Comment.get_post_page_comments_by_id(id, fromid, limit)
                rspd['commentstr'] = self.render('comments.html',
                                                 {'cobjs': cobjs})
                rspd['lavenum'] = allnum - showednum - limit
                self.write(json.dumps(rspd))
            return

        #
        usercomnum = self.get_cookie('usercomnum', '0')
        if int(usercomnum) > MAX_COMMENT_NUM_A_DAY:
            rspd = {'status': 403, 'msg': '403: Forbidden'}
            self.write(json.dumps(rspd))
            return

        try:
            timestamp = int(time())
            post_dic = {
                'author':
                self.get_argument("author"),
                'email':
                self.get_argument("email"),
                'content':
                safe_encode(self.get_argument("con").replace('\r', '\n')),
                'url':
                self.get_argument("url", ''),
                'postid':
                self.get_argument("postid"),
                'add_time':
                timestamp,
                'toid':
                self.get_argument("toid", ''),
                'visible':
                COMMENT_DEFAULT_VISIBLE
            }
        except:
            rspd['status'] = 500
            rspd['msg'] = '错误: 注意必填的三项'
            self.write(json.dumps(rspd))
            return

        pobj = Article.get_article_by_id_simple(id)
        if pobj and not pobj.closecomment:
            cobjid = Comment.add_new_comment(post_dic)
            if cobjid:
                Article.update_post_comment(pobj.comment_num + 1, id)
                rspd['status'] = 200
                #rspd['msg'] = '恭喜: 已成功提交评论'

                rspd['msg'] = self.render(
                    'comment.html', {
                        'cobjid':
                        cobjid,
                        'gravatar':
                        'http://www.gravatar.com/avatar/%s' %
                        md5(post_dic['email']).hexdigest(),
                        'url':
                        post_dic['url'],
                        'author':
                        post_dic['author'],
                        'visible':
                        post_dic['visible'],
                        'content':
                        post_dic['content'],
                    })

                #                 clear_cache_by_pathlist(['/','post:%s'%id])
                #send mail
                if not debug:
                    try:
                        if NOTICE_MAIL:
                            tolist = [NOTICE_MAIL]
                        else:
                            tolist = []
                        if post_dic['toid']:
                            toid = post_dic['toid']
                            tcomment = Comment.get_comment_by_id(toid)
                            if tcomment and tcomment.email:
                                tolist.append(tcomment.email)
                        commenturl = "%s/t/%s#r%s" % (BASE_URL, str(
                            pobj.id), str(cobjid))
                        m_subject = u'有人回复您在 《%s》 里的评论 %s' % (pobj.title,
                                                              str(cobjid))
                        m_html = u'这是一封提醒邮件(请勿直接回复): %s ,请尽快处理: %s' % (
                            m_subject, commenturl)

                        if tolist:
                            import sae.mail
                            sae.mail.send_mail(
                                ','.join(tolist), m_subject, m_html,
                                (MAIL_SMTP, int(MAIL_PORT), MAIL_FROM,
                                 MAIL_PASSWORD, True))

                    except:
                        pass
            else:
                rspd['msg'] = '错误: 未知错误'
        else:
            rspd['msg'] = '错误: 未知错误'
        self.write(json.dumps(rspd))
Exemple #47
0
                            "[Error]: Unable Parse Item {}\n".format(index_url, article_item_div)
            log_error_message(error_message)
            continue
        article_url = article_item_a.get("href")
        print("[Info]: ".ljust(70, "="))
        print("[Index Page]: {}{}".format(ROOT_URL, index_url))
        print("[Article Page]: {}{}".format(ROOT_URL, article_url))
        article_html = retrive_html_from_url(article_url)

        if not article_html:
            error_message = "[Error]: On Page: {}\n" \
                            "[Error]: Unable Retrive Page {}\n".format(index_url, article_url)
            log_error_message(error_message)
            continue

        article = Article(article_html, article_url)
        article_pool.add_article(article)

        article_author = User(article_html, "article",
                              "{}{}".format(ROOT_URL, article_url))
        user_pool.add_user(article_author)

        for comment_div in article_html.find_all(class_="push"):
            try:
                commenter = User(comment_div)
                user_pool.add_user(commenter)
            except:
                error_message = "[Error]: On Page: {}\n" \
                                "[Error]: Unable Parse Comment {}\n".format(article_url, comment_div)
                log_error_message(error_message)
Exemple #48
0
 def run(self):
     count = 0
     while True:
         # count += 1
         # log.info('第{}次'.format(count))
         # ADD_COLLECTION 补采账号  get_account 日常采集; 使用account_list 兼容单个账号和账号列表
         account_list = ADD_COLLECTION if ADD_COLLECTION else self.get_account(
         )
         log.info('一共获取{}账号'.format(len(account_list)))
         if account_list is None:
             log.info('调度队列为空,休眠5秒')
             time.sleep(5)
         for account_name in account_list:
             count += 1
             log.info('第{}次'.format(count))
             try:
                 self.search_name = account_name
                 html_account = self.account_homepage()
                 if html_account:
                     html = html_account
                 else:
                     log.info('{}|找到不到微信号'.format(account_name))
                     continue
                 urls_article = self.urls_article(html)
                 # 确定account信息
                 account = Account()
                 account.name = self.name
                 account.account = account_name
                 account.tags = self.get_tags()
                 account.get_account_id()
                 if not account.account_id:
                     log.info('没有account_id'.format(self.name))
                     break
                 # 判重
                 ids = self.dedup(account_name) if JUDEG else ''
                 entity = None
                 backpack_list = []
                 ftp_list = []
                 ftp_info = None
                 for page_count, url in enumerate(urls_article):
                     # if page_count < 15:
                     #     continue
                     article = Article()
                     try:
                         article.create(url, account)
                     except RuntimeError as run_error:
                         log.info('找不到浏览器 {}'.format(run_error))
                     log.info('第{}条 文章标题: {}'.format(
                         page_count, article.title))
                     log.info("当前文章url: {}".format(url))
                     entity = JsonEntity(article, account)
                     log.info('当前文章ID: {}'.format(entity.id))
                     if entity.id in ids and JUDEG is True:
                         log.info('当前文章已存在,跳过0')
                         # continue
                     backpack = Backpack()
                     backpack.create(entity)
                     backpack_list.append(backpack.create_backpack())
                     # self.save_to_mysql(entity)
                     # self.save_to_mongo(entity.to_dict())
                     # if page_count >= 3:
                     #     break
                 log.info("开始发包")
                 if entity and backpack_list:
                     entity.uploads(backpack_list)
                     log.info("发包完成")
             except Exception as e:
                 log.exception("解析公众号错误 {}".format(e))
                 if 'chrome not reachable' in str(e):
                     raise RuntimeError('chrome not reachable')
Exemple #49
0
def loaddir(directory, clear=False):
    if clear:
        Article.objects.all().delete()

    queue = os.listdir(directory)
    urls = set()
    while queue:
        artfile = queue.pop()
        if artfile[0] == '.': continue
        if artfile in ('template', 'template.rst', 'template.txt'): continue
        artfile = path.join(directory, artfile)
        if path.isdir(artfile):
            queue.extend([
                path.join(artfile,f) for f in os.listdir(artfile)
                ])
            continue

        input = file(artfile)
        header = {}
        linenr = 0
        while True:
            line = input.readline().strip()
            linenr += 1
            if line in ('---', '..'): break
            if line.find(':') < 0:
                raise IOError('gitcms.pages.load: In file %s, line %s. No \':\' found!' % (artfile, linenr))
            tag,value = line.split(':',1)
            value = value.strip()
            header[tag] = value
        blank = input.readline()
        linenr += 1
        if blank.strip():
            raise IOError, 'Blank line expected while processing file (%s:%s)\nGot "%s"' % (artfile, linenr,blank)
        content = input.read()
        content = preprocess_rst_content(content)

        url = header['url']
        if url and url[-1] == '/':
            import warnings
            warnings.warn('''\
gitcms.pages.loaddir: Removing / at end of url (%s)

(Both versions will work for accessing the page.)
''' % url)
            url = url[:-1]

        if url and url[0] == '/':
            import warnings
            warnings.warn('gitcms.pages.loaddir: Removing / at start of url ({0})'
                        .format(url))
            url = url[1:]

        if url in urls:
            raise IOError('gitcms.pages.loaddir: repeated URL detected (%s)' % url)

        taglist = []
        for c in header.get('categories','').split():
            taglist.append(tag_for(c))
        # if we got so far, implies that our article is safe to store.
        urls.add(url)
        A = Article(title=header['title'], url=url, meta=header.get('meta', ''), author=header.get('author', ''), content=content)
        A.save()
        for c in taglist:
            A.tags.add(c)
Exemple #50
0
 def get(self, articles=None):
   articles = Article.query()
   self.render_template('home.html', articles=articles)
Exemple #51
0
 def get(self):
     temp_template = jinja_env.get_template('templates/temp.html')
     all_articles = Article.query().fetch()
     self.response.out.write(temp_template.render({'articles' : all_articles}))
Exemple #52
0
 def get(self):
   user_id = self.user_info['user_id']
   user_key = ndb.Key('User', user_id)
   articles = Article.query(Article.user_key == user_key).fetch()
   self.render_template('user_articles.html', articles=articles)
Exemple #53
0
 def get(self):
     books_post = Article.query().filter(Article.category == 'Books').fetch()
     books_template = jinja_env.get_template('templates/books.html')
     self.response.out.write(books_template.render())
     self.response.write(books_post)
 def result_to_article(self, result):
     title = ('%s - %s') % (result['title'], result['publisher'])
     url = b64encode(result['url'])
     return Article(title, url)
Exemple #55
0
 def get(self):
     feelgood_post = Article.query().filter(Article.category == 'FeelGood').fetch()
     feelgood_template = jinja_env.get_template('templates/feelgood.html')
     self.response.out.write(feelgood_template.render())
     self.response.write(feelgood_post)
 def article_from_feed_entry(self, entry):
     url_parts = urlparse.urlparse(entry.link)
     query_parts = urlparse.parse_qs(url_parts.query)
     url = query_parts['url'][0]
     url = b64encode(url)
     return Article(title=entry.title, url=url)
Exemple #57
0
    def test_regex(self):
        # Create some articles with a bit more interesting headlines
        # for testing field lookups:
        for a in Article.objects.all():
            a.delete()
        now = datetime.now()
        a1 = Article(pub_date=now, headline='f')
        a1.save()
        a2 = Article(pub_date=now, headline='fo')
        a2.save()
        a3 = Article(pub_date=now, headline='foo')
        a3.save()
        a4 = Article(pub_date=now, headline='fooo')
        a4.save()
        a5 = Article(pub_date=now, headline='hey-Foo')
        a5.save()
        a6 = Article(pub_date=now, headline='bar')
        a6.save()
        a7 = Article(pub_date=now, headline='AbBa')
        a7.save()
        a8 = Article(pub_date=now, headline='baz')
        a8.save()
        a9 = Article(pub_date=now, headline='baxZ')
        a9.save()
        # Zero-or-more.
        self.assertQuerysetEqual(
            Article.objects.filter(headline__regex=r'fo*'), [
                '<Article: f>', '<Article: fo>', '<Article: foo>',
                '<Article: fooo>'
            ])
        self.assertQuerysetEqual(
            Article.objects.filter(headline__iregex=r'fo*'), [
                '<Article: f>',
                '<Article: fo>',
                '<Article: foo>',
                '<Article: fooo>',
                '<Article: hey-Foo>',
            ])
        # One-or-more.
        self.assertQuerysetEqual(
            Article.objects.filter(headline__regex=r'fo+'),
            ['<Article: fo>', '<Article: foo>', '<Article: fooo>'])
        # Wildcard.
        self.assertQuerysetEqual(
            Article.objects.filter(headline__regex=r'fooo?'),
            ['<Article: foo>', '<Article: fooo>'])
        # Leading anchor.
        self.assertQuerysetEqual(
            Article.objects.filter(headline__regex=r'^b'),
            ['<Article: bar>', '<Article: baxZ>', '<Article: baz>'])
        self.assertQuerysetEqual(
            Article.objects.filter(headline__iregex=r'^a'),
            ['<Article: AbBa>'])
        # Trailing anchor.
        self.assertQuerysetEqual(Article.objects.filter(headline__regex=r'z$'),
                                 ['<Article: baz>'])
        self.assertQuerysetEqual(
            Article.objects.filter(headline__iregex=r'z$'),
            ['<Article: baxZ>', '<Article: baz>'])
        # Character sets.
        self.assertQuerysetEqual(
            Article.objects.filter(headline__regex=r'ba[rz]'),
            ['<Article: bar>', '<Article: baz>'])
        self.assertQuerysetEqual(
            Article.objects.filter(headline__regex=r'ba.[RxZ]'),
            ['<Article: baxZ>'])
        self.assertQuerysetEqual(
            Article.objects.filter(headline__iregex=r'ba[RxZ]'),
            ['<Article: bar>', '<Article: baxZ>', '<Article: baz>'])

        # And more articles:
        a10 = Article(pub_date=now, headline='foobar')
        a10.save()
        a11 = Article(pub_date=now, headline='foobaz')
        a11.save()
        a12 = Article(pub_date=now, headline='ooF')
        a12.save()
        a13 = Article(pub_date=now, headline='foobarbaz')
        a13.save()
        a14 = Article(pub_date=now, headline='zoocarfaz')
        a14.save()
        a15 = Article(pub_date=now, headline='barfoobaz')
        a15.save()
        a16 = Article(pub_date=now, headline='bazbaRFOO')
        a16.save()

        # alternation
        self.assertQuerysetEqual(
            Article.objects.filter(headline__regex=r'oo(f|b)'), [
                '<Article: barfoobaz>',
                '<Article: foobar>',
                '<Article: foobarbaz>',
                '<Article: foobaz>',
            ])
        self.assertQuerysetEqual(
            Article.objects.filter(headline__iregex=r'oo(f|b)'), [
                '<Article: barfoobaz>',
                '<Article: foobar>',
                '<Article: foobarbaz>',
                '<Article: foobaz>',
                '<Article: ooF>',
            ])
        self.assertQuerysetEqual(
            Article.objects.filter(headline__regex=r'^foo(f|b)'),
            ['<Article: foobar>', '<Article: foobarbaz>', '<Article: foobaz>'])

        # Greedy matching.
        self.assertQuerysetEqual(
            Article.objects.filter(headline__regex=r'b.*az'), [
                '<Article: barfoobaz>',
                '<Article: baz>',
                '<Article: bazbaRFOO>',
                '<Article: foobarbaz>',
                '<Article: foobaz>',
            ])
        self.assertQuerysetEqual(
            Article.objects.filter(headline__iregex=r'b.*ar'), [
                '<Article: bar>',
                '<Article: barfoobaz>',
                '<Article: bazbaRFOO>',
                '<Article: foobar>',
                '<Article: foobarbaz>',
            ])
Exemple #58
0
 def get(self):
     sports_post = Article.query().filter(Article.category == 'Sports').fetch()
     sports_template = jinja_env.get_template('templates/sports.html')
     self.response.out.write(sports_template.render())
     self.response.write(sports_post)
Exemple #59
0
 def test_regex_backreferencing(self):
     # Grouping and backreferences.
     now = datetime.now()
     a10 = Article(pub_date=now, headline='foobar')
     a10.save()
     a11 = Article(pub_date=now, headline='foobaz')
     a11.save()
     a12 = Article(pub_date=now, headline='ooF')
     a12.save()
     a13 = Article(pub_date=now, headline='foobarbaz')
     a13.save()
     a14 = Article(pub_date=now, headline='zoocarfaz')
     a14.save()
     a15 = Article(pub_date=now, headline='barfoobaz')
     a15.save()
     a16 = Article(pub_date=now, headline='bazbaRFOO')
     a16.save()
     self.assertQuerysetEqual(
         Article.objects.filter(headline__regex=r'b(.).*b\1'), [
             '<Article: barfoobaz>', '<Article: bazbaRFOO>',
             '<Article: foobarbaz>'
         ])
Exemple #60
0
 def get(self):
     self.response.write('news')
     news_post = Article.query().filter(Article.category == 'News').fetch()
     news_template = jinja_env.get_template('templates/news.html')
     self.response.out.write(news_template.render())
     self.response.write(news_post)