def get(self,req_user=''): user_lang = 'en' #********************** User Auth **************************# user = users.get_current_user() nickname = '' if user: nickname=user.nickname() if nickname: user_info = User.all().filter('user',nickname) if user_info.count(1)>0: user_info = user_info.get() user_lang = user_info.lang auth_url = users.create_logout_url(self.request.uri) auth_text= 'signout' else: auth_url = users.create_login_url(self.request.uri) auth_text= 'signin' entry_count =Entry.all().count(1000) if req_user: tag_user = req_user tags = Tag.all().filter("user",req_user) else: tag_user = '******' tags = Tag.all() tags_count = tags.count(1000) tag_list=[] for tag in tags: tag_count=tag.count_link + tag.count_note + tag.count_pic if tag.count_link >= tag.count_note: if tag.count_link >= tag.count_pic: max_type = 'link' else: max_type = 'pic' else: if tag.count_pic >= tag.count_note: max_type = 'pic' else: max_type = 'note' #logging.info(tag_count) #logging.info(entry_count) #logging.info(tags_count) tag_list.append({ "info":tag, "type":max_type, "level":int(round(tag_count/(float(entry_count)/tags_count))) }) template_values = { 'nickname' : nickname, 'req_user' : req_user, 'auth_url' : auth_url, 'auth_text': auth_text, 'tag_user' : tag_user, 'tags' : tag_list, 'uri' : self.request.uri } path = os.path.join(os.path.dirname(__file__),'templates/'+user_lang+'/tag.html') self.response.out.write(template.render(path,template_values))
def post(self): """Creates a new tag for the logged in user""" user = utils.get_current_user() title = self.request.get('title') new_title = self.request.get('newTitle') or None if not user: self.error(403) return if not title: self.error(400) # Create new tag if not new_title: m = Tag.all().ancestor(user).filter('title_lower =', title.lower()).get() if m: # Not unique self.error(409) return m = Tag( parent=user, title=title, title_lower=title.lower(), ) m.put() # Rename Tag else: m = Tag.all().ancestor(user).filter('title_lower =', new_title.lower()).get() if m: # Not unique self.error(409) return m = Tag.all().ancestor(user).filter('title_lower =', title.lower()).get() if not m: # Original tag not found self.error(404) return m.title = new_title m.title_lower = new_title.lower() m.save() self.response.headers['Content-Type'] = 'text/plain; charset=utf-8' self.response.headers['Access-Control-Allow-Origin'] = '*' self.response.out.write('ok')
def get(self): user = users.get_current_user() key = self.request.get('key') if key and users.is_current_user_admin(): e = db.get(key) if e: if e.tags: for tag_name in e.tags: tag = Tag.all().filter('name',tag_name) if(tag.count(1)>0): t = tag.get() if e.type == 'link': t.count_link -= 1 if e.type == 'note': t.count_note -= 1 if e.type == 'pic': t.count_pic -= 1 t.put() db.delete(e) self.response.out.write('1') else: self.response.out.write('0') memcache.delete(key) else: self.response.out.write('0')
def delete(self, title): """Delete the specified tag for the current user""" user = utils.get_current_user() title = title.decode('utf-8') if not user: self.error(403) return m = Tag.all().ancestor(user).filter('title_lower =', title.lower()).get() if not m: # Original tag not found self.error(404) return entries = Entry.all().filter('tags =', m.key()) # Remove tag from entries for entry in entries: logging.info(entry) entry.tags.remove(m.key()) entry.save() m.delete() self.response.headers['Content-Type'] = 'text/plain; charset=utf-8' self.response.headers['Access-Control-Allow-Origin'] = '*' self.response.out.write('ok')
def get(self): user = users.get_current_user() key = self.request.get('key') if key and users.is_current_user_admin(): e = db.get(key) if e: if e.tags: for tag_name in e.tags: tag = Tag.all().filter('name', tag_name) if (tag.count(1) > 0): t = tag.get() if e.type == 'link': t.count_link -= 1 if e.type == 'note': t.count_note -= 1 if e.type == 'pic': t.count_pic -= 1 t.put() db.delete(e) self.response.out.write('1') else: self.response.out.write('0') memcache.delete(key) else: self.response.out.write('0')
def get(self, user, tag_title): """Gets RSS feed for a user, filtered by tag""" user = utils.get_user_model_by_id_or_nick(user) if not user: self.error(403) return tag_title = tag_title.decode('utf-8') tag = Tag.all().ancestor(user.key()).filter('title_lower =', tag_title.lower()).get() if not tag: self.error(404) return entries = Entry.all().filter('tags =', tag.key()).filter('published =', True).order('-time_published').fetch(20) entries = [e.to_struct() for e in entries] path = os.path.join(os.path.dirname(__file__), 'template.rss') self.response.headers['Content-Type'] = 'application/xml; charset=utf-8' self.response.out.write(template.render(path, { 'entries': entries, 'url': self.request.url, 'title': tag_title, }))
def update_basic_info( update_categories=False, update_tags=False, update_links=False, update_comments=False, update_archives=False, update_pages=False): from model import Entry,Archive,Comment,Category,Tag,Link basic_info = ObjCache.get(is_basicinfo=True) if basic_info is not None: info = ObjCache.get_cache_value(basic_info.cache_key) if update_pages: info['menu_pages'] = Entry.all().filter('entrytype =','page')\ .filter('published =',True)\ .filter('entry_parent =',0)\ .order('menu_order').fetch(limit=1000) if update_archives: info['archives'] = Archive.all().order('-year').order('-month').fetch(12) if update_comments: info['recent_comments'] = Comment.all().order('-date').fetch(5) if update_links: info['blogroll'] = Link.all().filter('linktype =','blogroll').fetch(limit=1000) if update_tags: info['alltags'] = Tag.all().order('-tagcount').fetch(limit=100) if update_categories: info['categories'] = Category.all().fetch(limit=1000) logging.debug('basic_info updated') basic_info.update(info)
def get(self): entry_count = Entry.all().count(1000) tags = Tag.all().order('usetime') tags_count = tags.count(1000) tag_list = [] for tag in tags: tag_count = tag.count_link + tag.count_note + tag.count_pic if tag.count_link >= tag.count_note: if tag.count_link >= tag.count_pic: max_type = 'link' else: max_type = 'pic' else: if tag.count_pic >= tag.count_note: max_type = 'pic' else: max_type = 'note' tag_list.append({ "info": tag, "type": max_type, "level": tag_count / (entry_count / tags_count) }) template_values = {'tags': tag_list} path = os.path.join(os.path.dirname(__file__), 'templates/tag.html') self.response.out.write(template.render(path, template_values))
def getTagsProposalsForText(text): result = [] tags = Tag.all() for tag in tags: if re.search(r'\b'+re.escape(tag.name)+r'\b',text): result.append(tag.name) result = excludeMismatches(result) return result
def get(self): entry_count =Entry.all().count(1000) tags = Tag.all().order('usetime') tags_count = tags.count(1000) tag_list=[] for tag in tags: tag_count=tag.count_link + tag.count_note + tag.count_pic if tag.count_link >= tag.count_note: if tag.count_link >= tag.count_pic: max_type = 'link' else: max_type = 'pic' else: if tag.count_pic >= tag.count_note: max_type = 'pic' else: max_type = 'note' tag_list.append({ "info":tag, "type":max_type, "level":tag_count/(entry_count/tags_count) }) template_values = { 'tags' : tag_list } path = os.path.join(os.path.dirname(__file__),'templates/tag.html') self.response.out.write(template.render(path,template_values))
def getTagLists(): key_ = "tag_lists" tags = memcache.get(key_) if tags is not None: return tags else: tags_ = Tag.all() tags = [x for x in tags_] if not memcache.add(key_, tags, 3600): logging.error("Memcache set failed.") return tags
def get(self): user_lang = 'en' #********************** User Auth **************************# user = users.get_current_user() nickname = '' if user: nickname = user.nickname() if nickname: user_info = User.all().filter('user', nickname) if user_info.count(1) > 0: user_info = user_info.get() user_lang = user_info.lang auth_url = users.create_logout_url(self.request.uri) auth_text = 'signout' key = self.request.get('key') if key: e = db.get(key) title = e.title url = e.url purl = '' content = e.content type = e.type tags = ' '.join(tag for tag in e.tags) tags += ' ' else: title = unescape(self.request.get('title')) url = unescape(self.request.get('url')) purl = unescape(self.request.get('purl')) content = unescape(self.request.get('content')) type = self.request.get('type') if not type: type = 'link' tags = '' template_values = { 'nickname': nickname, 'auth_url': auth_url, 'auth_text': auth_text, 'type': type, 'key': key, 'tag_list': Tag.all().filter("user", nickname).order('usetime'), 'title': title, 'url': url, 'purl': purl, 'content': content, 'tags': tags } path = os.path.join(os.path.dirname(__file__), 'templates/' + user_lang + '/add.html') self.response.out.write(template.render(path, template_values)) else: self.redirect(users.create_login_url(self.request.uri))
def getTagList(): key_ = "blog_tagList_key" try: tags = memcache.get(key_) except Exception: tags = None if tags is None: tags = Tag.all().filter('valid',True).order('tag') memcache.add(key=key_, value=tags, time=3600) else: logging.debug("getTagList from cache. ") return tags
def get(self): if users.is_current_user_admin(): key = self.request.get('key') if key: e = db.get(key) title = e.title url = e.url purl = '' content = e.content type = e.type tags = ' '.join(tag for tag in e.tags) tags +=' ' else: title = unescape(self.request.get('title')) url = unescape(self.request.get('url')) purl = unescape(self.request.get('purl')) content = unescape(self.request.get('content')) type= self.request.get('type') if not type: type = 'link' tags = '' template_values = { 'type' : type, 'key' : key, 'tag_list': Tag.all().order('usetime'), 'title' : title, 'url' : url, 'purl' : purl, 'content' : content, 'tags' : tags } path = os.path.join(os.path.dirname(__file__),'templates/add.html') self.response.out.write(template.render(path,template_values)) else: self.redirect(users.create_login_url(self.request.uri))
def get(self): user_lang = 'en' #********************** User Auth **************************# user = users.get_current_user() nickname = '' if user: nickname=user.nickname() if nickname: user_info = User.all().filter('user',nickname) if user_info.count(1)>0: user_info = user_info.get() user_lang = user_info.lang auth_url = users.create_logout_url(self.request.uri) auth_text= 'signout' key = self.request.get('key') if key: e = db.get(key) title = e.title url = e.url purl = '' content = e.content type = e.type tags = ' '.join(tag for tag in e.tags) tags +=' ' else: title = unescape(self.request.get('title')) url = unescape(self.request.get('url')) purl = unescape(self.request.get('purl')) content = unescape(self.request.get('content')) type= self.request.get('type') if not type: type = 'link' tags = '' template_values = { 'nickname' : nickname, 'auth_url' : auth_url, 'auth_text': auth_text, 'type' : type, 'key' : key, 'tag_list': Tag.all().filter("user",nickname).order('usetime'), 'title' : title, 'url' : url, 'purl' : purl, 'content' : content, 'tags' : tags } path = os.path.join(os.path.dirname(__file__),'templates/'+user_lang+'/add.html') self.response.out.write(template.render(path,template_values)) else: self.redirect(users.create_login_url(self.request.uri))
def get(self,format): tags = Tag.all().run() db.delete(tags) firstLevelTags = [ "ActionScript", "Asp", "BASIC", "C", "C++", "Clojure", "COBOL", "ColdFusion", "Erlang", "Fortran", "Groovy", "Haskell", "Java", "JavaScript", "Lisp", "Perl", "PHP", "Python", "Ruby", "Scala", "Scheme", "haxe", "nodejs", 'framework', 'tool', 'wiki', 'tutorial', 'howto', 'library', 'service', 'language' ] for tag in firstLevelTags: t = Tag(name=tag.lower()) t.put() entries = Entry.all() for e in entries: newtags = getTagKeys(e.tagsRaw) e.tags = newtags e.put() simplewebapp.formatResponse(format, self, "OK")
def get(self): """Gets the tags for the logged in user""" user = utils.get_current_user() if not user: self.error(403) return tags = Tag.all().ancestor(user) self.response.headers['Content-Type'] = 'application/json; charset=utf-8' self.response.headers['Access-Control-Allow-Origin'] = '*' tags = [i.to_struct() for i in tags] self.response.out.write(json.dumps(tags))
def get(self,format="json"): query = self.request.get("q").lower() if len(query) == 0: simplewebapp.formatResponse(format,self,[]) return resultedTags = [] tags = TagModel.all().run() for tag in tags: if tag.name.startswith(query): resultedTags.append(tag.name) simplewebapp.formatResponse(format,self,resultedTags)
def post(self, feed_id, entry_id, tag_title): """ Create or modify tag """ current_user = utils.get_current_user() if not current_user: logging.info('no user logged in') self.error(403) return logging.info(feed_id) feed = InputFeed.get_by_id(int(feed_id), parent=current_user) if not feed: logging.info('no feed found') self.error(404) return entry = Entry.get_by_id(int(entry_id), parent=feed) if not entry: logging.info('no entry found') self.error(404) return if not tag_title: logging.info('no tag_title provided found') self.error(400) return tag_title = tag_title.decode('utf-8') tag = Tag.all().ancestor(current_user).filter('title_lower =', tag_title.lower()).get() if not tag: logging.info('tag not found, creating new') tag = Tag( parent=current_user, title=tag_title, title_lower=tag_title.lower(), ) tag.tag_entry(entry) self.response.headers['Content-Type'] = 'text/plain; charset=utf-8' self.response.headers['Access-Control-Allow-Origin'] = '*' self.response.out.write('ok')
def get(self): """Gets all entries from all feeds this user subscribes to""" user = utils.get_current_user() if not user: self.error(403) return # Filter and sorting order = self.request.get('order') lang = self.request.get('lang') tag_title = self.request.get('tag') # Filter if tag_title: tag = Tag.all().ancestor(user.key()).filter('title_lower =', tag_title.lower()).get() entries = Entry.all().filter('tags =', tag.key()) else: entries = Entry.all().ancestor(user) if lang and lang != 'all': entries = entries.filter('language =', lang) # Sorting if order: if order == 'date-asc': entries = entries.order('time_published') elif order == 'date-desc': entries = entries.order('-time_published') elif order == 'title-asc': entries = entries.order('title') elif order == 'title-desc': entries = entries.order('-title') else: entries = entries.order('-time_published') entries = entries.fetch(25) self.response.headers['Content-Type'] = 'application/json; charset=utf-8' self.response.headers['Access-Control-Allow-Origin'] = '*' entries = [i.to_struct(include_tags=True) for i in entries] self.response.out.write(json.dumps(entries))
def get(self,format): url = self.request.get("url").lower() try: content = resource.get(url).decodeBody().lower() except: content = "" tags = JSONDecoder().decode(content); ''' appends only new tags ''' newTags = [] for tag in tags: t = None t = Tag.all().filter("name = ", tag).fetch(1) if len(t) == 0: newTag = saveTag(tag, None); newTags.append(newTag.name) simplewebapp.formatResponse(format, self, newTags)
def delete(self, feed_id, entry_id, tag_title): """ Delete tag """ current_user = utils.get_current_user() tag_title = tag_title.decode('utf-8') if not current_user: logging.info('no user logged in') self.error(403) return logging.info(feed_id) feed = InputFeed.get_by_id(int(feed_id), parent=current_user) if not feed: logging.info('no feed found') self.error(404) return entry = Entry.get_by_id(int(entry_id), parent=feed) if not entry: logging.info('no entry found') self.error(404) return if not tag_title: logging.info('no tag_title provided found') self.error(400) return tag = Tag.all().ancestor(current_user).filter('title_lower =', tag_title.lower()).get() if not tag: logging.info('no tag found') self.error(404) return entry.tags.remove(tag.key()) entry.save() self.response.headers['Content-Type'] = 'text/plain; charset=utf-8' self.response.headers['Access-Control-Allow-Origin'] = '*' self.response.out.write('ok')
def get(self): if users.is_current_user_admin(): key = self.request.get('key') if key: e = db.get(key) title = e.title url = e.url purl = '' content = e.content type = e.type tags = ' '.join(tag for tag in e.tags) tags += ' ' else: title = unescape(self.request.get('title')) url = unescape(self.request.get('url')) purl = unescape(self.request.get('purl')) content = unescape(self.request.get('content')) type = self.request.get('type') if not type: type = 'link' tags = '' template_values = { 'type': type, 'key': key, 'tag_list': Tag.all().order('usetime'), 'title': title, 'url': url, 'purl': purl, 'content': content, 'tags': tags } path = os.path.join(os.path.dirname(__file__), 'templates/add.html') self.response.out.write(template.render(path, template_values)) else: self.redirect(users.create_login_url(self.request.uri))
def post(self): #********************** User Auth **************************# user = users.get_current_user() nickname = '' if user: nickname = user.nickname() if nickname: key = self.request.get('key') if key: e = db.get(key) if e.user != nickname: e = Entry() e.user = nickname else: e = Entry() e.user = nickname type = self.request.get('type') if not type: type = 'link' tz = self.request.get('tz') if tz[0:1] == '-': tz = int(tz[1:]) tz = -tz else: tz = int(tz[1:]) tz = +tz title = self.request.get('title') e.title = title.replace('&', '&').replace('<', '<').replace( '>', '>') url = self.request.get('url') purl = self.request.get('purl') if type == 'pic' and not key: e.url = purl.replace('&', '&').replace('<', '<').replace( '>', '>') else: e.url = url.replace('&', '&').replace('<', '<').replace( '>', '>') content = self.request.get('content') e.content = content if not key: e.addtime += datetime.timedelta(hours=tz) e.private = bool(int(self.request.get('private'))) e.type = type if type == 'pic' and not key: if url: try: result = urlfetch.fetch(url) if result.status_code == 200: e.image = db.Blob(result.content) except: self.response.out.write( 'Fetch picture fail! You can <a href="/add?type=pic">upload</a> it manually' ) return else: myfile = self.request.get("myfile") if not myfile: self.response.out.write('No file specified!') return try: e.image = db.Blob(myfile) except: self.response.out.write('Uploading fail!') return if key: #更新数据 for oldtag in e.tags: tag = Tag.all().filter("user", nickname).filter('name', oldtag) if (tag.count(1) > 0): t = tag.get() if type == 'link': t.count_link -= 1 if type == 'note': t.count_note -= 1 if type == 'pic': t.count_pic -= 1 t.put() else: #新增数据 max_pageCount = 900 #超过此数据,则pageid递增 entry = Entry.all().order('-addtime') if entry.count() > 0: cur_pageid = entry.get().pageid else: cur_pageid = 0 cur_pageCount = entry.filter('pageid =', cur_pageid).count(1000) if cur_pageCount >= max_pageCount: e.pageid = cur_pageid + 1 else: e.pageid = cur_pageid e.tags = [] tag_names = self.request.get('tags').split() for tag_name in tag_names: tag = Tag.all().filter("user", nickname).filter('name', tag_name) if (tag.count(1) > 0): t = tag.get() if type == 'link': t.count_link += 1 if type == 'note': t.count_note += 1 if type == 'pic': t.count_pic += 1 t.user = nickname t.usetime = datetime.datetime.now() t.put() else: t = Tag() t.name = tag_name if type == 'link': t.count_link = 1 if type == 'note': t.count_note = 1 if type == 'pic': t.count_pic = 1 t.user = nickname t.usetime = datetime.datetime.now() t.put() e.tags.append(db.Category(tag_name)) e.put() self.redirect('/' + type + '/' + nickname) else: self.redirect(users.create_login_url(self.request.uri))
def post(self): #********************** User Auth **************************# user = users.get_current_user() nickname = '' if user: nickname=user.nickname() if nickname: key = self.request.get('key') if key : e = db.get(key) if e.user != nickname: e = Entry() e.user = nickname else: e = Entry() e.user = nickname type = self.request.get('type') if not type: type = 'link' tz = self.request.get('tz') if tz[0:1]=='-': tz = int(tz[1:]) tz = -tz else: tz = int(tz[1:]) tz = +tz title = self.request.get('title') e.title = title.replace('&','&').replace('<','<').replace('>','>') url = self.request.get('url') purl= self.request.get('purl') if type == 'pic' and not key: e.url = purl.replace('&','&').replace('<','<').replace('>','>') else: e.url = url.replace('&','&').replace('<','<').replace('>','>') content = self.request.get('content') e.content = content if not key: e.addtime +=datetime.timedelta(hours=tz) e.private = bool(int(self.request.get('private'))) e.type = type if type =='pic' and not key: if url: try: result = urlfetch.fetch(url) if result.status_code == 200: e.image = db.Blob(result.content) except : self.response.out.write('Fetch picture fail! You can <a href="/add?type=pic">upload</a> it manually') return else: myfile = self.request.get("myfile") if not myfile: self.response.out.write( 'No file specified!') return try: e.image = db.Blob(myfile) except : self.response.out.write( 'Uploading fail!') return if key:#更新数据 for oldtag in e.tags: tag = Tag.all().filter("user",nickname).filter('name',oldtag) if(tag.count(1)>0): t = tag.get() if type == 'link': t.count_link -=1 if type == 'note': t.count_note -=1 if type == 'pic': t.count_pic -=1 t.put() else:#新增数据 max_pageCount =900 #超过此数据,则pageid递增 entry = Entry.all().order('-addtime') if entry.count()>0: cur_pageid = entry.get().pageid else: cur_pageid = 0 cur_pageCount = entry.filter('pageid =',cur_pageid).count(1000) if cur_pageCount>=max_pageCount: e.pageid = cur_pageid+1 else: e.pageid = cur_pageid e.tags = [] tag_names = self.request.get('tags').split() for tag_name in tag_names: tag = Tag.all().filter("user",nickname).filter('name',tag_name) if(tag.count(1)>0): t = tag.get() if type == 'link': t.count_link +=1 if type == 'note': t.count_note +=1 if type == 'pic': t.count_pic +=1 t.user = nickname t.usetime = datetime.datetime.now() t.put() else: t = Tag() t.name = tag_name if type == 'link': t.count_link =1 if type == 'note': t.count_note =1 if type == 'pic': t.count_pic =1 t.user = nickname t.usetime = datetime.datetime.now() t.put() e.tags.append(db.Category(tag_name)) e.put() self.redirect('/'+type+'/'+nickname) else: self.redirect(users.create_login_url(self.request.uri))
def get(self): tags = Tag.all().order("-entrycount") template_values = {"tags": tags} self.generate("admin/admin_tags.html", template_values)
def post(self): if users.is_current_user_admin(): key = self.request.get('key') if key : e = db.get(key) else: e = Entry() type = self.request.get('type') if not type: type = 'link' title = self.request.get('title') e.title = title.replace('&','&').replace('<','<').replace('>','>') url = self.request.get('url') purl= self.request.get('purl') if type == 'pic' and not key: e.url = purl.replace('&','&').replace('<','<').replace('>','>') else: e.url = url.replace('&','&').replace('<','<').replace('>','>') content = self.request.get('content') e.content = content if not key: e.addtime +=datetime.timedelta(hours=+8) e.private = bool(int(self.request.get('private'))) e.type = type if type =='pic' and not key: if url: try: result = urlfetch.fetch(url) if result.status_code == 200: e.image = db.Blob(result.content) except : self.response.out.write('获取图片超时!') return else: myfile = self.request.get("myfile") if not myfile: self.response.out.write( '没有选择文件!') return try: e.image = db.Blob(myfile) except : self.response.out.write( '文件上传失败!') return if key:#更新数据 for oldtag in e.tags: tag = Tag.all().filter('name',oldtag) if(tag.count(1)>0): t = tag.get() if type == 'link': t.count_link -=1 if type == 'note': t.count_note -=1 if type == 'pic': t.count_pic -=1 t.put() else:#新增数据 max_pageCount =900 #超过此数据,则pageid递增 entry = Entry.all().order('-addtime') if entry.count()>0: cur_pageid = entry.get().pageid else: cur_pageid = 0 cur_pageCount = entry.filter('pageid =',cur_pageid).count(1000) if cur_pageCount>=max_pageCount: e.pageid = cur_pageid+1 else: e.pageid = cur_pageid e.tags = [] tag_names = self.request.get('tags').split() for tag_name in tag_names: tag = Tag.all().filter('name',tag_name) if(tag.count(1)>0): t = tag.get() if type == 'link': t.count_link +=1 if type == 'note': t.count_note +=1 if type == 'pic': t.count_pic +=1 t.usetime = datetime.datetime.now() t.put() else: t = Tag() t.name = tag_name if type == 'link': t.count_link =1 if type == 'note': t.count_note =1 if type == 'pic': t.count_pic =1 t.usetime = datetime.datetime.now() t.put() e.tags.append(db.Category(tag_name)) e.put() self.redirect('/'+type+'/') else: self.redirect(users.create_login_url(self.request.uri))
def getTags(): result = [] tags = Tag.all() for i in tags: result.append(i.name) return result
def get(self, req_type='link', req_tag=''): if not req_type: req_type = 'link' #********************** User Auth **************************# isAdmin = False user = users.get_current_user() if user: if users.is_current_user_admin(): isAdmin = True auth_url = users.create_logout_url(self.request.uri) auth_text = '注销' else: auth_url = users.create_login_url(self.request.uri) auth_text = '登录' #********************** Pagenator init**************************# limit = 20 p = self.request.get('p') if not p: p = 1 else: p = int(p) offset = (p - 1) * limit #********************** Query **************************# e = Entry.all().filter('type', req_type).order("-addtime") if req_tag: e = e.filter("tags", unquote(req_tag).decode('utf-8')) if not isAdmin: e = e.filter("private", False) if e and e.count() > 0: cur_pageid = e.get().pageid else: cur_pageid = 0 item_count = 0 while cur_pageid >= 0: entry = Entry.all().filter('type', req_type) if req_tag: entry = entry.filter('tags', unquote(req_tag).decode('utf-8')) if not isAdmin: entry = entry.filter('private', False) item_count += entry.filter('pageid', cur_pageid).count() cur_pageid -= 1 e = e.fetch(limit, offset) #********************** Pagenator **************************# page_count = int(math.ceil(item_count / float(limit))) #总页数 if page_count <= 7: page_numbers = range(1, page_count + 1) else: if p <= 6: page_numbers = range(1, max(1, p - 3)) else: page_numbers = [1, 2] + ['...'] page_numbers += range(max(1, p - 3), min(p + 4, page_count + 1)) if p >= page_count - 5: page_numbers += range(min(p + 4, page_count + 1), page_count + 1) else: page_numbers += (['...'] + range(page_count - 1, page_count + 1)) template_values = { 'user': user, 'req_type': req_type, 'isAdmin': isAdmin, 'auth_url': auth_url, 'auth_text': auth_text, 'entry_list': e, 'tag_list': Tag.all().order("-count_" + req_type), 'is_paginated': page_count > 1, 'has_next': p * limit < item_count, 'has_previous': p > 1, 'current_page': p, 'next_page': p + 1, 'previous_page': p - 1, 'pages': page_count, 'page_numbers': page_numbers, 'count': item_count } path = os.path.join(os.path.dirname(__file__), 'templates/' + req_type + '.html') self.response.out.write(template.render(path, template_values))
def get(self,req_type='link',req_tag=''): if not req_type: req_type = 'link' #********************** User Auth **************************# isAdmin = False user = users.get_current_user() if user: if users.is_current_user_admin(): isAdmin = True auth_url = users.create_logout_url(self.request.uri) auth_text= '注销' else: auth_url = users.create_login_url(self.request.uri) auth_text= '登录' #********************** Pagenator init**************************# limit = 20; p = self.request.get('p') if not p: p=1 else: p = int(p) offset = (p-1)*limit #********************** Query **************************# e = Entry.all().filter('type',req_type).order("-addtime") if req_tag: e = e.filter("tags", unquote(req_tag).decode('utf-8')) if not isAdmin: e = e.filter("private", False) if e and e.count()>0: cur_pageid = e.get().pageid else: cur_pageid = 0 item_count = 0 while cur_pageid>=0: entry=Entry.all().filter('type',req_type) if req_tag: entry = entry.filter('tags',unquote(req_tag).decode('utf-8')) if not isAdmin: entry = entry.filter('private', False) item_count += entry.filter('pageid',cur_pageid).count() cur_pageid -=1 e = e.fetch(limit,offset) #********************** Pagenator **************************# page_count = int(math.ceil(item_count / float(limit))) #总页数 if page_count <=7 : page_numbers = range(1,page_count+1) else: if p<=6: page_numbers = range(1,max(1,p-3)) else: page_numbers = [1,2] + ['...'] page_numbers += range(max(1,p-3),min(p+4,page_count+1)) if p >= page_count-5: page_numbers += range(min(p+4,page_count+1),page_count+1) else: page_numbers += (['...']+range(page_count-1,page_count+1)) template_values = { 'user' : user, 'req_type' : req_type, 'isAdmin' : isAdmin, 'auth_url' : auth_url, 'auth_text': auth_text, 'entry_list': e, 'tag_list' : Tag.all().order("-count_"+req_type), 'is_paginated': page_count> 1, 'has_next': p*limit < item_count, 'has_previous': p > 1, 'current_page': p, 'next_page': p + 1, 'previous_page': p - 1, 'pages': page_count, 'page_numbers': page_numbers, 'count': item_count } path = os.path.join(os.path.dirname(__file__),'templates/'+req_type+'.html') self.response.out.write(template.render(path,template_values))
def post(self): if users.is_current_user_admin(): key = self.request.get('key') if key: e = db.get(key) else: e = Entry() type = self.request.get('type') if not type: type = 'link' title = self.request.get('title') e.title = title.replace('&', '&').replace('<', '<').replace( '>', '>') url = self.request.get('url') purl = self.request.get('purl') if type == 'pic' and not key: e.url = purl.replace('&', '&').replace('<', '<').replace( '>', '>') else: e.url = url.replace('&', '&').replace('<', '<').replace( '>', '>') content = self.request.get('content') e.content = content if not key: e.addtime += datetime.timedelta(hours=+8) e.private = bool(int(self.request.get('private'))) e.type = type if type == 'pic' and not key: if url: try: result = urlfetch.fetch(url) if result.status_code == 200: e.image = db.Blob(result.content) except: self.response.out.write('获取图片超时!') return else: myfile = self.request.get("myfile") if not myfile: self.response.out.write('没有选择文件!') return try: e.image = db.Blob(myfile) except: self.response.out.write('文件上传失败!') return if key: #更新数据 for oldtag in e.tags: tag = Tag.all().filter('name', oldtag) if (tag.count(1) > 0): t = tag.get() if type == 'link': t.count_link -= 1 if type == 'note': t.count_note -= 1 if type == 'pic': t.count_pic -= 1 t.put() else: #新增数据 max_pageCount = 900 #超过此数据,则pageid递增 entry = Entry.all().order('-addtime') if entry.count() > 0: cur_pageid = entry.get().pageid else: cur_pageid = 0 cur_pageCount = entry.filter('pageid =', cur_pageid).count(1000) if cur_pageCount >= max_pageCount: e.pageid = cur_pageid + 1 else: e.pageid = cur_pageid e.tags = [] tag_names = self.request.get('tags').split() for tag_name in tag_names: tag = Tag.all().filter('name', tag_name) if (tag.count(1) > 0): t = tag.get() if type == 'link': t.count_link += 1 if type == 'note': t.count_note += 1 if type == 'pic': t.count_pic += 1 t.usetime = datetime.datetime.now() t.put() else: t = Tag() t.name = tag_name if type == 'link': t.count_link = 1 if type == 'note': t.count_note = 1 if type == 'pic': t.count_pic = 1 t.usetime = datetime.datetime.now() t.put() e.tags.append(db.Category(tag_name)) e.put() self.redirect('/' + type + '/') else: self.redirect(users.create_login_url(self.request.uri))
def get(self): tags = Tag.all().order('-entrycount') template_values = { 'tags':tags, } self.generate('admin/admin_tags.html',template_values)
def get(self, req_user=''): user_lang = 'en' #********************** User Auth **************************# user = users.get_current_user() nickname = '' if user: nickname = user.nickname() if nickname: user_info = User.all().filter('user', nickname) if user_info.count(1) > 0: user_info = user_info.get() user_lang = user_info.lang auth_url = users.create_logout_url(self.request.uri) auth_text = 'signout' else: auth_url = users.create_login_url(self.request.uri) auth_text = 'signin' entry_count = Entry.all().count(1000) if req_user: tag_user = req_user tags = Tag.all().filter("user", req_user) else: tag_user = '******' tags = Tag.all() tags_count = tags.count(1000) tag_list = [] for tag in tags: tag_count = tag.count_link + tag.count_note + tag.count_pic if tag.count_link >= tag.count_note: if tag.count_link >= tag.count_pic: max_type = 'link' else: max_type = 'pic' else: if tag.count_pic >= tag.count_note: max_type = 'pic' else: max_type = 'note' #logging.info(tag_count) #logging.info(entry_count) #logging.info(tags_count) tag_list.append({ "info": tag, "type": max_type, "level": int(round(tag_count / (float(entry_count) / tags_count))) }) template_values = { 'nickname': nickname, 'req_user': req_user, 'auth_url': auth_url, 'auth_text': auth_text, 'tag_user': tag_user, 'tags': tag_list, 'uri': self.request.uri } path = os.path.join(os.path.dirname(__file__), 'templates/' + user_lang + '/tag.html') self.response.out.write(template.render(path, template_values))