예제 #1
0
def news_patch(context, data_dict):
    '''Patch a news. See news_create for
    possible fields.

    The difference between the update and patch methods is that the patch will
    perform an update of the provided parameters, while leaving all other
    parameters unchanged, whereas the update method deletes all parameters
    not explicitly provided in the data_dict

    :param id: The id of the news.
    :type id: string

    :returns: a patched news
    :rtype: dictionary

    '''

    log.info('News patch: %r', data_dict)

    logic.check_access('news_patch', context, data_dict)

    news_patch_schema = schema.news_patch_schema()
    fields = news_patch_schema.keys()

    # Exclude fields from the schema that are not in data_dict
    for field in fields:
        if field not in data_dict.keys() and field != 'id':
            news_patch_schema.pop(field)

    data, errors = df.validate(data_dict, news_patch_schema, context)

    if errors:
        raise toolkit.ValidationError(errors)

    news = ckanextNews.get(key=data['id'], attr='id')

    if news is None:
        raise logic.NotFound

    fields = news_patch_schema.keys()

    for field in fields:
        setattr(news, field, data.get(field))

        news.save()

    out = news_dictize(news)

    return out
예제 #2
0
def news_show(context, data_dict):
    '''Return the metadata of a news.

    :param id: the id of the news
    :type id: string

    :rtype: dictionary

    '''
    log.info('News show: %r', data_dict)

    id = toolkit.get_or_bust(data_dict, 'id')

    news = ckanextNews.get(key=id, attr='id')

    if news is None:
        raise logic.NotFound

    out = news_dictize(news)

    return out
예제 #3
0
def news_update(context, data_dict):
    '''Update a news. This will update all fields. See news_create for
    possible fields.

    :param id: The id of the news.
    :type id: string

    :returns: an updated news
    :rtype: dictionary

    '''

    log.info('News update: %r', data_dict)

    logic.check_access('news_update', context, data_dict)

    news_update_schema = schema.news_update_schema()

    data, errors = df.validate(data_dict, news_update_schema, context)

    if errors:
        raise toolkit.ValidationError(errors)

    news = ckanextNews.get(key=data['id'], attr='id')

    if news is None:
        raise logic.NotFound

    news.title = data.get('title')
    news.content = data.get('content', u'')
    news.meta = data.get('meta', u'{}')
    news.expiration_date = data.get('expiration_date')
    news.image_url = data.get('image_url', u'')
    news.save()

    out = news_dictize(news)

    return out
예제 #4
0
def news_create(context, data_dict):
    '''Create a news.

    :param title: The title of the news.
    :type title: string

    :param description: Description of the news.
    :type description: string

    :param active: State of the news (optional). Default is true.
    :type active: boolean

    :param meta: Additional meta data for the news such as latitude/longitude etc.
    :type meta: string in JSON format

    :returns: the newly created news object
    :rtype: dictionary

    '''

    log.info('News create: %r', data_dict)
    l.check_access('news_create', context, data_dict)
    data, errors = df.validate(data_dict, schema.news_create_schema(), context)

    if errors:
        raise t.ValidationError(errors)

    title = data.get('title')
    name = gen_news_name(title)
    content = data.get('content', u'')
    meta = data.get('meta', u'{}')
    expiration_date = data.get('expiration_date')
    image_url = data.get('image_url', u'')

    m = context.get('model')
    user_obj = m.User.get(context.get('user'))

    news = ckanextNews(title=title,
                       name=name,
                       content=content,
                       meta=meta,
                       expiration_date=expiration_date,
                       image_url=image_url,
                       creator_id=user_obj.id)
    news.save()
    out = news_dictize(news)

    # Send mail notification to all news subscribed users except the creator of the news
    # TODO: Email notifications should be sent asynchronous using celery tasks
    # TODO: Setup email server for testing mode
    send_email_condition = config.get('testing', False)
    if not send_email_condition:
        users = _get_action('news_mail_subscribed_users_show')(
            {
                'ignore_auth': True
            }, {})
        vars = {
            'site_title_dk': config_option_show('ckan.site_title', 'da_DK'),
            'site_title_en': config_option_show('ckan.site_title', 'en'),
            'site_url': config.get('ckan.site_url'),
            'news_item_title': out['title'],
            'news_item_content': render_markdown(out['content'], True)
        }

        for u in users:
            if user_obj.id == u['subscriber_id']:
                continue

            u_obj = context['model'].User.get(u['subscriber_id'])
            if u_obj is None:
                continue

            vars['user_name'] = u_obj.name
            msg_body = render_jinja2('emails/news_published.txt', vars)
            msg_subject = render_jinja2('emails/news_published_subject.txt',
                                        vars)
            send_email(msg_body, u_obj.email, msg_subject)

    return out