def list(self): from uliweb.utils.generic import ListView pageno = request.GET.get('page', 0) view = ListView(self.model, pageno=pageno, order_by=self.model.c.publish_date.desc()) return {'blogs':view.query(), 'view':view, 'count':view.query().count()}
def list(self): from uliweb import request from uliweb.utils.generic import ListView from uliweb.utils.common import get_choice import math pageno = int(request.values.get('page', 1)) - 1 rows_per_page=int(request.values.get('rows', settings.get_var('MESSAGES/PAGE_NUMS', 10))) read_flag = request.GET.get('read', '') type_flag = request.GET.get('type', '') condition = None condition = (self.model.c.user == request.user.id) & condition condition = (self.model.c.send_flag == 'r') & condition if read_flag: condition = (self.model.c.read_flag == bool(read_flag=='1')) & condition if type_flag: condition = (self.model.c.type == type_flag) & condition def create_date(value, obj): from uliweb.utils.timesince import timesince return timesince(value) def user_image(value, obj): return functions.get_user_image(obj.sender, size=20) def message(value, obj): return value fields_convert_map = {'create_date':create_date, 'user_image':user_image, 'message':message} view = ListView(self.model, condition=condition, order_by=[self.model.c.create_date.desc()], rows_per_page=rows_per_page, pageno=pageno, fields_convert_map=fields_convert_map) view.query() result = {} result['read_flag'] = read_flag result['type_flag'] = type_flag result['message_type_name'] = get_choice(settings.get_var('MESSAGES/MESSAGE_TYPE'), type_flag, '全部类型') pages = int(math.ceil(1.0*view.total/rows_per_page)) # result['page'] = pageno+1 # result['total'] = view.total # result['pages'] = pages result['pagination'] = functions.create_pagination(functions.request_url(), view.total, pageno+1, rows_per_page) result['objects'] = list(view.objects()) ids = [] for row in result['objects']: ids.append(row._obj_.id) self.model.filter(self.model.c.id.in_(ids)).update(read_flag=True) _del_key(request.user.id) return result
def list(self): from uliweb.utils.generic import ListView pageno = request.GET.get("page", 0) view = ListView(self.model, pageno=pageno, order_by=self.model.c.publish_date.desc()) return {"blogs": view.query(), "view": view, "count": view.query().count()}
def sended_list(self): from uliweb import request from uliweb.utils.generic import ListView from uliweb.utils.common import get_choice import math pageno = int(request.values.get("page", 1)) - 1 rows_per_page = int(request.values.get("rows", settings.get_var("MESSAGES/PAGE_NUMS", 10))) read_flag = request.GET.get("read", "") type_flag = request.GET.get("type", "") condition = None condition = (self.model.c.sender == request.user.id) & condition condition = (self.model.c.send_flag == "s") & condition if read_flag: condition = (self.model.c.read_flag == bool(read_flag == "1")) & condition if type_flag: condition = (self.model.c.type == type_flag) & condition def create_date(value, obj): from uliweb.utils.timesince import timesince return timesince(value) def user_image(value, obj): return functions.get_user_image(obj.user, size=20) def message(value, obj): return value fields_convert_map = {"create_date": create_date, "user_image": user_image, "message": message} view = ListView( self.model, condition=condition, order_by=[self.model.c.create_date.desc()], rows_per_page=rows_per_page, pageno=pageno, fields_convert_map=fields_convert_map, ) view.query() result = {} result["read_flag"] = read_flag result["type_flag"] = type_flag result["message_type_name"] = get_choice(settings.get_var("MESSAGES/MESSAGE_TYPE"), type_flag, "全部类型") pages = int(math.ceil(1.0 * view.total / rows_per_page)) # result['page'] = pageno+1 # result['total'] = view.total # result['pages'] = pages result["pagination"] = functions.create_pagination(request.url, view.total, pageno + 1, rows_per_page) result["objects"] = view.objects() return result
def list(self): from uliweb.utils.generic import ListView pageno = request.GET.get('page', 0) view = ListView(self.model, pageno=pageno, order_by=self.model.c.publish_date.desc()) return { 'blogs': view.query(), 'view': view, 'count': view.query().count() }
def view(self): #objects = self.blogs.all() # model = functions.get_model('blogs').all() # print self.blogs # print model # return {'objects':model} from uliweb.utils.generic import ListView pageno = int(request.GET.get('page',1)) - 1 rows_per_page = int(request.GET.get('rows', 8)) view = ListView(self.blogs, order_by=self.blogs.c.modified_time.desc(), pageno=pageno, rows_per_page=rows_per_page) view.query() pagination = functions.create_pagination(request.url, view.total, pageno+1, rows_per_page) return {'objects': view.objects(),'pagination': pagination}
def index(self): """ 教程显示首页 """ from uliweb.utils.generic import ListView condition = (self.model.c.deleted == False) pageno = int(request.GET.get('page', 1)) - 1 rows_per_page = int(request.GET.get('rows', 10)) cateobjects = self.model_cate.all() # def render(r, obj): # from uliweb import Storage # # data = Storage(dict(r)) # data['image'] = obj.get_image() # data['author'] = unicode(obj.modified_user) # data['modified_date'] = self._get_date(obj.modified_date) # return data def image(value, obj): return obj.get_image() def author(value, obj): return unicode(obj.modified_user) def modified_date(value, obj): return self._get_date(obj.modified_date) fields_convert_map = { 'image': image, 'author': author, 'modified_date': modified_date } view = ListView(self.model, condition=condition, order_by=self.model.c.modified_date.desc(), pageno=pageno, rows_per_page=rows_per_page, fields_convert_map=fields_convert_map) view.query() pagination = functions.create_pagination(request.url, view.total, pageno + 1, rows_per_page) return { 'pagination': pagination, 'objects': view.objects(), 'cateobjects': cateobjects }
def test(self): from uliweb.utils.generic import ListView print functions #页面编号 pageno = int(request.GET.get('page', 1)) - 1 #每页文章数量,5为每页显示的文章 rows_per_page = int(request.GET.get('rows', 8)) print int(request.GET.get('rows', 1)) #后面的参数为请求网址,所有记录数, 总页数, 每页文章数 def modified_date(value, obj): return self._get_date(obj.modified_date) #fields_convert_map = {'modified_date': modified_date} #view = ListView(self.model, order_by=self.model.c.modified_time.desc(), pageno=pageno, rows_per_page=rows_per_page, fields_convert_map=fields_convert_map) view = ListView(self.model, order_by=self.model.c.modified_time.desc(), pageno=pageno, rows_per_page=rows_per_page) view.query() pagination = functions.create_pagination(request.url, view.total, pageno+1, rows_per_page) return {'objects': view.objects(),'pagination': pagination}
def index(self): """ 教程显示首页 """ from uliweb.utils.generic import ListView condition = (self.model.c.deleted==False) pageno = int(request.GET.get('page', 1)) - 1 rows_per_page = int(request.GET.get('rows', 10)) cateobjects = self.model_cate.all() # def render(r, obj): # from uliweb import Storage # # data = Storage(dict(r)) # data['image'] = obj.get_image() # data['author'] = unicode(obj.modified_user) # data['modified_date'] = self._get_date(obj.modified_date) # return data def image(value, obj): return obj.get_image() def author(value, obj): return unicode(obj.modified_user) def modified_date(value, obj): return self._get_date(obj.modified_date) fields_convert_map = {'image':image, 'author':author, 'modified_date':modified_date} view = ListView(self.model, condition=condition, order_by=self.model.c.modified_date.desc(), pageno=pageno, rows_per_page=rows_per_page, fields_convert_map=fields_convert_map) view.query() pagination = functions.create_pagination(request.url, view.total, pageno+1, rows_per_page) return {'pagination':pagination, 'objects':view.objects(),'cateobjects':cateobjects}
def topic_view(self, forum_id, topic_id): """ 显示某主题页面 """ from uliweb.utils.generic import ListView from uliweb import settings pageno = int(request.values.get('page', 1)) - 1 rows_per_page=int(request.values.get('rows', settings.get_var('PARA/FORUM_PAGE_NUMS'))) Post = get_model('forumpost') Topic = get_model('forumtopic') Forum = get_model('forum') topic = Topic.get(int(topic_id)) forum = topic.forum condition = Post.c.topic == int(topic_id) condition1 = (Post.c.parent == None) & condition condition2 = (Post.c.parent != None) & condition order_by = [Post.c.floor] def created_on(value, obj): return date.to_local(value).strftime('%Y-%m-%d %H:%M:%S %Z') def content(value, obj): return self._get_post_content(obj) def username(value, obj): try: username = unicode(obj.posted_by) except NotFound: username = obj._posted_by_ return username def userimage(value, obj): get_user_image = function('get_user_image') try: url = get_user_image(obj.posted_by) except NotFound: url = get_user_image() return url def actions(value, obj): if not request.user: return '' a = [] is_manager = forum.managers.has(request.user) if obj.floor == 1 and obj.parent == None: #第一楼为主贴,可以允许关闭,顶置等操作 if is_manager: a.append('<a href="#" rel="%d" class="close_thread">%s</a>' % (obj.id, self.status['close'][obj.topic.closed])) a.append('<a href="#" rel="%d" class="hidden">%s</a>' % (obj.id, self.status['hidden'][obj.topic.hidden])) a.append('<a href="#" rel="%d" class="top">%s</a>' % (obj.id, self.status['sticky'][obj.topic.sticky])) a.append('<a href="#" rel="%d" class="essence">%s</a>' % (obj.id, self.status['essence'][obj.topic.essence])) a.append('<a href="#" rel="%d" class="homepage">%s</a>' % (obj.id, self.status['homepage'][obj.topic.homepage])) if is_manager or (obj.posted_by.id == request.user.id and obj.created_on+timedelta(days=settings.get_var('PARA/FORUM_EDIT_DELAY'))>=date.now()): #作者或管理员且在n天之内,则可以编辑 url = url_for(ForumView.edit_topic, forum_id=forum_id, topic_id=topic_id) a.append('<a href="%s" rel="%d" class="edit">编辑</a>' % (url, obj.id)) if is_manager: url = url_for(ForumView.remove_topic, forum_id=forum_id, topic_id=topic_id) a.append('<a href="%s" rel="%d" class="delete_topic">删除主题</a>' % (url, obj.id)) #处理贴子转移,管理员可以转移 if is_manager or request.user.is_superuser: url = url_for(ForumView.move_topic, forum_id=forum_id, topic_id=topic_id) a.append('<a href="%s" rel="%d" class="move_topic">移动主题</a>' % (url, obj.id)) if is_manager or (obj.posted_by.id == request.user.id): if (obj.deleted and (obj.deleted_by.id == request.user.id or is_manager)) or not obj.deleted: a.append('<a href="#" rel="%d" class="delete">%s</a>' % (obj.id, self.status['delete'][obj.deleted])) try: obj.posted_by if obj.posted_by.id == request.user.id: a.append('<a href="#" rel="%d" class="email">%s</a>' % (obj.id, self.status['email'][obj.reply_email])) except NotFound: obj.posted_by = None obj.save() a.append('<a href="/%d/%d/%d/new_reply">回复该作者</a>' % (forum_id, topic_id, obj.id)) return ' | '.join(a) def updated(value, obj): if obj.floor == 1 and obj.topic.updated_on and not obj.parent: return u'<div class="updated">由 %s 于 %s 更新</div>' % (obj.topic.modified_user.username, timesince(obj.topic.updated_on)) fields = ['topic', 'id', 'username', 'userimage', 'posted_by', 'content', 'created_on', 'actions', 'floor', 'updated', 'parent', ] fields_convert_map = {'created_on':created_on, 'content':content, 'username':username, 'userimage':userimage, 'actions':actions, 'updated':updated} #view1 为生成一级回复,即回复主题 view1 = ListView(Post, fields=fields, condition=condition1, order_by=order_by, rows_per_page=rows_per_page, pageno=pageno, fields_convert_map=fields_convert_map) #view2 为生成二级乃至多级回复 view2 = ListView(Post, fields=fields, condition=condition2, order_by=order_by, pagination=False, fields_convert_map=fields_convert_map) key = '__topicvisited__:forumtopic:%s:%s:%s' % (request.remote_addr, forum_id, topic_id) cache = function('get_cache')() v = cache.get(key, None) if not v: Topic.filter(Topic.c.id==int(topic_id)).update(num_views=Topic.c.num_views+1) cache.set(key, 1, settings.get_var('PARA/FORUM_USER_VISITED_TIMEOUT')) slug = uuid.uuid1().hex #处理posts和sub_posts query = view1.query() posts = [] sub_posts = {} def process_sub(ids): _ids = [] for x in Post.filter(Post.c.parent.in_(ids)).order_by(Post.c.floor): obj = view2.object(x) d = sub_posts.setdefault(str(x._parent_), []) d.append(obj) _ids.append(x.id) if _ids: process_sub(_ids) ids = [] for row in query: posts.append(view1.object(row)) ids.append(row.id) process_sub(ids) pagination = functions.create_pagination(request.path+'?'+request.query_string, view1.total, pageno+1, rows_per_page) return {'forum':forum, 'topic':topic, 'slug':slug, 'has_email':bool(request.user and request.user.email), 'page':pageno+1, 'pagination':pagination, 'posts':posts, 'sub_posts':sub_posts}
def forum_index(self, id): """ 显示某论坛页面 """ from uliweb.utils.generic import ListView from sqlalchemy.sql import and_ import math pageno = int(request.values.get('page', 1)) - 1 rows_per_page=int(request.values.get('rows', settings.get_var('PARA/FORUM_INDEX_NUMS'))) Topic = get_model('forumtopic') Forum = get_model('forum') User = get_model('user') forum = Forum.get(int(id)) condition = Topic.c.forum == int(id) order_by = [Topic.c.sticky.desc(), Topic.c.last_reply_on.desc()] if not forum.managers.has(request.user): condition = (Topic.c.hidden==False) & condition filter = request.GET.get('filter', 'all') if filter == 'essence': condition = (Topic.c.essence==True) & condition elif filter == 'sticky': condition = (Topic.c.sticky==True) & condition term = request.GET.get('term', '') type = request.GET.get('type', '1') if term: if type == '1': #查找主题 condition = (Topic.c.subject.like('%'+term+'%')) & condition elif type == '2': #查找用户名 condition = and_(Topic.c.posted_by == User.c.id, User.c.username.like('%' + term + '%') | User.c.nickname.like('%' + term + '%'), ) & condition def created_on(value, obj): return value.strftime('%Y-%m-%d') def last_reply_on(value, obj): return timesince(value) def subject(value, obj): import cgi if obj.topic_type: _type = u'[%s]' % obj.get_display_value('topic_type') else: _type = '' s = '' if obj.sticky: s += u'<font color="red">[顶]</font>' if obj.hidden: s += u'<font color="red">[隐]</font>' if obj.closed: s += u'<font color="red">[关]</font>' if obj.essence: s += u'<font color="red">[精]</font>' if obj.homepage: s += u'<font color="red">[首]</font>' return _type+ '<a href="/%d/%d">%s</a>' % (int(id), obj.id, cgi.escape(obj.subject)) + s fields_convert_map = {'created_on':created_on, 'subject':subject, 'last_reply_on':last_reply_on} view = ListView(Topic, condition=condition, order_by=order_by, rows_per_page=rows_per_page, pageno=pageno, fields_convert_map=fields_convert_map) view.query() #in order to get the total count objects = view.objects() pages = int(math.ceil(1.0*view.total/rows_per_page)) return {'forum':forum, 'objects':objects, 'filter':filter, 'term':term, 'page':pageno+1, 'total':view.total, 'pages':pages, 'pagination':functions.create_pagination(request.path+'?'+request.query_string, view.total, pageno+1, rows_per_page), 'type':type, 'filter_name':dict(settings.get_var('PARA/FILTERS')).get(filter)}
def list(self): from uliweb import request from uliweb.utils.generic import ListView from uliweb.utils.common import get_choice import math pageno = int(request.values.get('page', 1)) - 1 rows_per_page = int( request.values.get('rows', settings.get_var('MESSAGES/PAGE_NUMS', 10))) read_flag = request.GET.get('read', '') type_flag = request.GET.get('type', '') condition = None condition = (self.model.c.user == request.user.id) & condition condition = (self.model.c.send_flag == 'r') & condition if read_flag: condition = (self.model.c.read_flag == bool(read_flag == '1')) & condition if type_flag: condition = (self.model.c.type == type_flag) & condition def create_date(value, obj): from uliweb.utils.timesince import timesince return timesince(value) def user_image(value, obj): return functions.get_user_image(obj.sender, size=20) def message(value, obj): return value fields_convert_map = { 'create_date': create_date, 'user_image': user_image, 'message': message } view = ListView(self.model, condition=condition, order_by=[self.model.c.create_date.desc()], rows_per_page=rows_per_page, pageno=pageno, fields_convert_map=fields_convert_map) view.query() result = {} result['read_flag'] = read_flag result['type_flag'] = type_flag result['message_type_name'] = get_choice( settings.get_var('MESSAGES/MESSAGE_TYPE'), type_flag, '全部类型') pages = int(math.ceil(1.0 * view.total / rows_per_page)) # result['page'] = pageno+1 # result['total'] = view.total # result['pages'] = pages result['pagination'] = functions.create_pagination( request.url, view.total, pageno + 1, rows_per_page) result['objects'] = list(view.objects()) ids = [] for row in result['objects']: ids.append(row._obj_.id) self.model.filter(self.model.c.id.in_(ids)).update(read_flag=True) _del_key(request.user.id) return result
def topic_view(self, forum_id, topic_id): """ 显示某主题页面 """ from uliweb.utils.generic import ListView from uliweb import settings pageno = int(request.values.get("page", 1)) - 1 rows_per_page = int(request.values.get("rows", settings.get_var("PARA/FORUM_PAGE_NUMS"))) Post = get_model("forumpost") Topic = get_model("forumtopic") Forum = get_model("forum") forum = Forum.get(int(forum_id)) condition = Post.c.topic == int(topic_id) condition1 = (Post.c.parent == None) & condition condition2 = (Post.c.parent != None) & condition order_by = [Post.c.floor] def created_on(value, obj): return date.to_local(value).strftime("%Y-%m-%d %H:%M:%S %Z") def content(value, obj): if obj.deleted: return u'<div class="deleted">内容已经被 %s 于时间 %s 删除 </div>' % ( obj.deleted_by.username, obj.field_str(obj.deleted_on), ) else: return value def username(value, obj): return obj.posted_by.username def userimage(value, obj): get_user_image = function("get_user_image") url = get_user_image(obj.posted_by) return url def actions(value, obj): if not request.user: return "" a = [] is_manager = forum.managers.has(request.user) if obj.floor == 1 and obj.parent == None: # 第一楼为主贴,可以允许关闭,顶置等操作 if is_manager: a.append( '<a href="#" rel="%d" class="close">%s</a>' % (obj.id, self.status["close"][obj.topic.closed]) ) a.append( '<a href="#" rel="%d" class="hidden">%s</a>' % (obj.id, self.status["hidden"][obj.topic.hidden]) ) a.append( '<a href="#" rel="%d" class="top">%s</a>' % (obj.id, self.status["sticky"][obj.topic.sticky]) ) a.append( '<a href="#" rel="%d" class="essence">%s</a>' % (obj.id, self.status["essence"][obj.topic.essence]) ) a.append( '<a href="#" rel="%d" class="homepage">%s</a>' % (obj.id, self.status["homepage"][obj.topic.homepage]) ) if is_manager or ( obj.posted_by.id == request.user.id and obj.created_on + timedelta(days=settings.get_var("PARA/FORUM_EDIT_DELAY")) >= date.now() ): # 作者或管理员且在n天之内,则可以编辑 url = url_for(ForumView.edit_topic, forum_id=forum_id, topic_id=topic_id) a.append('<a href="%s" rel="%d" class="edit">编辑</a>' % (url, obj.id)) if is_manager: url = url_for(ForumView.remove_topic, forum_id=forum_id, topic_id=topic_id) a.append('<a href="%s" rel="%d" class="delete_topic">删除主题</a>' % (url, obj.id)) if is_manager or (obj.posted_by.id == request.user.id): if (obj.deleted and (obj.deleted_by.id == request.user.id or is_manager)) or not obj.deleted: a.append( '<a href="#" rel="%d" class="delete">%s</a>' % (obj.id, self.status["delete"][obj.deleted]) ) if obj.posted_by.id == request.user.id: a.append('<a href="#" rel="%d" class="email">%s</a>' % (obj.id, self.status["email"][obj.reply_email])) a.append('<a href="/forum/%d/%d/%d/new_reply">回复该作者</a>' % (forum_id, topic_id, obj.id)) return " | ".join(a) def updated(value, obj): if obj.floor == 1 and obj.topic.updated_on and not obj.parent: return u'<div class="updated">由 %s 于 %s 更新</div>' % ( obj.topic.modified_user.username, timesince(obj.topic.updated_on), ) fields = [ "topic", "id", "username", "userimage", "posted_by", "content", "created_on", "actions", "floor", "updated", "parent", ] fields_convert_map = { "created_on": created_on, "content": content, "username": username, "userimage": userimage, "actions": actions, "updated": updated, } # view1 为生成一级回复,即回复主题 view1 = ListView( Post, fields=fields, condition=condition1, order_by=order_by, rows_per_page=rows_per_page, pageno=pageno, fields_convert_map=fields_convert_map, ) # view2 为生成二级乃至多级回复 view2 = ListView( Post, fields=fields, condition=condition2, order_by=order_by, pagination=False, fields_convert_map=fields_convert_map, ) key = "__topicvisited__:forumtopic:%s:%s:%s" % (request.remote_addr, forum_id, topic_id) cache = function("get_cache")() v = cache.get(key, None) if not v: Topic.filter(Topic.c.id == int(topic_id)).update(num_views=Topic.c.num_views + 1) cache.set(key, 1, settings.get_var("PARA/FORUM_USER_VISITED_TIMEOUT")) slug = uuid.uuid1().hex topic = Topic.get(int(topic_id)) # 处理posts和sub_posts query = view1.query() posts = [] sub_posts = {} def process_sub(ids): _ids = [] for x in Post.filter(Post.c.parent.in_(ids)).order_by(Post.c.floor): obj = view2.object(x) d = sub_posts.setdefault(str(x._parent_), []) d.append(obj) _ids.append(x.id) if _ids: process_sub(_ids) ids = [] for row in query: posts.append(view1.object(row)) ids.append(row.id) process_sub(ids) pagination = functions.create_pagination(request.url, view1.total, pageno + 1, rows_per_page) return { "forum": forum, "topic": topic, "slug": slug, "has_email": bool(request.user and request.user.email), "page": pageno + 1, "pagination": pagination, "posts": posts, "sub_posts": sub_posts, }
def forum_index(self, id): """ 显示某论坛页面 """ from uliweb.utils.generic import ListView from sqlalchemy.sql import and_ import math pageno = int(request.values.get("page", 1)) - 1 rows_per_page = int(request.values.get("rows", settings.get_var("PARA/FORUM_INDEX_NUMS"))) Topic = get_model("forumtopic") Forum = get_model("forum") User = get_model("user") forum = Forum.get(int(id)) condition = Topic.c.forum == int(id) order_by = [Topic.c.sticky.desc(), Topic.c.last_reply_on.desc()] if not forum.managers.has(request.user): condition = (Topic.c.hidden == False) & condition filter = request.GET.get("filter", "all") if filter == "essence": condition = (Topic.c.essence == True) & condition elif filter == "sticky": condition = (Topic.c.sticky == True) & condition term = request.GET.get("term", "") type = request.GET.get("type", "1") if term: if type == "1": # 查找主题 condition = (Topic.c.subject.like("%" + term + "%")) & condition elif type == "2": # 查找用户名 condition = ( and_( Topic.c.posted_by == User.c.id, User.c.username.like("%" + term + "%") | User.c.nickname.like("%" + term + "%"), ) & condition ) def created_on(value, obj): return value.strftime("%Y-%m-%d") def last_reply_on(value, obj): return timesince(value) def subject(value, obj): if obj.topic_type: _type = u"[%s]" % obj.get_display_value("topic_type") else: _type = "" s = "" if obj.sticky: s += u'<font color="red">[顶]</font>' if obj.hidden: s += u'<font color="red">[隐]</font>' if obj.closed: s += u'<font color="red">[关]</font>' if obj.essence: s += u'<font color="red">[精]</font>' if obj.homepage: s += u'<font color="red">[首]</font>' return _type + '<a href="/forum/%d/%d">%s</a>' % (int(id), obj.id, obj.subject) + s fields_convert_map = {"created_on": created_on, "subject": subject, "last_reply_on": last_reply_on} view = ListView( Topic, condition=condition, order_by=order_by, rows_per_page=rows_per_page, pageno=pageno, fields_convert_map=fields_convert_map, ) view.query() # in order to get the total count objects = view.objects() pages = int(math.ceil(1.0 * view.total / rows_per_page)) return { "forum": forum, "objects": objects, "filter": filter, "term": term, "page": pageno + 1, "total": view.total, "pages": pages, "pagination": functions.create_pagination(request.url, view.total, pageno + 1, rows_per_page), "type": type, "filter_name": dict(settings.get_var("PARA/FILTERS")).get(filter), }
def topic_view(self, forum_id, topic_id): """ 显示某主题页面 """ from uliweb.utils.generic import ListView from uliweb import settings pageno = int(request.values.get('page', 1)) - 1 rows_per_page = int( request.values.get('rows', settings.get_var('PARA/FORUM_PAGE_NUMS'))) Post = get_model('forumpost') Topic = get_model('forumtopic') Forum = get_model('forum') forum = Forum.get(int(forum_id)) condition = Post.c.topic == int(topic_id) condition1 = (Post.c.parent == None) & condition condition2 = (Post.c.parent != None) & condition order_by = [Post.c.floor] def created_on(value, obj): return date.to_local(value).strftime('%Y-%m-%d %H:%M:%S %Z') def content(value, obj): if obj.deleted: return u'<div class="deleted">内容已经被 %s 于时间 %s 删除 </div>' % ( obj.deleted_by.username, obj.field_str(obj.deleted_on)) else: return value def username(value, obj): return obj.posted_by.username def userimage(value, obj): get_user_image = function('get_user_image') url = get_user_image(obj.posted_by) return url def actions(value, obj): if not request.user: return '' a = [] is_manager = forum.managers.has(request.user) if obj.floor == 1 and obj.parent == None: #第一楼为主贴,可以允许关闭,顶置等操作 if is_manager: a.append('<a href="#" rel="%d" class="close">%s</a>' % (obj.id, self.status['close'][obj.topic.closed])) a.append('<a href="#" rel="%d" class="hidden">%s</a>' % (obj.id, self.status['hidden'][obj.topic.hidden])) a.append('<a href="#" rel="%d" class="top">%s</a>' % (obj.id, self.status['sticky'][obj.topic.sticky])) a.append( '<a href="#" rel="%d" class="essence">%s</a>' % (obj.id, self.status['essence'][obj.topic.essence])) a.append( '<a href="#" rel="%d" class="homepage">%s</a>' % (obj.id, self.status['homepage'][obj.topic.homepage])) if is_manager or ( obj.posted_by.id == request.user.id and obj.created_on + timedelta(days=settings.get_var( 'PARA/FORUM_EDIT_DELAY')) >= date.now()): #作者或管理员且在n天之内,则可以编辑 url = url_for(ForumView.edit_topic, forum_id=forum_id, topic_id=topic_id) a.append('<a href="%s" rel="%d" class="edit">编辑</a>' % (url, obj.id)) if is_manager: url = url_for(ForumView.remove_topic, forum_id=forum_id, topic_id=topic_id) a.append( '<a href="%s" rel="%d" class="delete_topic">删除主题</a>' % (url, obj.id)) if is_manager or (obj.posted_by.id == request.user.id): if (obj.deleted and (obj.deleted_by.id == request.user.id or is_manager)) or not obj.deleted: a.append('<a href="#" rel="%d" class="delete">%s</a>' % (obj.id, self.status['delete'][obj.deleted])) if obj.posted_by.id == request.user.id: a.append('<a href="#" rel="%d" class="email">%s</a>' % (obj.id, self.status['email'][obj.reply_email])) a.append('<a href="/forum/%d/%d/%d/new_reply">回复该作者</a>' % (forum_id, topic_id, obj.id)) return ' | '.join(a) def updated(value, obj): if obj.floor == 1 and obj.topic.updated_on and not obj.parent: return u'<div class="updated">由 %s 于 %s 更新</div>' % ( obj.topic.modified_user.username, timesince(obj.topic.updated_on)) fields = [ 'topic', 'id', 'username', 'userimage', 'posted_by', 'content', 'created_on', 'actions', 'floor', 'updated', 'parent', ] fields_convert_map = { 'created_on': created_on, 'content': content, 'username': username, 'userimage': userimage, 'actions': actions, 'updated': updated } #view1 为生成一级回复,即回复主题 view1 = ListView(Post, fields=fields, condition=condition1, order_by=order_by, rows_per_page=rows_per_page, pageno=pageno, fields_convert_map=fields_convert_map) #view2 为生成二级乃至多级回复 view2 = ListView(Post, fields=fields, condition=condition2, order_by=order_by, pagination=False, fields_convert_map=fields_convert_map) key = '__topicvisited__:forumtopic:%s:%s:%s' % (request.remote_addr, forum_id, topic_id) cache = function('get_cache')() v = cache.get(key, None) if not v: Topic.filter(Topic.c.id == int(topic_id)).update( num_views=Topic.c.num_views + 1) cache.set(key, 1, settings.get_var('PARA/FORUM_USER_VISITED_TIMEOUT')) slug = uuid.uuid1().hex topic = Topic.get(int(topic_id)) #处理posts和sub_posts query = view1.query() posts = [] sub_posts = {} def process_sub(ids): _ids = [] for x in Post.filter(Post.c.parent.in_(ids)).order_by( Post.c.floor): obj = view2.object(x) d = sub_posts.setdefault(str(x._parent_), []) d.append(obj) _ids.append(x.id) if _ids: process_sub(_ids) ids = [] for row in query: posts.append(view1.object(row)) ids.append(row.id) process_sub(ids) pagination = functions.create_pagination(request.url, view1.total, pageno + 1, rows_per_page) return { 'forum': forum, 'topic': topic, 'slug': slug, 'has_email': bool(request.user and request.user.email), 'page': pageno + 1, 'pagination': pagination, 'posts': posts, 'sub_posts': sub_posts }
def forum_index(self, id): """ 显示某论坛页面 """ from uliweb.utils.generic import ListView from sqlalchemy.sql import and_ import math pageno = int(request.values.get('page', 1)) - 1 rows_per_page = int( request.values.get('rows', settings.get_var('PARA/FORUM_INDEX_NUMS'))) Topic = get_model('forumtopic') Forum = get_model('forum') User = get_model('user') forum = Forum.get(int(id)) condition = Topic.c.forum == int(id) order_by = [Topic.c.sticky.desc(), Topic.c.last_reply_on.desc()] if not forum.managers.has(request.user): condition = (Topic.c.hidden == False) & condition filter = request.GET.get('filter', 'all') if filter == 'essence': condition = (Topic.c.essence == True) & condition elif filter == 'sticky': condition = (Topic.c.sticky == True) & condition term = request.GET.get('term', '') type = request.GET.get('type', '1') if term: if type == '1': #查找主题 condition = (Topic.c.subject.like('%' + term + '%')) & condition elif type == '2': #查找用户名 condition = and_( Topic.c.posted_by == User.c.id, User.c.username.like('%' + term + '%') | User.c.nickname.like('%' + term + '%'), ) & condition def created_on(value, obj): return value.strftime('%Y-%m-%d') def last_reply_on(value, obj): return timesince(value) def subject(value, obj): if obj.topic_type: _type = u'[%s]' % obj.get_display_value('topic_type') else: _type = '' s = '' if obj.sticky: s += u'<font color="red">[顶]</font>' if obj.hidden: s += u'<font color="red">[隐]</font>' if obj.closed: s += u'<font color="red">[关]</font>' if obj.essence: s += u'<font color="red">[精]</font>' if obj.homepage: s += u'<font color="red">[首]</font>' return _type + '<a href="/forum/%d/%d">%s</a>' % (int(id), obj.id, obj.subject) + s fields_convert_map = { 'created_on': created_on, 'subject': subject, 'last_reply_on': last_reply_on } view = ListView(Topic, condition=condition, order_by=order_by, rows_per_page=rows_per_page, pageno=pageno, fields_convert_map=fields_convert_map) view.query() #in order to get the total count objects = view.objects() pages = int(math.ceil(1.0 * view.total / rows_per_page)) return { 'forum': forum, 'objects': objects, 'filter': filter, 'term': term, 'page': pageno + 1, 'total': view.total, 'pages': pages, 'pagination': functions.create_pagination(request.url, view.total, pageno + 1, rows_per_page), 'type': type, 'filter_name': dict(settings.get_var('PARA/FILTERS')).get(filter) }