def POST(self): def check(cate): parent=cate.parent_cat skey=cate.key() while parent: if parent.key()==skey: return False parent=parent.parent_cat return True action=self.param("action") name=self.param("name") slug=self.param("slug") parentkey=self.param('parentkey') key=self.param('key') vals={'action':action,'postback':True} try: if action=='add': cat= Category(name=name,slug=slug) if not (name and slug): raise Exception(_('Please input name and slug.')) if parentkey: cat.parent_cat=Category.get(parentkey) cat.put() self.redirect('/admin/categories') #vals.update({'result':True,'msg':_('Saved ok')}) #self.render2('views/admin/category.html',vals) elif action=='edit': cat=Category.get(key) cat.name=name cat.slug=slug if not (name and slug): raise Exception(_('Please input name and slug.')) if parentkey: cat.parent_cat=Category.get(parentkey) if not check(cat): raise Exception(_('A circle declaration found.')) else: cat.parent_cat=None cat.put() self.redirect('/admin/categories') except Exception ,e : if cat.is_saved(): cates=[c for c in Category.all() if c.key()!=cat.key()] else: cates= Category.all() vals.update({'result':False,'msg':e.message,'category':cat,'key':key,'categories':cates}) self.render2('views/admin/category.html',vals)
def wwb_list_category(): html = '' for category in Category.all()[:10]: html += '<li><a href="%s">%s ( %d )</a></li>' % ( category.get_absolute_url(), category.title.encode('utf-8'), category.posts.count()) return html
def generate_sitemap(): """Deferred task to generate a Sitemap.xml for all published blog posts""" root = etree.Element('urlset', { 'attr': 'http://www.sitemaps.org/schemas/sitemap/0.9' }) def add_url(location, last_modified=None, change_freq='always', priority=0.5): e = etree.SubElement(root, 'url') etree.SubElement(e, 'loc').text = location if last_modified: etree.SubElement(e, 'lastmod').text = last_modified.strftime('%Y-%m-%dT%H:%M:%S+00:00') etree.SubElement(e, 'changefreq').text = change_freq etree.SubElement(e, 'priority').text = str(priority) for p in Post.objects_published().order('-created_at'): add_url(p.absolute_url(external=True), p.updated_at) for c in Category.all(): add_url(c.absolute_url(external=True)) add_url(url_for('blog.index', _external=True), priority=1.0) add_url(url_for('blog.latest_posts', _external=True)) logging.info('Generated sitemap.xml with %d blog posts', len(root)) xml = etree.tostring(root, encoding='utf-8', pretty_print=True, xml_declaration=True) cache.set('blog_sitemaps', xml) return xml
def GET(self,tags=None): urls = [] def addurl(loc,lastmod=None,changefreq=None,priority=None): url_info = { 'location': loc, 'lastmod': lastmod, 'changefreq': changefreq, 'priority': priority } urls.append(url_info) addurl(self.blog.baseurl,changefreq='daily',priority=0.9 ) entries = Entry.all().filter(published = True).order_by('-date')[0:self.blog.sitemap_entries]#.fetch(self.blog.sitemap_entries) for item in entries: loc = "%s/%s" % (self.blog.baseurl, item.link) addurl(loc,item.mod_date or item.date,'never',0.6) if self.blog.sitemap_include_category: cats=Category.all() for cat in cats: loc="%s/category/%s"%(self.blog.baseurl,cat.slug) addurl(loc,None,'weekly',0.5) if self.blog.sitemap_include_tag: tags=Tag.all() for tag in tags: loc="%s/tag/%s"%(self.blog.baseurl, urlencode(tag.tag)) addurl(loc,None,'weekly',0.5) ## self.response.headers['Content-Type'] = 'application/atom+xml' self.render('/admin/views/sitemap.xml', {'urlset':urls}, content_type='text/xml')#, content_type='application/xhtml+xml')
def GET(self,tags=None): try: all=self.param('all') except: all=False if(all): entries = Entry.all().order('-date') filename='micolog.%s.xml'%datetime.now().strftime('%Y-%m-%d') else: str_date_begin=self.param('date_begin') str_date_end=self.param('date_end') try: date_begin=datetime.strptime(str_date_begin,"%Y-%m-%d") date_end=datetime.strptime(str_date_end,"%Y-%m-%d") entries = Entry.all().filter('date >=',date_begin).filter('date <',date_end).order('-date') filename='micolog.%s.%s.xml'%(str(str_date_begin),str(str_date_end)) except: self.render2('views/admin/404.html') cates=Category.all() tags=Tag.all() self.response.headers['Content-Type'] = 'binary/octet-stream'#'application/atom+xml' self.response.headers['Content-Disposition'] = 'attachment; filename=%s'%filename self.render2('views/wordpress.xml',{'entries':entries,'cates':cates,'tags':tags})
def admin_list_category(request): if users.is_current_user_admin(): return object_list(request, template_object_name='obj', queryset=Category.all(), allow_empty=True, extra_context={'type': 'category'}, template_name='admin/list.html', paginate_by=setting.ADMIN_LIST_PER_PAGE, ) else: return HttpResponseRedirect('/')
def admin_list_category(request): if users.is_current_user_admin(): return object_list( request, template_object_name='obj', queryset=Category.all(), allow_empty=True, extra_context={'type': 'category'}, template_name='admin/list.html', paginate_by=setting.ADMIN_LIST_PER_PAGE, ) else: return HttpResponseRedirect('/')
def GET(self,slug=None): action=self.param("action") key=self.param('key') category=None if action and action=='edit': try: category=Category.get(key) except: pass else: action='add' vals={'action':action,'category':category,'key':key,'categories':[c for c in Category.all() if not category or c.key()!=category.key()]} self.render2('views/admin/category.html',vals)
def initialize(self, request): m_pages=Entry.all().filter('entrytype =','page')\ .filter('published =',True)\ .filter('entry_parent =',0)\ .order('menu_order') blogroll=Link.all().filter('linktype =','blogroll') archives=Archive.all().order('-year').order('-month').fetch(12) alltags=Tag.all() self.template_vals.update({ 'menu_pages':m_pages, 'categories':Category.all(), 'blogroll':blogroll, 'archives':archives, 'alltags':alltags, 'recent_comments':Comment.all().order('-date').fetch(5) })
def GET(self,slug=None): if not slug: self.error(404) return try: page_index=int(self.param('page')) except: page_index=1 slug=urldecode(slug) cats=Category.all().filter(slug = slug)[0:1]#.fetch(1) if cats: entries=Entry.all().filter(published = True).filter(categorie_keys = cats[0].key()).order_by("-date") entries,links=Pager(query=entries,items_per_page=20).fetch(page_index) self.render('category',{'entries':entries,'category':cats[0],'pager':links}) else: self.error(404,slug)
def initialize(self, request): BaseRequestHandler.initialize(self,request) m_pages=Entry.all().filter(entrytype = 'page') \ .filter(published = True)\ .filter(entry_parent = 0)\ .order_by('menu_order') blogroll=Link.all().filter(linktype = 'blogroll') #archives=Archive.all().order_by('-year').order_by('-month').fetch(12) archives=Archive.all().order_by('-year', '-month')[0:12] alltags=Tag.all() self.template_vals.update({ 'menu_pages':m_pages, 'categories':Category.all(), 'blogroll':blogroll, 'archives':archives, 'alltags':alltags, 'recent_comments':Comment.all().order_by('-date')[0:5]#.fetch(5) })
def GET(self,slug='post'): action=self.param("action") entry=None cats=Category.all() alltags=Tag.all() if action and action=='edit': try: key=self.param('key') entry=Entry.get(key) except: pass else: action='add' def mapit(cat): return {'name':cat.name,'slug':cat.slug,'select':entry and cat.key() in entry.categorie_keys} vals={'action':action,'entry':entry,'entrytype':slug,'cats':map(mapit,cats),'alltags':alltags} self.render2('views/admin/entry.html',vals)
def process_request(self, request): if not hasattr(request, 'categories'): request.categories = Category.all().order('-post_count') if not hasattr(request, 'most_read_posts'): request.most_read_posts = Post.all().order('-read_count')[:10] return None
def POST(self,slug='post'): action=self.param("action") title=self.param("post_title") content=self.param('content') tags=self.param("tags") cats=self.request.get_all('cats') key=self.param('key') if self.param('publish')!='': published=True elif self.param('unpublish')!='': published=False else: published=self.param('published')=='True' allow_comment=self.parambool('allow_comment') allow_trackback=self.parambool('allow_trackback') entry_slug=self.param('slug') entry_parent=self.paramint('entry_parent') menu_order=self.paramint('menu_order') entry_excerpt=self.param('excerpt').replace('\n','<br />') password=self.param('password') sticky=self.parambool('sticky') is_external_page=self.parambool('is_external_page') target=self.param('target') external_page_address=self.param('external_page_address') def mapit(cat): return {'name':cat.name,'slug':cat.slug,'select':cat.slug in cats} vals={'action':action,'postback':True,'cats':Category.all(),'entrytype':slug, 'cats':map(mapit,Category.all()), 'entry':{ 'title':title,'content':content,'strtags':tags,'key':key,'published':published, 'allow_comment':allow_comment, 'allow_trackback':allow_trackback, 'slug':entry_slug, 'entry_parent':entry_parent, 'excerpt':entry_excerpt, 'menu_order':menu_order, 'is_external_page':is_external_page, 'target':target, 'external_page_address':external_page_address, 'password':password, 'sticky':sticky} } if not (title and (content or (is_external_page and external_page_address))): vals.update({'result':False, 'msg':_('Please input title and content.')}) self.render2('views/admin/entry.html',vals) else: if action=='add': entry= Entry(title=title,content=content) entry.settags(tags) entry.entrytype=slug entry.slug=entry_slug.replace(" ","-") entry.entry_parent=entry_parent entry.menu_order=menu_order entry.excerpt=entry_excerpt entry.is_external_page=is_external_page entry.target=target entry.external_page_address=external_page_address newcates=[] entry.allow_comment=allow_comment entry.allow_trackback=allow_trackback entry.author=self.author.user entry.author_name=self.author.dispname entry.password=password entry.sticky=sticky if cats: for cate in cats: c=Category.all().filter('slug =',cate) if c: newcates.append(c[0].key()) entry.categorie_keys=newcates; entry.save(published) if published: smsg=_('Saved ok. <a href="/blog/%(link)s" target="_blank">View it now!</a>') else: smsg=_('Saved ok.') vals.update({'action':'edit','result':True,'msg':smsg%{'link':str(entry.link)},'entry':entry}) self.render2('views/admin/entry.html',vals) if published and entry.allow_trackback and self.blog.allow_pingback: try: autoPingback(str(entry.fullurl),HTML=content) except: pass elif action=='edit': #try: if 1: entry=Entry.get(key) entry.title=title entry.content=content entry.slug=entry_slug.replace(' ','-') entry.entry_parent=entry_parent entry.menu_order=menu_order entry.excerpt=entry_excerpt entry.is_external_page=is_external_page entry.target=target entry.external_page_address=external_page_address entry.settags(tags) entry.author=self.author.user entry.author_name=self.author.dispname entry.password=password entry.sticky=sticky newcates=[] if cats: for cate in cats: c=Category.all().filter('slug =',cate) if c: newcates.append(c[0].key()) entry.categorie_keys=newcates; entry.allow_comment=allow_comment entry.allow_trackback=allow_trackback entry.save(published) if published: smsg=_('Saved ok. <a href="/blog/%(link)s" target="_blank">View it now!</a>') else: smsg=_('Saved ok.') vals.update({'result':True,'msg':smsg%{'link':str(entry.link)},'entry':entry}) self.render2('views/admin/entry.html',vals) #except: if 0: vals.update({'result':False,'msg':_('Error:Entry can''t been saved.')}) self.render2('views/admin/entry.html',vals)
def wwb_list_category(): html = '' for category in Category.all()[:10]: html += '<li><a href="%s">%s ( %d )</a></li>'%(category.get_absolute_url(), category.title.encode('utf-8'), category.posts.count()) return html
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from django.conf.urls.defaults import * from django.contrib.sitemaps import FlatPageSitemap, GenericSitemap from blog.models import Post, Category, Tag from feeds.models import LatestEntries, HottestEntries feeds = {"latest": LatestEntries, "hottest": HottestEntries} posts = {"queryset": Post.all().filter("is_published", True), "date_field": "create_time"} tags = {"queryset": Tag.all().order("-post_count")} categories = {"queryset": Category.all().order("-post_count")} sitemaps = { "flatpages": FlatPageSitemap, "blog": GenericSitemap(posts, priority=0.6, changefreq="monthly"), #'tag': GenericSitemap(tags, priority=0.5, changefreq='monthly'), #'category': GenericSitemap(categories, priority=0.4, changefreq='yearly'), } urlpatterns = patterns( "", (r"^$", "blog.views.list_post"), (r"update/$", "blog.views.update"), (r"^about/$", "blog.views.about"), (r"^download/$", "blog.views.download"), (r"^account/setting/$", "account.views.setting"),
feeds = { 'latest': LatestEntries, 'hottest': HottestEntries, } posts = { 'queryset': Post.all().filter('is_published', True), 'date_field': 'create_time', } tags = { 'queryset': Tag.all().order('-post_count'), } categories = { 'queryset': Category.all().order('-post_count'), } sitemaps = { 'flatpages': FlatPageSitemap, 'blog': GenericSitemap(posts, priority=0.6, changefreq='monthly'), #'tag': GenericSitemap(tags, priority=0.5, changefreq='monthly'), #'category': GenericSitemap(categories, priority=0.4, changefreq='yearly'), } urlpatterns = patterns('', (r'^$', 'blog.views.list_post'), (r'update/$', 'blog.views.update'), (r'^about/$', 'blog.views.about'), (r'^timeline/$', 'blog.views.timeline'), (r'^events/$', 'blog.views.events'),
def widgets(context): context['categories'] = Category.all() context['tags'] = Tag.all() context['archives'] = Post.getArchives() context['settings'] = UserSettings.getByCurrentUser() return context
feeds = { 'latest': LatestEntries, 'hottest': HottestEntries, } posts = { 'queryset': Post.all().filter('is_published', True), 'date_field': 'create_time', } tags = { 'queryset': Tag.all().order('-post_count'), } categories = { 'queryset': Category.all().order('-post_count'), } sitemaps = { 'flatpages': FlatPageSitemap, 'blog': GenericSitemap(posts, priority=0.6, changefreq='monthly'), #'tag': GenericSitemap(tags, priority=0.5, changefreq='monthly'), #'category': GenericSitemap(categories, priority=0.4, changefreq='yearly'), } urlpatterns = patterns( '', (r'^$', 'blog.views.list_post'), (r'update/$', 'blog.views.update'), (r'^about/$', 'blog.views.about'), (r'^download/$', 'blog.views.download'),
def items(self): return Category.all()