def seo_processor(requests): key = 'seo_processor' value = cache.get(key) if value: logger.info('get processor cache.') return value else: logger.info('set processor cache.') setting = get_blog_setting() 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 } cache.set(key, value, 60 * 60 * 10) return value
def load_sidebar(user): """ 加载侧边栏 :return: """ logger.info('load sidebar') recent_articles = Article.objects.filter( status='p')[:settings.SIDEBAR_ARTICLE_COUNT] sidebar_categorys = Category.objects.all() most_read_articles = Article.objects.filter( status='p').order_by('-views')[:settings.SIDEBAR_ARTICLE_COUNT] dates = Article.objects.datetimes('created_time', 'month', order='DESC') links = Links.objects.all() commment_list = Comment.objects.order_by( '-id')[:settings.SIDEBAR_COMMENT_COUNT] show_adsense = settings.SHOW_GOOGLE_ADSENSE # tags= return { 'recent_articles': recent_articles, 'sidebar_categorys': sidebar_categorys, 'most_read_articles': most_read_articles, 'article_dates': dates, 'sidabar_links': links, 'sidebar_comments': commment_list, 'user': user, 'show_adsense': show_adsense }
def load_sidebar(user): """ 加载侧边栏 :return: """ logger.info('load sidebar') recent_articles = Article.objects.filter(status='p')[:settings.SIDEBAR_ARTICLE_COUNT] sidebar_categorys = Category.objects.all() most_read_articles = Article.objects.filter(status='p').order_by('-views')[:settings.SIDEBAR_ARTICLE_COUNT] dates = Article.objects.datetimes('created_time', 'month', order='DESC') links = Links.objects.all() commment_list = Comment.objects.order_by('-id')[:settings.SIDEBAR_COMMENT_COUNT] show_adsense = settings.SHOW_GOOGLE_ADSENSE # 标签云 计算字体大小 # 根据总数计算出平均值 大小为 (数目/平均值)*步长 increment = 5 tags = Tag.objects.all() sidebar_tags = None if tags: s = list(map(lambda t: (t, t.get_article_count()), tags)) count = sum(map(lambda t: t[1], s)) dd = count / len(tags) sidebar_tags = list(map(lambda x: (x[0], x[1], (x[1] / dd) * increment + 10), s)) return { 'recent_articles': recent_articles, 'sidebar_categorys': sidebar_categorys, 'most_read_articles': most_read_articles, 'article_dates': dates, 'sidabar_links': links, 'sidebar_comments': commment_list, 'user': user, 'show_adsense': show_adsense, 'sidebar_tags': sidebar_tags }
def seo_processor(requests): key = 'seo_processor' value = cache.get(key) if value: logger.info('get processor cache.') return value else: logger.info('set processor cache.') setting = get_blog_setting() 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, } cache.set(key, value, 60 * 60 * 10) return value
def convert_to_amap(locations): datas = ';'.join(map(lambda x: str(x.lon) + ',' + str(x.lat), locations)) key = '8440a376dfc9743d8924bf0ad141f28e' api = 'http://restapi.amap.com/v3/assistant/coordinate/convert' query = {'key': key, 'locations': datas, 'coordsys': 'gps'} rsp = requests.get(url=api, params=query) logger.info(type(rsp.text)) result = json.loads(rsp.text) return result['locations']
def comment_list(self): cache_key = 'article_comments_{id}'.format(id=self.id) value = cache.get(cache_key) if value: logger.info('get article comments:{id}'.format(id=self.id)) return value else: comments = self.comment_set.filter(is_enable=True) cache.set(cache_key, comments) logger.info('set article comments:{id}'.format(id=self.id)) return comments
def get_queryset_from_cache(self, cache_key): # raise NotImplementedError() value = cache.get(cache_key) if value: logger.info('get view cache.key:{key}'.format(key=cache_key)) return value else: article_list = self.get_queryset_data() cache.set(cache_key, article_list) logger.info('set view cache.key:{key}'.format(key=cache_key)) return article_list
def comment_list(self): cache_key = 'article_comments_{id}'.format(id=self.id) value = cache.get(cache_key) if value: logger.info('get article comments:{id}'.format(id=self.id)) return value else: comments = self.comment_set.all() cache.set(cache_key, comments) logger.info('set article comments:{id}'.format(id=self.id)) return comments
def getdata(self, content): requrl = self.__build_req_url(content) res = self.UserAgent(requrl).decode('utf-8') logger.info(res) try: jsons = json.loads(res, encoding='utf-8') if str(jsons["code"]) == '100000': return jsons["text"] except Exception as e: logger.warn(e) return "哎呀,出错啦。"
def load_sidebar(user): """ 加载侧边栏 :return: """ logger.info('load sidebar') from DjangoBlog.utils import get_blog_setting blogsetting = get_blog_setting() recent_articles = Article.objects.filter( status='p')[:blogsetting.sidebar_article_count] sidebar_categorys = Category.objects.all() extra_sidebars = SideBar.objects.filter( is_enable=True).order_by('sequence') most_read_articles = Article.objects.filter( status='p').order_by('-views')[:blogsetting.sidebar_article_count] dates = Article.objects.datetimes('created_time', 'month', order='DESC') links = Links.objects.all() commment_list = Comment.objects.filter( is_enable=True).order_by('-id')[:blogsetting.sidebar_comment_count] # show_adsense = settings.SHOW_GOOGLE_ADSENSE # 标签云 计算字体大小 # 根据总数计算出平均值 大小为 (数目/平均值)*步长 increment = 5 tags = Tag.objects.all() sidebar_tags = None if tags and len(tags) > 0: s = list(map(lambda t: (t, t.get_article_count()), tags)) count = sum(map(lambda t: t[1], s)) dd = 1 if count == 0 else count / len(tags) sidebar_tags = list( map(lambda x: (x[0], x[1], (x[1] / dd) * increment + 10), s)) return { 'recent_articles': recent_articles, 'sidebar_categorys': sidebar_categorys, 'most_read_articles': most_read_articles, 'article_dates': dates, 'sidabar_links': links, 'sidebar_comments': commment_list, 'user': user, 'show_google_adsense': blogsetting.show_google_adsense, 'google_adsense_codes': blogsetting.google_adsense_codes, 'open_site_comment': blogsetting.open_site_comment, 'show_gongan_code': blogsetting.show_gongan_code, 'sidebar_tags': sidebar_tags, 'extra_sidebars': extra_sidebars }
def get_access_token_by_code(self, code): params = { 'client_id': self.client_id, 'client_secret': self.client_secret, 'grant_type': 'authorization_code', 'code': code, 'redirect_uri': self.callback_url } rsp = self.do_post(self.TOKEN_URL, params) obj = json.loads(rsp) try: self.access_token = str(obj['access_token']) self.openid = str(obj['id_token']) logger.info(self.ICON_NAME + ' oauth ' + rsp) return self.access_token except: logger.info(self.ICON_NAME + ' oauth error ' + rsp) return None
def get_oauth_userinfo(self): if not self.is_authorized: return None params = {'access_token': self.access_token} rsp = self.do_get(self.API_URL, params) try: datas = json.loads(rsp) user = OAuthUser() user.picture = datas['picture'] user.nikename = datas['name'] user.openid = datas['sub'] user.type = 'google' if datas['email']: user.email = datas['email'] return user except: logger.info('google oauth error.rsp:' + rsp) return None
def get_oauth_userinfo(self): params = {'access_token': self.access_token} rsp = self.do_get(self.API_URL, params) try: datas = json.loads(rsp) user = OAuthUser() user.picture = datas['avatar_url'] user.nikename = datas['name'] user.openid = datas['id'] user.type = 'github' if datas['email']: user.email = datas['email'] return user except: logger.info('github oauth error.rsp:' + rsp) return None
def load_sidebar(user): """ 加载侧边栏 :return: """ logger.info('load sidebar') from DjangoBlog.utils import get_blog_setting blogsetting = get_blog_setting() recent_articles = Article.objects.filter(status='p')[:blogsetting.sidebar_article_count] sidebar_categorys = Category.objects.all() extra_sidebars = SideBar.objects.filter(is_enable=True).order_by('sequence') most_read_articles = Article.objects.filter(status='p').order_by('-views')[:blogsetting.sidebar_article_count] dates = Article.objects.datetimes('created_time', 'month', order='DESC') links = Links.objects.all() commment_list = Comment.objects.filter(is_enable=True).order_by('-id')[:blogsetting.sidebar_comment_count] # show_adsense = settings.SHOW_GOOGLE_ADSENSE # 标签云 计算字体大小 # 根据总数计算出平均值 大小为 (数目/平均值)*步长 increment = 5 tags = Tag.objects.all() sidebar_tags = None if tags and len(tags) > 0: s = list(map(lambda t: (t, t.get_article_count()), tags)) count = sum(map(lambda t: t[1], s)) dd = 1 if count == 0 else count / len(tags) sidebar_tags = list(map(lambda x: (x[0], x[1], (x[1] / dd) * increment + 10), s)) return { 'recent_articles': recent_articles, 'sidebar_categorys': sidebar_categorys, 'most_read_articles': most_read_articles, 'article_dates': dates, 'sidabar_links': links, 'sidebar_comments': commment_list, 'user': user, 'show_google_adsense': blogsetting.show_google_adsense, 'google_adsense_codes': blogsetting.google_adsense_codes, 'open_site_comment': blogsetting.open_site_comment, 'show_gongan_code': blogsetting.show_gongan_code, 'sidebar_tags': sidebar_tags, 'extra_sidebars': extra_sidebars }
def manage_owntrack_log(request): try: s = json.loads(request.read().decode('utf-8')) tid = s['tid'] lat = s['lat'] lon = s['lon'] logger.info('tid:{tid}.lat:{lat}.lon:{lon}'.format(tid=tid, lat=lat, lon=lon)) if tid and lat and lon: m = OwnTrackLog() m.tid = tid m.lat = lat m.lon = lon m.save() return HttpResponse('ok') else: return HttpResponse('data error') except Exception as e: logger.warn(e) return HttpResponse('error')
def get_oauth_userinfo(self): if not self.is_authorized: return None params = {'uid': self.openid, 'access_token': self.access_token} rsp = self.do_get(self.API_URL, params) try: datas = json.loads(rsp) user = OAuthUser() user.picture = datas['avatar_large'] user.nikename = datas['screen_name'] user.openid = datas['id'] user.type = 'weibo' user.token = self.access_token if 'email' in datas and datas['email']: user.email = datas['email'] return user except: logger.info('weibo oauth error.rsp:' + rsp) return None
def seo_processor(requests): key = 'seo_processor' value = cache.get(key) if value: logger.info('get processor cache.') return value else: logger.info('set processor cache.') value = { 'SITE_NAME': settings.SITE_NAME, 'SHOW_GOOGLE_ADSENSE': settings.SHOW_GOOGLE_ADSENSE, 'SITE_SEO_DESCRIPTION': settings.SITE_SEO_DESCRIPTION, 'SITE_DESCRIPTION': settings.SITE_DESCRIPTION, 'SITE_KEYWORDS': settings.SITE_SEO_KEYWORDS, 'SITE_BASE_URL': requests.scheme + '://' + requests.get_host() + '/', 'ARTICLE_SUB_LENGTH': settings.ARTICLE_SUB_LENGTH, 'nav_category_list': Category.objects.all(), 'nav_pages': Article.objects.filter(type='p', status='p') } cache.set(key, value, 60 * 60 * 10) return value
def get_oauth_userinfo(self): params = { 'access_token': self.access_token } rsp = self.do_get(self.API_URL, params) try: datas = json.loads(rsp) user = OAuthUser() user.picture = datas['avatar_url'] user.nikename = datas['name'] user.openid = datas['id'] user.type = 'github' user.token = self.access_token user.matedata = rsp if datas['email']: user.email = datas['email'] return user except: logger.info('github oauth error.rsp:' + rsp) return None
def get_oauth_userinfo(self): if not self.is_authorized: return None params = { 'access_token': self.access_token } rsp = self.do_get(self.API_URL, params) try: datas = json.loads(rsp) user = OAuthUser() user.matedata = rsp user.picture = datas['picture'] user.nikename = datas['name'] user.openid = datas['sub'] user.token = self.access_token user.type = 'google' if datas['email']: user.email = datas['email'] return user except: logger.info('google oauth error.rsp:' + rsp) return None
def get_oauth_userinfo(self): if not self.is_authorized: return None params = { 'uid': self.openid, 'access_token': self.access_token } rsp = self.do_get(self.API_URL, params) try: datas = json.loads(rsp) user = OAuthUser() user.matedata = rsp user.picture = datas['avatar_large'] user.nikename = datas['screen_name'] user.openid = datas['id'] user.type = 'weibo' user.token = self.access_token if 'email' in datas and datas['email']: user.email = datas['email'] return user except: logger.info('weibo oauth error.rsp:' + rsp) return None
def get_oauth_userinfo(self): params = {'access_token': self.access_token} logger.info('get userinfo request' + json.dumps(params)) rsp = self.do_get(self.API_URL, params) logger.info('get userinfo response' + rsp) try: datas = json.loads(rsp) logger.info('get userinfo datas' + json.dumps(datas)) user = OAuthUser() user.picture = datas['avatar_url'] user.nikename = datas['login'] user.openid = datas['id'] user.type = 'github' user.token = self.access_token user.matedata = rsp if datas['email']: user.email = datas['email'] return user except: logger.info('github oauth error.rsp:' + rsp) return None
def get_access_token_by_code(self, code): params = { 'client_id': self.client_id, 'client_secret': self.client_secret, 'code': code } logger.info('send accesss request' + json.dumps(params)) rsp = self.do_post(self.TOKEN_URL, params) logger.info('get access token resp' + rsp) try: from urllib import parse r = parse.parse_qs(rsp) self.access_token = (r['access_token'][0]) logger.info('get access token success') return self.access_token except: return None
def authorize(request): manager = None type = request.GET.get('type', None) if not type: return HttpResponseRedirect('/') manager = get_manager_by_type(type) if not manager: return HttpResponseRedirect('/') logger.info('get manager success') code = request.GET.get('code', None) rsp = manager.get_access_token_by_code(code) nexturl = request.GET.get('next_url', None) if not nexturl: nexturl = '/' if not rsp: logger.info('get access_token failed') return HttpResponseRedirect(manager.get_authorization_url(nexturl)) user = manager.get_oauth_userinfo() if user: if not user.nikename: import datetime user.nikename = "djangoblog" + datetime.datetime.now().strftime('%y%m%d%I%M%S') try: user = OAuthUser.objects.get(type=type, openid=user.openid) except ObjectDoesNotExist: pass # facebook的token过长 if type == 'facebook': user.token = '' email = user.email if email: author = None try: author = get_user_model().objects.get(id=user.author_id) except ObjectDoesNotExist: pass if not author: result = get_user_model().objects.get_or_create(email=user.email) author = result[0] if result[1]: author.username = user.nikename author.save() user.author = author logger.info('start to save user') user.save() logger.info('save user success') login(request, author) return HttpResponseRedirect(nexturl) if not email: logger.info('start to save user') user.save() logger.info('save user success') url = reverse('oauth:require_email', kwargs={ 'oauthid': user.id }) return HttpResponseRedirect(url) else: return HttpResponseRedirect(nexturl)