Esempio n. 1
0
def delete_article(request):
    article_id = int(request.matchdict['article_id'])
    dbsession = DBSession()
    article = dbsession.query(Article).get(article_id)

    if article is None:
        return HTTPNotFound()

    # delete article and all article comments, invalidate tags too
    dbsession.query(Comment).filter(Comment.article_id == article_id).delete()
    dbsession.delete(article)
    h.get_public_tags_cloud(force_reload=True)

    data = {}
    return data
Esempio n. 2
0
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
Esempio n. 3
0
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