def edit_article_comment_ajax(request): """ Update comment and return updated and rendered data """ comment_id = int(request.matchdict['comment_id']) dbsession = DBSession() comment = dbsession.query(Comment).options(joinedload('user')).options(joinedload('user.roles')).get(comment_id) # passed POST parameters are: 'body', 'name', 'email', 'website', 'date', 'ip', 'xffip' params = { 'body': 'body', 'name': 'display_name', 'email': 'email', 'website': 'website', 'ip': 'ip_address', 'xffip': 'xff_ip_address' } for k, v in params.items(): value = request.POST[k] if value == '': value = None setattr(comment, v, value) comment.set_body(request.POST['body']) comment.is_subscribed = 'is_subscribed' in request.POST comment.published = h.str_to_timestamp(request.POST['date']) dbsession.flush() #comment_user = None #if comment.user is not None: # comment_user = dbsession.query(User).options(joinedload('roles')).get(comment.user) dbsession.expunge(comment) if comment.user is not None: dbsession.expunge(comment.user) for p in comment.user.roles: dbsession.expunge(p) data = {} # without "unicode" or "str" it generates broken HTML # because render() returns webhelpers.html.builder.literal renderer_dict = {'comment': comment} if comment.user is not None: comment._real_email = comment.user.email else: comment._real_email = comment.email if comment._real_email == '': comment._real_email = None data['rendered'] = render('/blog/single_comment.mako', renderer_dict, request) return data
def write_article(request): _ = request.translate c = { 'new_article': True, 'submit_url': route_url('blog_write_article', request), 'errors': {}, 'tags': [] } if request.method == 'GET': a = Article('new-article-shortcut', 'New article title') c['tags'] = [] c['article'] = a c['article_published_str'] = h.timestamp_to_str(a.published) elif request.method == 'POST': article = Article() e = _check_article_fields(article, request) c['errors'].update(e) c['article_published_str'] = request.POST['published'] if 'published' not in request.POST: c['errors']['published'] = _('invalid date and time format') else: # parse value to check structure date_re = re.compile('^([0-9]{4})-([0-9]{2})-([0-9]{2}) ([0-9]{2}):([0-9]{2})$') mo = date_re.match(request.POST['published']) if mo is None: c['errors']['published'] = _('invalid date and time format') else: # we need to convert LOCAL date and time to UTC seconds article.published = h.str_to_timestamp(request.POST['published']) v = [int(x) for x in mo.groups()[0:3]] article.shortcut_date = '{0:04d}/{1:02d}/{2:02d}'.format(*v) dbsession = DBSession() q = dbsession.query(Article).filter(Article.shortcut_date == article.shortcut_date)\ .filter(Article.shortcut == article.shortcut) res = q.first() if res is not None: c['errors']['shortcut'] = _('duplicated shortcut') # tags c['tags'] = [] if 'tags' in request.POST: tags_str = request.POST['tags'] tags = set([s.strip() for s in tags_str.split(',')]) for tag_str in tags: if tag_str == '': continue c['tags'].append(tag_str) if len(c['errors']) == 0: dbsession = DBSession() # save and redirect user = request.user article.user_id = user.id dbsession.add(article) dbsession.flush() # required as we need to obtain article_id article_id = article.id for tag_str in c['tags']: tag = Tag(tag_str, article) dbsession.add(tag) # force update of tags cloud h.get_public_tags_cloud(force_reload=True) return HTTPFound(location=route_url('blog_go_article', request, article_id=article_id)) c['article'] = article else: return HTTPBadRequest() return c
def _update_article(article_id, request): _ = request.translate dbsession = DBSession() article = dbsession.query(Article).get(article_id) if article is None: return HTTPNotFound() # check fields etc e = _check_article_fields(article, request) c = {'errors': {}} c['article'] = article c['errors'].update(e) c['article_published_str'] = request.POST['published'] if 'published' not in request.POST: c['errors']['published'] = _('invalid date and time format') else: # parse value to check structure date_re = re.compile('^([0-9]{4})-([0-9]{2})-([0-9]{2}) ([0-9]{2}):([0-9]{2})$') mo = date_re.match(request.POST['published']) if mo is None: c['errors']['published'] = _('invalid date and time format') else: # we need to convert LOCAL date and time to UTC seconds article.published = h.str_to_timestamp(request.POST['published']) v = [int(x) for x in mo.groups()[0:3]] article.shortcut_date = '{0:04d}/{1:02d}/{2:02d}'.format(*v) dbsession = DBSession() q = dbsession.query(Article).filter(Article.shortcut_date == article.shortcut_date)\ .filter(Article.id != article_id)\ .filter(Article.shortcut == article.shortcut) res = q.first() if res is not None: c['errors']['shortcut'] = _('duplicated shortcut') # tags c['tags'] = [] # these are new tags if 'tags' in request.POST: tags_str = request.POST['tags'] tags = set([s.strip() for s in tags_str.split(',')]) for tag_str in tags: if tag_str == '': continue c['tags'].append(tag_str) if len(c['errors']) == 0: for tag in article.tags: dbsession.delete(tag) for tag_str in c['tags']: tag = Tag(tag_str, article) dbsession.add(tag) # force update of tags cloud h.get_public_tags_cloud(force_reload=True) return HTTPFound(location=route_url('blog_go_article', request, article_id=article_id)) else: transaction.abort() return c