Exemplo n.º 1
0
	def generate(self, page=None, filter_=None, name=None, format='html'):
		# i hate favicon
		if page == 'favicon.ico':
			abort(404)
		
		if page is None or page == 'main':
			page_obj = meta.Session.query(Page).filter(Page.slug=="main").filter(Page.visible==True).one()
			page_id = None
		else:
			try:
				page_obj = meta.Session.query(Page).filter(Page.slug==page).filter(Page.visible==True).one()
			except:
				abort(404)
			page_id = page_obj.id
		
		items_per_page = int(app_globals.settings(u'maximum_items','blog'))
		page_num = int(request.GET.get('p',1))
		first_item = (page_num - 1) * items_per_page
		
		query = meta.Session.query(Article).filter(Article.published != None)
		specs = {'published':{'$ne':None}}
		if page_id is None:
			query = query.filter(Article.page.has(Page.in_main==True))
			#specs['page.in_main'] = True
		else:
			query = query.filter(Article.page.has(Page.id==page_id))
			#specs['page.id'] = page_id
		
		if filter_ == 'user' and name is not None:
			query = query.filter(Article.author_rel.has(User.name==name))
			#specs['author_rel.name'] = name
		elif filter_ == 'tag' and name is not None:
			#specs['tags.id'] = name
			query = query.filter(Article.tags.any(Tag.id==name))
		
		items = query.order_by(Article.sticky.desc(),Article.published.asc()).limit(items_per_page).offset(first_item).options(orm.joinedload('tags')).all()
		#items = Article.fetch(
		#	specs,
		#	sort=[('sticky','DESCENDING'),('published','DESCENDING')],
		#	offset=first_item,limit=items_per_page,options=[orm.joinedload('tags')]
		#)
		
		if filter_ is not None:
			filter_ = (filter_,name)
		else:
			filter_ = None
		
		if format == 'atom':
			response.content_type = 'application/atom+xml'
			return render_jinja2("%s.atom.jinja"%page_obj.template)
		#elif format == 'json':
		#	response.content_type = 'application/json'
		#	return json.dumps({'data':[item.to_dict() for item in items],'page':page_obj})
		elif format == 'html':
			return render_jinja2("%s.jinja"%page_obj.template, extra_vars={'items':items, 'page_num':page_num, 'page':page_obj})
		else:
			abort(404)
Exemplo n.º 2
0
	def _create(self, format, parent_id=None):
		if format == 'json':
			params = self._validate(json.loads(request.body), CreateArticle, 'new')
		#elif format == 'atom':
		#	from lxml import etree
		#	params = Article.parse_xml(etree.fromstring(request.body))
		elif format == 'html':
			params = self._validate(request.POST.mixed(), CreateArticle, 'new')
		else:
			raise UnacceptedFormat(format)
		
		item = Article.from_dict(params, default_thumb=app_globals.settings(u'default_thumb',u'blog'))
		item.add_author_or_contributor(user_from_session(session))
		if item.published is not None:
			item.atom_id = atom.get_tag_uri(url("story", permalink=str(item.permalink), qualified=True), item.published, item.title)
		item.save()
		app_globals.clear_get_cached_article_from_id()
		app_globals.clear_get_article_from_permalink()
		return item
Exemplo n.º 3
0
	def search(self):
		try:
			q = request.GET.getone("q")
			pnum = request.GET.get("pnum",None)
		except:
			redirect(url("main"))
		page_num = 1 if pnum is None else int(pnum)
		items_per_page = int(app_globals.settings(u'maximum_items','blog'))
		first_item = (page_num - 1) * items_per_page
		
		q_string = q.lower()
		res = meta.Session.query(Article).filter(
			sql.or_(
				Article.tags.any(sql.func.lower(Tag.label).contains(q_string)),
				sql.func.lower(Article.title).contains(q_string),
				sql.func.lower(Article.metacontent).contains(q_string)
			)
		).filter(Article.published!=None).filter(Article.page.has(Page.in_main==True)).\
		order_by(Article.published.desc()).limit(items_per_page).offset(first_item)
		items = res.all()
		
		return render_jinja2('/blog/stream.jinja', extra_vars={'items':items, 'page_num':page_num, 'page':None})