def seo_processor(requests): key = 'seo_processor' value = None # cache.get(key) if value: return value else: logger.info('set processor cache.') setting = BlogSettings.objects.first() if not setting: setting = BlogSettings(sitename='site1') setting.save() value = { 'SITE_NAME': setting.sitename, 'SHOW_GOOGLE_ADSENSE': setting.show_google_adsense, 'GOOGLE_ADSENSE_CODES': setting.google_adsense_codes, 'SITE_SEO_DESCRIPTION': setting.site_seo_description, 'SITE_DESCRIPTION': setting.site_description, 'SITE_KEYWORDS': setting.site_keywords, 'SITE_BASE_URL': requests.scheme + '://' + requests.get_host() + '/', 'ARTICLE_SUB_LENGTH': setting.article_sub_length, # 'nav_category_list': Category.objects.all(), # 'nav_pages': Article.objects.filter(type='p', status='p'), 'OPEN_SITE_COMMENT': setting.open_site_comment, 'BEIAN_CODE': setting.beiancode, 'ANALYTICS_CODE': setting.analyticscode, "BEIAN_CODE_GONGAN": setting.gongan_beiancode, "SHOW_GONGAN_CODE": setting.show_gongan_code, "CURRENT_YEAR": datetime.now().year } # cache.set(key, value, 60 * 60 * 10) return value
def index(request): """Home page view""" site_settings = SiteSettings.load() blog_settings = BlogSettings.load() recent_posts = Post.get_displayable()[:site_settings.number_recent_posts] if site_settings.number_recent_posts > 0 and len(recent_posts) > 0: post_col_width = round(10. / float(site_settings.number_recent_posts)) else: post_col_width = 0 if post_col_width > 0: max_len_to_use = site_settings.number_recent_posts if len(recent_posts) < site_settings.number_recent_posts: post_col_width = min(round(10. / float(len(recent_posts))), 5) max_len_to_use = len(recent_posts) while post_col_width * max_len_to_use > 10: post_col_width -= 1 return render( request, "index.html", { 'settings': site_settings, 'recent_posts': recent_posts, 'show_authors': blog_settings.show_authors, 'post_col_width': post_col_width, 'home_show_card': site_settings.home_show_card, 'home_tagline': site_settings.home_tagline, 'home_image': site_settings.home_image, })
def archive(request): """ 文章归档 :param request: :return: """ _blog_list = Article.objects.values("id", "title", "date_time").order_by('-date_time') archive_dict = {} for blog in _blog_list: pub_month = blog.get("date_time").strftime("%Y{}%m".format('-')) if pub_month in archive_dict: archive_dict[pub_month].append(blog) else: archive_dict[pub_month] = [blog] data = sorted([{ "date": _[0], "blogs": _[1] } for _ in archive_dict.items()], key=lambda item: item["date"], reverse=True) _blogSettings = BlogSettings.get_blog_setting() gongan_code = re.findall('\d+', _blogSettings.gongan_beiancode ) if _blogSettings.show_gongan_code else '' return render( request, 'blog/archive.html', { "data": data, "settings": _blogSettings, "gongan_code": gongan_code[0] if gongan_code else '', })
def form_valid(self, form): q = BlogSettings.all().get() if q is not None: q.coments_authenticate = form.cleaned_data['coments_authenticate'] q.put() else: q = BlogSettings(coments_authenticate=form.cleaned_data['coments_authenticate']).put() return super(Admin_Blog_Settings_IndexController, self).form_valid(form)
def get_initial(self): """ Get initial data for the form """ q = BlogSettings.all().get() if q is not None: self.initial['coments_authenticate'] = q.coments_authenticate return self.initial.copy()
def get_context_data(self, slug, **kwargs): context = super(PostHomePage, self).get_context_data(**kwargs) settings = BlogSettings.all().get() posts = Posts.all().filter("is_active =", True).filter("slug =", slug).get() context['post'] = posts context['comments'] = posts.comments_set.fetch(None) context['settings'] = settings return context
def message(request): _blogSettings = BlogSettings.get_blog_setting() gongan_code = re.findall('\d+', _blogSettings.gongan_beiancode ) if _blogSettings.show_gongan_code else '' return render( request, 'blog/message_board.html', { "source_id": "message", "changyan_code": _blogSettings.changyan_code, "settings": _blogSettings, "gongan_code": gongan_code[0] if gongan_code else '', })
def detail(request, pk): """ 博文详情 :param request: :param pk: :return: """ blog = get_object_or_404(Article, pk=pk) blog.viewed() _blogSettings = BlogSettings.get_blog_setting() gongan_code = re.findall('\d+', _blogSettings.gongan_beiancode ) if _blogSettings.show_gongan_code else '' return render( request, 'blog/detail.html', { "blog": blog, "changyan_code": _blogSettings.changyan_code, "settings": _blogSettings, "gongan_code": gongan_code[0] if gongan_code else '', })
def blog_list(request): """ 列表 :param request: :return: """ page_number = get_page(request) blog_count = Article.objects.count() page_info = PageInfo(page_number, blog_count) _blog_list = Article.objects.all()[page_info.index_start:page_info. index_end] _blogSettings = BlogSettings.get_blog_setting() gongan_code = re.findall('\d+', _blogSettings.gongan_beiancode ) if _blogSettings.show_gongan_code else '' return render( request, 'blog/list.html', { "blog_list": _blog_list, "page_info": page_info, "settings": _blogSettings, "gongan_code": gongan_code[0] if gongan_code else '', })
def tag(request, name): """ 标签 :param request: :param name :return: """ page_number = get_page(request) blog_count = Article.objects.filter(tag__tag_name=name).count() page_info = PageInfo(page_number, blog_count) _blog_list = Article.objects.filter( tag__tag_name=name)[page_info.index_start:page_info.index_end] _blogSettings = BlogSettings.get_blog_setting() gongan_code = re.findall('\d+', _blogSettings.gongan_beiancode ) if _blogSettings.show_gongan_code else '' return render( request, 'blog/tag.html', { "blog_list": _blog_list, "tag": name, "page_info": page_info, "settings": _blogSettings, "gongan_code": gongan_code[0] if gongan_code else '', })
def search(request): """ 搜索 :param request: :return: """ key = request.GET['key'] page_number = get_page(request) blog_count = Article.objects.filter(title__icontains=key).count() page_info = PageInfo(page_number, blog_count) _blog_list = Article.objects.filter( title__icontains=key)[page_info.index_start:page_info.index_end] _blogSettings = BlogSettings.get_blog_setting() gongan_code = re.findall('\d+', _blogSettings.gongan_beiancode ) if _blogSettings.show_gongan_code else '' return render( request, 'blog/search.html', { "blog_list": _blog_list, "pages": page_info, "key": key, "settings": _blogSettings, "gongan_code": gongan_code[0] if gongan_code else '', })
def index(request): _blog_list = Article.objects.all().order_by('-date_time')[0:5] _blog_hot = Article.objects.all().order_by('-view')[0:6] _carousels = Carousels.get_carousels() _blogSettings = BlogSettings.get_blog_setting() gongan_code = re.findall('\d+', _blogSettings.gongan_beiancode ) if _blogSettings.show_gongan_code else '' return render( request, 'blog/index.html', { "blog_list": _blog_list, "blog_hot": _blog_hot, "carousels": _carousels, "settings": _blogSettings, "gongan_code": gongan_code[0] if gongan_code else '', "messages": _blogSettings.multi_messages.split('\n') if _blogSettings.multi_messages else [] })
def get_blog_setting(): value = cache.get('get_blog_setting') if value: logger.info('get cache get_blog_setting') return value else: from blog.models import BlogSettings if not BlogSettings.objects.count(): setting = BlogSettings() setting.sitename = 'DjangoBlog' setting.site_description = '基于Django的博客系统' setting.site_seo_description = '基于Django的博客系统' setting.site_keywords = 'Django,Python' setting.article_sub_length = 300 setting.sidebar_article_count = 10 setting.sidebar_comment_count = 5 setting.show_google_adsense = False setting.open_site_comment = True setting.analyticscode = '' setting.beiancode = '' setting.show_gongan_code = False setting.save() value = BlogSettings.objects.first() logger.info('set cache get_blog_setting') cache.set('get_blog_setting', value) return value
def get_blog_setting(): value = cache.get('get_blog_setting') if value: return value else: from blog.models import BlogSettings if not BlogSettings.objects.count(): setting = BlogSettings() setting.sitename = 'jingBlog' setting.site_description = '' setting.site_seo_description = '' setting.site_keywords = 'Django,Python' setting.article_sub_length = 300 setting.sidebar_article_count = 10 setting.sidebar_comment_count = 5 setting.show_google_adsense = False setting.open_site_comment = True setting.analyticscode = '' setting.beiancode = '鄂ICP备2020017327号' setting.show_gongan_code = False setting.save() ## 第一次生产后就定死了 暂时修改为重复辅助 else : setting = BlogSettings() setting.sitename = 'jingBlog' setting.site_description = '' setting.site_seo_description = '' setting.site_keywords = 'Django,Python' setting.article_sub_length = 300 setting.sidebar_article_count = 10 setting.sidebar_comment_count = 5 setting.show_google_adsense = False setting.open_site_comment = True setting.analyticscode = '' setting.beiancode = '鄂ICP备2020017327号' setting.show_gongan_code = False setting.save() inedx = len(BlogSettings.objects.all())-1 value = BlogSettings.objects.all()[inedx] logger.info('set cache get_blog_setting') cache.set('get_blog_setting', value) return value
def get_blog_setting(): value = cache.get('get_blog_setting') if value: return value else: from blog.models import BlogSettings if not BlogSettings.objects.count(): setting = BlogSettings() setting.sitename = 'agrosite' setting.site_description = 'This is the description of this site' setting.site_seo_description = 'This is the seo description of this site' setting.site_keywords = 'agrosite,farm,agriculture,agrohelp,farmer,vegetables,agroapp' setting.article_sub_length = 200 setting.sidebar_article_count = 10 setting.sidebar_comment_count = 5 setting.show_google_adsense = False setting.open_site_comment = True setting.analyticscode = '' setting.beiancode = '' setting.show_gongan_code = False setting.save() value = BlogSettings.objects.first() logger.info('set cache get_blog_setting') cache.set('get_blog_setting', value) return value
def get_blog_setting(): value = cache.get('get_blog_setting') if value: return value else: from blog.models import BlogSettings if not BlogSettings.objects.count(): setting = BlogSettings() setting.sitename = 'mtuktarov empire' setting.site_description = 'if I had my own world...' setting.site_seo_description = 'mtuktarov' setting.site_keywords = 'love,hope,truth' setting.article_sub_length = 300 setting.sidebar_article_count = 10 setting.sidebar_comment_count = 5 setting.show_google_adsense = False setting.enable_site_comment = True setting.analyticscode = '' setting.footer_title = 'if I had my own world...' setting.show_views_bar = False setting.show_category_bar = False setting.show_search_bar = False setting.show_menu_bar = False setting.save() value = BlogSettings.objects.first() # logger.debug('set cache get_blog_setting') cache.set('get_blog_setting', value) return value
def get_blog_setting(): value = cache.get('get_blog_setting') if value: return value else: from blog.models import BlogSettings if not BlogSettings.objects.count(): setting = BlogSettings() setting.sitename = 'DjangoBlog' setting.site_description = 'Blog system based on Django' setting.site_seo_description = 'Blog system based on Django' setting.site_keywords = 'Django,Python' setting.article_sub_length = 300 setting.sidebar_article_count = 10 setting.sidebar_comment_count = 5 setting.show_google_adsense = False setting.open_site_comment = True setting.analyticscode = '' setting.beiancode = '' setting.show_gongan_code = False setting.save() value = BlogSettings.objects.first() logger.info('set cache get_blog_setting') cache.set('get_blog_setting', value) return value
def get_blog_setting(): value = cache.get('get_blog_setting') if 1 == 2 and value: return value else: from blog.models import BlogSettings if not BlogSettings.objects.count(): setting = BlogSettings() setting.sitename = '华泰辉创' setting.site_description = '基于Django的博客系统' setting.site_seo_description = '基于Django的博客系统' setting.site_keywords = 'Django,Python' setting.article_sub_length = 300 setting.sidebar_article_count = 10 setting.sidebar_comment_count = 5 setting.show_google_adsense = False setting.open_site_comment = True setting.analyticscode = '' setting.beiancode = '京ICP备19011312号-1' setting.gongan_beiancode = '' setting.show_gongan_code = False setting.save() value = BlogSettings.objects.first() logger.info('set cache get_blog_setting value={%s}' % value) #cache.set('get_blog_setting', value) return value