def add(self): from uliweb.utils.generic import AddView view = AddView(self.model, url_for(BlogView.list), default_data={'author': request.user.id}) return view.run()
def add(self): """ 添加课程 """ from uliweb.utils.generic import AddView def get_url(id): return url_for(self.__class__.index) def get_form_field(name): from uliweb.form import SelectField if name == 'teachers': return SelectField('教师', multiple=True, help_string='添加除你之外的教师', datatype=int) def pre_save(data): if request.user.id not in data['teachers']: data['teachers'].append(request.user.id) view = AddView(self.model, ok_url=get_url, get_form_field=get_form_field, pre_save=pre_save, form_args={'title': '添加新课程'}) return view.run()
def add_paragraph_comment(self, cid): """ 添加某个段落的评论 """ from uliweb.utils.generic import AddView def post_save(obj, data): t = obj.chapter.tutorial t.last_comment_user = request.user.id t.last_comment_date = date.now() t.comments_count += 1 t.save() obj.chapter.comments_count += 1 obj.chapter.save() default_data = { 'chapter': int(cid), 'anchor': int(request.GET.get('para')) } view = AddView(self.model_comments, success_data=self._get_comment_data, default_data=default_data, post_save=post_save) return view.run(json_result=True)
def add(self): """ 添加新教程 """ from uliweb.utils.generic import AddView def get_url(**kwargs): return url_for(TutorialView.read, **kwargs) def pre_save(data): data['creator'] = request.user.id if request.user.id not in data['authors']: data['authors'].append(request.user.id) data['modified_date'] = date.now() data['modified_user'] = request.user.id def post_created_form(fcls, model): fcls.authors.html_attrs['url'] = '/users/search' fcls.authors.choices = [('', '')] view = AddView(self.model, ok_url=get_url, pre_save=pre_save, post_created_form=post_created_form) return view.run()
def add(self): """ Add new role """ from uliweb.utils.generic import AddView from functools import partial url = partial(url_for, RoleView.view) default_data = {'reverse': False} fields = [ { 'name': 'name', 'verbose_name': _('Name') }, { 'name': 'description', 'verbose_name': _('Description') }, ] view = AddView(self.model, url, default_data=default_data, fields=fields) return view.run()
def generic_add(model=None, get_url=get_url, layout=None, template=None, title=None, view=None, data=None): from uliweb.utils.generic import AddView from uliweb import response if not view: Model = get_model(model) if not model or not Model: return error("Can't find model [%s], please check it" % model) get_url = partial(get_url, action='view') view = AddView(model, get_url) result = view.run() if isinstance(result, dict): layout = layout or 'layout.html' template = template or 'generic_add.html' response.template = template if not title: name = getattr(model, '__verbose_name__', '') title = _("Add") + safe_unicode(name) elif callable(title): title = title('add') data = data or {} result['layout'] = layout result['get_url'] = get_url result['title'] = title result.update(data) return result
def generic_add(model=None, get_url=get_url, layout='layout.html', template=None, title=None, view=None, data=None): from uliweb.utils.generic import AddView from uliweb import response if not view: Model = get_model(model) if not model or not Model: return error("Can't find model [%s], please check it" % model) get_url = partial(get_url, action='view') view = AddView(model, get_url) result = view.run() if isinstance(result, dict): template = template or 'generic_add.html' response.template = template if not title: name = getattr(model, '__verbose_name__', '') title = _("Add") + safe_unicode(name) elif callable(title): title = title('add') data = data or {} result['get_url'] = get_url result['title'] = title result.update(data) return render_template(template, layout, result) else: return result
def new_topic(self, id): """ 发表新主题 """ from uliweb.utils.generic import AddView type = request.GET.get('type', '') Forum = get_model('forum') forum = Forum.get(int(id)) if forum.manager_only and not forum.managers.has(request.user.id): error('本论坛被设置为只有管理员才可以发贴!') def post_save(obj, data): from sqlalchemy.sql import select, func Post = get_model('forumpost') p = Post(topic=obj.id, posted_by=request.user, slug=obj.slug, content=data['content'], floor=1, reply_email=data['reply_email']) p.save() obj.last_post = p.id obj.save() Forum.filter(Forum.c.id==int(id)).update(num_posts=Forum.c.num_posts+1, num_topics=Forum.c.num_topics+1, last_post_user=request.user.id, last_reply_on=date.now(), last_post=p.id) #根据slug的值清除附件中无效的文件 self._clear_files(obj.slug, data['content']) def get_form_field(name): from uliweb.utils.generic import ReferenceSelectField from uliweb.form import TextField, BooleanField forumtopictype = get_model('forumtopictype') if name == 'content': return TextField('内容', required=True, convert_html=True) elif name == 'topic_type': if type: return ReferenceSelectField('forumtopictype', condition=forumtopictype.c.forum==forum.id, label='主题分类名称', default=type) else: return ReferenceSelectField('forumtopictype', condition=forumtopictype.c.forum==forum.id, label='主题分类名称') elif name == 'reply_email': return BooleanField('有回复时邮件通知我') slug = uuid.uuid1().hex data = {'slug':slug, 'reply_email':False, 'enable_comment':True} has_email = bool(request.user and request.user.email) view = AddView('forumtopic', url_for(ForumView.forum_index, id=int(id)), default_data={'forum':int(id), 'last_post_user':request.user.id, 'last_reply_on':date.now()}, hidden_fields=['slug'], data=data, post_save=post_save, get_form_field=get_form_field, template_data={'forum':forum, 'has_email':has_email, 'slug':slug}) return view.run()
def forum_add(self): """ 添加新的论坛 """ from uliweb.utils.generic import AddView view = AddView("forum", url_for(ForumView.admin_forum)) return view.run()
def add(self): from uliweb.utils.generic import AddView def get_url(id): return url_for(BlogView.view, id=id) view = AddView(self.model, ok_url=get_url) return view.run()
def forum_add(self): """ 添加新的论坛 """ from uliweb.utils.generic import AddView view = AddView('forum', url_for(ForumView.admin_forum)) return view.run()
def category_add(self): """ 添加新的板块 """ from uliweb.utils.generic import AddView view = AddView('forumcategory', url_for(ForumView.admin_category)) return view.run()
def forumtopictype_add(self): """ 添加新的论坛 """ from uliweb.utils.generic import AddView view = AddView('forumtopictype', url_for(ForumView.admin_forumtopictype)) return view.run()
def forumtopictype_add(self): """ 添加新的论坛 """ from uliweb.utils.generic import AddView forum_id = int(request.POST.get("forum_id")) view = AddView("forumtopictype", success_data=True, default_data={"forum": forum_id}) return view.run(json_result=True)
def new_topic(self, id): """ 发表新主题 """ from uliweb.utils.generic import AddView import uuid Forum = get_model("forum") forum = Forum.get(int(id)) def post_save(obj, data): from sqlalchemy.sql import select, func Post = get_model("forumpost") p = Post(topic=obj.id, posted_by=request.user, content=data["content"], floor=1) p.save() Forum.filter(Forum.c.id == int(id)).update( num_posts=Forum.c.num_posts + 1, num_topics=Forum.c.num_topics + 1, last_post_user=request.user.id, last_reply_on=date.now(), ) # 根据slug的值清除附件中无效的文件 self._clear_files(obj.slug, data["content"]) def get_form_field(name): from uliweb.utils.generic import ReferenceSelectField from uliweb.form import TextField forumtopictype = get_model("forumtopictype") if name == "content": return TextField("内容", required=True, rows=20, convert_html=True) if name == "topic_type": return ReferenceSelectField( "forumtopictype", condition=forumtopictype.c.forum == forum.id, label="主题分类名称" ) slug = uuid.uuid1().hex data = {"slug": slug} view = AddView( "forumtopic", url_for(ForumView.forum_index, id=int(id)), default_data={"forum": int(id), "last_post_user": request.user.id, "last_reply_on": date.now()}, hidden_fields=["slug"], data=data, post_save=post_save, get_form_field=get_form_field, template_data={"forum": forum}, ) return view.run()
def forumtopictype_add(self): """ 添加新的论坛 """ from uliweb.utils.generic import AddView forum_id = int(request.POST.get('forum_id')) view = AddView('forumtopictype', success_data=True, default_data={'forum': forum_id}) return view.run(json_result=True)
def add_chapter(self, t_id): """ 増加章节, t_id为教程id """ from uliweb.utils.generic import AddView from sqlalchemy.sql import func obj = self.model.get_or_notfound(int(t_id)) if not self._can_edit_tutorial(obj): flash("你无权添加新章节", 'error') # return redirect(url_for(TutorialView.read, id=t_id)) Redirect(url_for(TutorialView.read, id=t_id)) def get_url(**kwargs): return url_for(TutorialView.view_chapter, **kwargs) def pre_save(data): if 'parent' in request.GET: data['parent'] = int(request.GET.get('parent')) data['tutorial'] = int(t_id) order, = self.model_chapters.filter( self.model_chapters.c.tutorial == int(t_id)).values_one( func.max(self.model_chapters.c.order)) data['order'] = order + 1 if order > 0 else 1 # data['content'] = self._prepare_content(data['content'], data['render']) data['html'] = self._get_chapter_html(data['content'], data['format'], data['render'], data['scrollable']) data['chars_count'] = len(data['content']) data['modified_date'] = date.now() data['modified_user'] = request.user.id data['format'] = '2' def post_save(obj, data): obj.tutorial.modified_date = date.now() obj.tutorial.modified_user = request.user.id obj.tutorial.save() #删除章节顺序cache cache = functions.get_cache() cache.delete(self._get_tutorial_chapters_cache_key(int(t_id))) template_data = {'object': obj} data = {'scrollable': True} view = AddView(self.model_chapters, ok_url=get_url, pre_save=pre_save, template_data=template_data, post_save=post_save, data=data) return view.run()
def add(self): from uliweb.utils.generic import AddView from uliweb.orm import get_model from forms import AddUserForm def post_save(obj, data): obj.set_password(settings.USER_ADMIN.DEFAULT_PASSWORD) if request.user.is_superuser: view = AddView('user', get_url('view'), post_save=post_save, form_cls=AddUserForm) return view.run() else: flash(_('You have no previlege to create user.'), 'error') return redirect(url_for(config_users_list))
def add(self): """ Add new role """ from uliweb.utils.generic import AddView default_data = {'reverse':False} fields = [ {'name':'name', 'verbose_name':_('Name')}, {'name':'description', 'verbose_name':_('Description')}, ] view = AddView(self.model, url_for(self.__class__.view, _format=True), default_data=default_data, fields=fields) return view.run()
def add(self): from uliweb.utils.generic import AddView from uliweb.orm import get_model from forms import AddUserForm from functools import partial def post_save(obj, data): obj.set_password(settings.USER_ADMIN.DEFAULT_PASSWORD) if request.user.is_superuser: view = AddView('user', partial(get_url, 'view'), post_save=post_save, form_cls=AddUserForm) return view.run() else: flash(_('You have no previlege to create user.'), 'error') return redirect(url_for(config_users_list))
def add(self): from uliweb.utils.generic import AddView helper = ApproveHelper() def pre_save(data): data["submitter"] = request.user.id data["submitter_date"] = datetime.datetime.now() def post_save(obj, data): helper.bind(obj) helper.create_workflow() view = AddView(self.model, url_for(self.__class__.list), pre_save=pre_save, post_save=post_save) result = view.run() return result
def add_chapter(self, t_id): """ 増加章节, t_id为教程id """ from uliweb.utils.generic import AddView from sqlalchemy.sql import func obj = self.model.get_or_notfound(int(t_id)) if not self._can_edit_tutorial(obj): flash("你无权添加新章节", 'error') # return redirect(url_for(TutorialView.read, id=t_id)) Redirect(url_for(TutorialView.read, id=t_id)) def get_url(**kwargs): return url_for(TutorialView.view_chapter, **kwargs) def pre_save(data): if 'parent' in request.GET: data['parent'] = int(request.GET.get('parent')) data['tutorial'] = int(t_id) order, = self.model_chapters.filter(self.model_chapters.c.tutorial==int(t_id)).values_one(func.max(self.model_chapters.c.order)) data['order'] = order+1 if order>0 else 1 # data['content'] = self._prepare_content(data['content'], data['render']) data['html'] = self._get_chapter_html(data['content'], data['format'], data['render'], data['scrollable']) data['chars_count'] = len(data['content']) data['modified_date'] = date.now() data['modified_user'] = request.user.id data['format'] = '2' def post_save(obj, data): obj.tutorial.modified_date = date.now() obj.tutorial.modified_user = request.user.id obj.tutorial.save(); #删除章节顺序cache cache = functions.get_cache() cache.delete(self._get_tutorial_chapters_cache_key(int(t_id))) template_data = {'object':obj} data = {'scrollable':True} view = AddView(self.model_chapters, ok_url=get_url, pre_save=pre_save, template_data=template_data, post_save=post_save, data=data) return view.run()
def add(self): """ Add new permission """ from uliweb.utils.generic import AddView from functools import partial url = partial(url_for, PermissionView.view) default_data = {'reverse':False} fields = [ {'name':'name', 'verbose_name':_('Name')}, {'name':'description', 'verbose_name':_('Description')}, ] view = AddView(self.model, url, default_data=default_data, fields=fields) return view.run()
def add_paragraph_comment(self, cid): """ 添加某个段落的评论 """ from uliweb.utils.generic import AddView def post_save(obj, data): t = obj.chapter.tutorial t.last_comment_user = request.user.id t.last_comment_date = date.now() t.comments_count += 1 t.save() obj.chapter.comments_count += 1 obj.chapter.save() default_data = {'chapter':int(cid), 'anchor':int(request.GET.get('para'))} view = AddView(self.model_comments, success_data=self._get_comment_data, default_data=default_data, post_save=post_save) return view.run(json_result=True)
def admin_forum_add(self): """ 添加新的论坛 """ from uliweb.utils.generic import AddView, get_field_display def post_created_form(fcls, model): fcls.managers.choices = [('', '')] def success_data(obj, data): d = obj.to_dict() d['managers'] = convert_managers(None, obj) return d view = AddView(self.model, success_data=success_data, post_created_form=post_created_form, default_data={'category':request.GET.get('category')}, ) return view.run(json_result=True)
def admin_forum_add(self): """ 添加新的论坛 """ from uliweb.utils.generic import AddView, get_field_display def post_created_form(fcls, model): fcls.managers.choices = [('', '')] def success_data(obj, data): d = obj.to_dict() d['managers'] = convert_managers(None, obj) return d view = AddView( self.model, success_data=success_data, post_created_form=post_created_form, default_data={'category': request.GET.get('category')}, ) return view.run(json_result=True)
def new_post(self, forum_id, topic_id): """ 发表新回复 """ from uliweb.utils.generic import AddView Post = get_model("forumpost") Topic = get_model("forumtopic") Forum = get_model("forum") def pre_save(data): from sqlalchemy.sql import select, func data["topic"] = int(topic_id) data["floor"] = (do_(select([func.max(Post.c.floor)], Post.c.topic == int(topic_id))).scalar() or 0) + 1 def post_save(obj, data): Topic.filter(Topic.c.id == int(topic_id)).update( num_replies=Topic.c.num_replies + 1, last_post_user=request.user.id, last_reply_on=date.now() ) Forum.filter(Forum.c.id == int(forum_id)).update( num_posts=Forum.c.num_posts + 1, last_post_user=request.user.id, last_reply_on=date.now() ) self._clear_files(obj.slug, data["content"]) def get_form_field(name): from uliweb.form import TextField if name == "content": return TextField("内容", required=True, convert_html=True, rows=20) view = AddView( "forumpost", url_for(ForumView.topic_view, forum_id=int(forum_id), topic_id=int(topic_id)), hidden_fields=["slug"], pre_save=pre_save, get_form_field=get_form_field, post_save=post_save, ) return view.run()
def add(self): """ 添加课程 """ from uliweb.utils.generic import AddView def get_url(id): return url_for(self.__class__.index) def get_form_field(name): from uliweb.form import SelectField if name == 'teachers': return SelectField('教师', multiple=True, help_string='添加除你之外的教师', datatype=int) def pre_save(data): if request.user.id not in data['teachers']: data['teachers'].append(request.user.id) view = AddView(self.model, ok_url=get_url, get_form_field=get_form_field, pre_save=pre_save, form_args={'title':'添加新课程'}) return view.run()
def add(self): from uliweb.utils.generic import AddView def pre_save(data): data['submitter'] = request.user.id data['submitter_date'] = datetime.datetime.now() def post_save(obj, data): Workflow = functions.get_workflow() workflow = Workflow.create("YesNoWorkflow", operator=request.user) workflow.ref_unique_id = "yesno,%d" % obj.id workflow.start() workflow.run() obj.workflow = workflow.get_id() obj.save() view = AddView(self.model, url_for(self.__class__.list), pre_save=pre_save, post_save=post_save) result = view.run() return result
def add(self): from uliweb.utils.generic import AddView view = AddView(self.model, url_for(BlogView.list), default_data={'author':request.user.id}) return view.run()
def new_post(self, forum_id, topic_id): """ 发表新回复 """ from uliweb.utils.generic import AddView Post = get_model('forumpost') Topic = get_model('forumtopic') Forum = get_model('forum') User = get_model('user') def pre_save(data): from sqlalchemy.sql import select, func data['topic'] = int(topic_id) data['floor'] = (do_( select([func.max(Post.c.floor)], Post.c.topic == int(topic_id))).scalar() or 0) + 1 def post_save(obj, data): from uliweb import functions from uliweb.utils.common import Serial from uliweb.mail import Mail Topic.filter(Topic.c.id == int(topic_id)).update( num_replies=Topic.c.num_replies + 1, last_post_user=request.user.id, last_reply_on=date.now(), last_post=obj.id) Forum.filter(Forum.c.id == int(forum_id)).update( num_posts=Forum.c.num_posts + 1, last_post_user=request.user.id, last_reply_on=date.now(), last_post=obj.id) self._clear_files(obj.slug, data['content']) #増加发送邮件的处理 emails = [] for u_id in Post.filter((Post.c.topic == int(topic_id)) & (Post.c.reply_email == True) & (Post.c.floor < obj.floor)).values( Post.c.posted_by): user = User.get(u_id[0]) if user and user.email and (user.email not in emails) and ( user.email != request.user.email): emails.append(user.email) if not emails: return _type = settings.get_var('PARA/FORUM_REPLY_PROCESS', 'print') url = '%s/forum/%s/%s' % (settings.get_var('PARA/DOMAIN'), forum_id, topic_id) d = {'url': str(url)} mail = { 'from_': settings.get_var('PARA/EMAIL_SENDER'), 'to_': emails, 'subject': settings.get_var('FORUM_EMAIL/FORUM_EMAIL_TITLE'), 'message': settings.get_var('FORUM_EMAIL/FORUM_EMAIL_TEXT') % d, 'html': True } if _type == 'mail': Mail().send_mail(**mail) elif _type == 'noprint': pass elif _type == 'print': print mail elif _type == 'redis': redis = functions.get_redis() _t = Serial.dump(mail) redis.lpush('send_mails', _t) def get_form_field(name): from uliweb.form import TextField if name == 'content': return TextField('内容', required=True, convert_html=True, rows=20) slug = uuid.uuid1().hex data = {'slug': slug, 'reply_email': False, 'content': ''} def success_data(obj, data): import math return { 'page': int( math.ceil(1.0 * obj.floor / settings.get_var('PARA/FORUM_PAGE_NUMS'))) } view = AddView('forumpost', url_for(ForumView.topic_view, forum_id=int(forum_id), topic_id=int(topic_id)), hidden_fields=['slug'], template_data={'slug': slug}, data=data, success_data=success_data, pre_save=pre_save, get_form_field=get_form_field, post_save=post_save) return view.run(json_result=True)
def new_topic(self, id): """ 发表新主题 """ from uliweb.utils.generic import AddView Forum = get_model('forum') forum = Forum.get(int(id)) def post_save(obj, data): from sqlalchemy.sql import select, func Post = get_model('forumpost') p = Post(topic=obj.id, posted_by=request.user, slug=obj.slug, content=data['content'], floor=1, reply_email=data['reply_email']) p.save() obj.last_post = p.id obj.save() Forum.filter(Forum.c.id == int(id)).update( num_posts=Forum.c.num_posts + 1, num_topics=Forum.c.num_topics + 1, last_post_user=request.user.id, last_reply_on=date.now(), last_post=p.id) #根据slug的值清除附件中无效的文件 self._clear_files(obj.slug, data['content']) def get_form_field(name): from uliweb.utils.generic import ReferenceSelectField from uliweb.form import TextField, BooleanField forumtopictype = get_model('forumtopictype') if name == 'content': return TextField('内容', required=True, convert_html=True) elif name == 'topic_type': return ReferenceSelectField( 'forumtopictype', condition=forumtopictype.c.forum == forum.id, label='主题分类名称') elif name == 'reply_email': return BooleanField('有回复时邮件通知我') slug = uuid.uuid1().hex data = {'slug': slug, 'reply_email': False} has_email = bool(request.user and request.user.email) view = AddView('forumtopic', url_for(ForumView.forum_index, id=int(id)), default_data={ 'forum': int(id), 'last_post_user': request.user.id, 'last_reply_on': date.now() }, hidden_fields=['slug'], data=data, post_save=post_save, get_form_field=get_form_field, template_data={ 'forum': forum, 'has_email': has_email, 'slug': slug }) return view.run()
def admin_categories_add(self): from uliweb.utils.generic import AddView view = AddView('forumcategory', success_data=True) return view.run(json_result=True)
def new_reply(self, forum_id, topic_id, parent_id): """ 发表新回复 """ from uliweb.utils.generic import AddView Forum = get_model('forum') forum = Forum.get(int(forum_id)) Topic = get_model('forumtopic') topic = Topic.get(int(topic_id)) Post = get_model('forumpost') post = Post.get(int(parent_id)) User = get_model('user') def pre_save(data): from sqlalchemy.sql import select, func data['topic'] = int(topic_id) data['parent'] = int(parent_id) data['floor'] = (do_( select([func.max(Post.c.floor)], Post.c.parent == post.id)).scalar() or 0) + 1 def post_save(obj, data): from uliweb import functions from uliweb.utils.common import Serial from uliweb.mail import Mail Post.filter(Post.c.id == int(parent_id)).update( num_replies=Post.c.num_replies + 1, last_post_user=request.user.id, last_reply_on=date.now()) self._clear_files(obj.slug, data['content']) Topic.filter(Topic.c.id == int(topic_id)).update( num_replies=Topic.c.num_replies + 1, last_post_user=request.user.id, last_reply_on=date.now(), last_post=obj.id) Forum.filter(Forum.c.id == int(forum_id)).update( num_posts=Forum.c.num_posts + 1, last_post_user=request.user.id, last_reply_on=date.now(), last_post=obj.id) #増加发送邮件的处理 emails = [] for u_id in Post.filter((Post.c.topic == int(topic_id)) & (Post.c.reply_email == True) & (Post.c.id == parent_id)).values( Post.c.posted_by): user = User.get(u_id[0]) if user and user.email and (user.email not in emails) and ( user.email != request.user.email): emails.append(user.email) if not emails: return _type = settings.get_var('PARA/FORUM_REPLY_PROCESS', 'print') url = '%s/forum/%s/%s' % (settings.get_var('PARA/DOMAIN'), forum_id, topic_id) d = {'url': str(url)} mail = { 'from_': settings.get_var('PARA/EMAIL_SENDER'), 'to_': emails, 'subject': settings.get_var('FORUM_EMAIL/FORUM_EMAIL_TITLE'), 'message': settings.get_var('FORUM_EMAIL/FORUM_EMAIL_TEXT') % d, 'html': True } if _type == 'mail': Mail().send_mail(**mail) elif _type == 'print': print mail elif _type == 'redis': redis = functions.get_redis() _t = Serial.dump(mail) redis.lpush('send_mails', _t) def get_form_field(name): from uliweb.form import TextField if name == 'content': return TextField('内容', required=True, convert_html=True, default='') slug = uuid.uuid1().hex data = { 'slug': slug, 'reply_email': False, 'content': _('RE') + ' @' + unicode(post.posted_by) + ': ' } has_email = bool(request.user and request.user.email) view = AddView( 'forumpost', url_for(ForumView.topic_view, forum_id=int(forum_id), topic_id=int(topic_id)), # default_data={'last_post_user':request.user.id, 'last_reply_on':date.now()}, hidden_fields=['slug'], data=data, pre_save=pre_save, get_form_field=get_form_field, post_save=post_save, template_data={ 'forum': forum, 'topic': topic, 'has_email': has_email, 'slug': slug }) return view.run()
def new_reply(self, forum_id, topic_id, parent_id): """ 发表新回复 """ from uliweb.utils.generic import AddView Forum = get_model('forum') forum = Forum.get(int(forum_id)) Topic = get_model('forumtopic') topic = Topic.get(int(topic_id)) Post = get_model('forumpost') post = Post.get(int(parent_id)) User = get_model('user') def pre_save(data): from sqlalchemy.sql import select, func data['topic'] = int(topic_id) data['parent'] = int(parent_id) data['floor'] = (do_(select([func.max(Post.c.floor)], Post.c.parent==post.id)).scalar() or 0) + 1 def post_save(obj, data): from uliweb import functions from uliweb.utils.common import Serial from uliweb.mail import Mail Post.filter(Post.c.id==int(parent_id)).update(num_replies=Post.c.num_replies+1, last_post_user=request.user.id, last_reply_on=date.now()) self._clear_files(obj.slug, data['content']) Topic.filter(Topic.c.id==int(topic_id)).update( num_replies=Topic.c.num_replies+1, last_post_user=request.user.id, last_reply_on=date.now(), last_post=obj.id) Forum.filter(Forum.c.id==int(forum_id)).update( num_posts=Forum.c.num_posts+1, last_post_user=request.user.id, last_reply_on=date.now(), last_post=obj.id) #増加发送邮件的处理 emails = [] for u_id in Post.filter((Post.c.topic==int(topic_id)) & (Post.c.reply_email==True) & (Post.c.id==parent_id)).values(Post.c.posted_by): user = User.get(u_id[0]) if user and user.email and (user.email not in emails) and (user.email!=request.user.email): emails.append(user.email) if not emails: return _type = settings.get_var('PARA/FORUM_REPLY_PROCESS', 'print') url = '%s/%s/%s' % (settings.get_var('PARA/DOMAIN'), forum_id, topic_id) d = {'url':str(url)} mail = {'from_':settings.get_var('PARA/EMAIL_SENDER'), 'to_':emails, 'subject':settings.get_var('FORUM_EMAIL/FORUM_EMAIL_TITLE'), 'message':settings.get_var('FORUM_EMAIL/FORUM_EMAIL_TEXT') % d, 'html':True} if _type == 'mail': Mail().send_mail(**mail) elif _type == 'print': print mail elif _type == 'redis': redis = functions.get_redis() _t = Serial.dump(mail) redis.lpush('send_mails', _t) def get_form_field(name): from uliweb.form import TextField if name == 'content': return TextField('内容',required=True, convert_html=True, default='') slug = uuid.uuid1().hex data = {'slug':slug, 'reply_email':False, 'content':_('RE')+ ' @'+unicode(post.posted_by)+': '} has_email = bool(request.user and request.user.email) view = AddView('forumpost', url_for(ForumView.topic_view, forum_id=int(forum_id), topic_id=int(topic_id)), # default_data={'last_post_user':request.user.id, 'last_reply_on':date.now()}, hidden_fields=['slug'], data=data, pre_save=pre_save, get_form_field=get_form_field, post_save=post_save, template_data={'forum':forum, 'topic':topic, 'has_email':has_email, 'slug':slug}) return view.run()
def new_post(self, forum_id, topic_id): """ 发表新回复 """ from uliweb.utils.generic import AddView Post = get_model('forumpost') Topic = get_model('forumtopic') Forum = get_model('forum') User = get_model('user') def pre_save(data): from sqlalchemy.sql import select, func data['topic'] = int(topic_id) data['floor'] = (do_(select([func.max(Post.c.floor)], Post.c.topic==int(topic_id))).scalar() or 0) + 1 def post_save(obj, data): from uliweb import functions from uliweb.utils.common import Serial from uliweb.mail import Mail Topic.filter(Topic.c.id==int(topic_id)).update( num_replies=Topic.c.num_replies+1, last_post_user=request.user.id, last_reply_on=date.now(), last_post=obj.id) Forum.filter(Forum.c.id==int(forum_id)).update( num_posts=Forum.c.num_posts+1, last_post_user=request.user.id, last_reply_on=date.now(), last_post=obj.id) self._clear_files(obj.slug, data['content']) #増加发送邮件的处理 emails = [] for u_id in Post.filter((Post.c.topic==int(topic_id)) & (Post.c.reply_email==True) & (Post.c.floor<obj.floor)).values(Post.c.posted_by): user = User.get(u_id[0]) if user and user.email and (user.email not in emails) and (user.email!=request.user.email): emails.append(user.email) if not emails: return _type = settings.get_var('PARA/FORUM_REPLY_PROCESS', 'print') url = '%s/%s/%s' % (settings.get_var('PARA/DOMAIN'), forum_id, topic_id) d = {'url':str(url)} mail = {'from_':settings.get_var('PARA/EMAIL_SENDER'), 'to_':emails, 'subject':settings.get_var('FORUM_EMAIL/FORUM_EMAIL_TITLE'), 'message':settings.get_var('FORUM_EMAIL/FORUM_EMAIL_TEXT') % d, 'html':True} if _type == 'mail': Mail().send_mail(**mail) elif _type == 'noprint': pass elif _type == 'print': print mail elif _type == 'redis': redis = functions.get_redis() _t = Serial.dump(mail) redis.lpush('send_mails', _t) def get_form_field(name): from uliweb.form import TextField if name == 'content': return TextField('内容', required=True, convert_html=True, rows=20) slug = uuid.uuid1().hex data = {'slug':slug, 'reply_email':False, 'content':''} def success_data(obj, data): import math return {'page':int(math.ceil(1.0*obj.floor/settings.get_var('PARA/FORUM_PAGE_NUMS')))} view = AddView('forumpost', url_for(ForumView.topic_view, forum_id=int(forum_id), topic_id=int(topic_id)), hidden_fields=['slug'], template_data={'slug':slug}, data=data, success_data=success_data, pre_save=pre_save, get_form_field=get_form_field, post_save=post_save) return view.run(json_result=True)
def new_post(self, forum_id, topic_id): """ 发表新回复 """ from uliweb.utils.generic import AddView Post = get_model("forumpost") Topic = get_model("forumtopic") Forum = get_model("forum") User = get_model("user") def pre_save(data): from sqlalchemy.sql import select, func data["topic"] = int(topic_id) data["floor"] = (do_(select([func.max(Post.c.floor)], Post.c.topic == int(topic_id))).scalar() or 0) + 1 def post_save(obj, data): from uliweb import functions from uliweb.utils.common import Serial from uliweb.mail import Mail Topic.filter(Topic.c.id == int(topic_id)).update( num_replies=Topic.c.num_replies + 1, last_post_user=request.user.id, last_reply_on=date.now(), last_post=obj.id, ) Forum.filter(Forum.c.id == int(forum_id)).update( num_posts=Forum.c.num_posts + 1, last_post_user=request.user.id, last_reply_on=date.now(), last_post=obj.id, ) self._clear_files(obj.slug, data["content"]) # 増加发送邮件的处理 emails = [] for u_id in Post.filter( (Post.c.topic == int(topic_id)) & (Post.c.reply_email == True) & (Post.c.floor < obj.floor) ).values(Post.c.posted_by): user = User.get(u_id[0]) if user and user.email and (user.email not in emails) and (user.email != request.user.email): emails.append(user.email) if not emails: return _type = settings.get_var("PARA/FORUM_REPLY_PROCESS", "print") url = "%s/forum/%s/%s" % (settings.get_var("PARA/DOMAIN"), forum_id, topic_id) d = {"url": str(url)} mail = { "from_": settings.get_var("PARA/EMAIL_SENDER"), "to_": emails, "subject": settings.get_var("FORUM_EMAIL/FORUM_EMAIL_TITLE"), "message": settings.get_var("FORUM_EMAIL/FORUM_EMAIL_TEXT") % d, "html": True, } if _type == "mail": Mail().send_mail(**mail) elif _type == "noprint": pass elif _type == "print": print mail elif _type == "redis": redis = functions.get_redis() _t = Serial.dump(mail) redis.lpush("send_mails", _t) def get_form_field(name): from uliweb.form import TextField if name == "content": return TextField("内容", required=True, convert_html=True, rows=20) slug = uuid.uuid1().hex data = {"slug": slug, "reply_email": False, "content": ""} def success_data(obj, data): import math return {"page": int(math.ceil(1.0 * obj.floor / settings.get_var("PARA/FORUM_PAGE_NUMS")))} view = AddView( "forumpost", url_for(ForumView.topic_view, forum_id=int(forum_id), topic_id=int(topic_id)), hidden_fields=["slug"], template_data={"slug": slug}, data=data, success_data=success_data, pre_save=pre_save, get_form_field=get_form_field, post_save=post_save, ) return view.run(json_result=True)
def new_reply(self, forum_id, topic_id, parent_id): """ 发表新回复 """ from uliweb.utils.generic import AddView Forum = get_model("forum") forum = Forum.get(int(forum_id)) Topic = get_model("forumtopic") topic = Topic.get(int(topic_id)) Post = get_model("forumpost") post = Post.get(int(parent_id)) User = get_model("user") def pre_save(data): from sqlalchemy.sql import select, func data["topic"] = int(topic_id) data["parent"] = int(parent_id) data["floor"] = (do_(select([func.max(Post.c.floor)], Post.c.parent == post.id)).scalar() or 0) + 1 def post_save(obj, data): from uliweb import functions from uliweb.utils.common import Serial from uliweb.mail import Mail Post.filter(Post.c.id == int(parent_id)).update( num_replies=Post.c.num_replies + 1, last_post_user=request.user.id, last_reply_on=date.now() ) self._clear_files(obj.slug, data["content"]) Topic.filter(Topic.c.id == int(topic_id)).update( num_replies=Topic.c.num_replies + 1, last_post_user=request.user.id, last_reply_on=date.now(), last_post=obj.id, ) Forum.filter(Forum.c.id == int(forum_id)).update( num_posts=Forum.c.num_posts + 1, last_post_user=request.user.id, last_reply_on=date.now(), last_post=obj.id, ) # 増加发送邮件的处理 emails = [] for u_id in Post.filter( (Post.c.topic == int(topic_id)) & (Post.c.reply_email == True) & (Post.c.id == parent_id) ).values(Post.c.posted_by): user = User.get(u_id[0]) if user and user.email and (user.email not in emails) and (user.email != request.user.email): emails.append(user.email) if not emails: return _type = settings.get_var("PARA/FORUM_REPLY_PROCESS", "print") url = "%s/forum/%s/%s" % (settings.get_var("PARA/DOMAIN"), forum_id, topic_id) d = {"url": str(url)} mail = { "from_": settings.get_var("PARA/EMAIL_SENDER"), "to_": emails, "subject": settings.get_var("FORUM_EMAIL/FORUM_EMAIL_TITLE"), "message": settings.get_var("FORUM_EMAIL/FORUM_EMAIL_TEXT") % d, "html": True, } if _type == "mail": Mail().send_mail(**mail) elif _type == "print": print mail elif _type == "redis": redis = functions.get_redis() _t = Serial.dump(mail) redis.lpush("send_mails", _t) def get_form_field(name): from uliweb.form import TextField if name == "content": return TextField("内容", required=True, convert_html=True, default="") slug = uuid.uuid1().hex data = {"slug": slug, "reply_email": False, "content": _("RE") + " @" + unicode(post.posted_by) + ": "} has_email = bool(request.user and request.user.email) view = AddView( "forumpost", url_for(ForumView.topic_view, forum_id=int(forum_id), topic_id=int(topic_id)), # default_data={'last_post_user':request.user.id, 'last_reply_on':date.now()}, hidden_fields=["slug"], data=data, pre_save=pre_save, get_form_field=get_form_field, post_save=post_save, template_data={"forum": forum, "topic": topic, "has_email": has_email, "slug": slug}, ) return view.run()
def add(self): from uliweb.utils.generic import AddView view = AddView(self.model, success_data=True) return view.run(json_result=True)