예제 #1
0
def user_subscribed_to_news(context, data_dict):
    model = context['model']
    user = context['user']
    user_obj = model.User.get(user)
    is_subscribed = ckanextNewsSubscriptions.get_subscription(user_obj.id)

    if is_subscribed:
        return True
    else:
        return False
예제 #2
0
def news_subscription_create(context, data_dict):
    '''Create a subscription .

        :param notify_by_mail: Setting if user should be notified by mail for news.
        :type notify_by_mail: boolean, default False

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

        '''

    l.check_access('news_subscription', context, data_dict)
    news_subscription_schema = schema.news_subscription_schema()
    data, errors = df.validate(data_dict, news_subscription_schema, context)

    if errors:
        raise t.ValidationError(errors)

    model = context['model']
    user = context['user']
    user_obj = model.User.get(user)

    notify_by_mail = data_dict.get('notify_by_mail', False)

    is_subscribed = ckanextNewsSubscriptions.get_subscription(user_obj.id)

    if is_subscribed:
        raise t.ValidationError('Subscribtion for user %s already exists!' %
                                user_obj.name)

    subscription = ckanextNewsSubscriptions(subscriber_id=user_obj.id,
                                            notify_by_mail=notify_by_mail)
    subscription.save()
    out = subscription.as_dict()
    send_email_condition = config.get('testing', False)
    if not send_email_condition:
        # Notify the user via email
        # TODO: E-mail notifications should be sent asynchronous using celery tasks
        # TODO: Setup email server for testing mode
        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'),
            'user_name': user_obj.name
        }
        msg_body = render_jinja2('emails/news_subscriptions.txt', vars)
        msg_subject = render_jinja2('emails/news_subscriptions_subject.txt',
                                    vars)
        send_email(msg_body, user_obj.email, msg_subject)

    return out
예제 #3
0
def news_subscription_show(context, data_dict):
    '''Get subscription .

        :returns: subscription object if existing
        :rtype: dictionary

        '''

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

    model = context['model']
    user = context['user']
    user_obj = model.User.get(user)

    subscription = ckanextNewsSubscriptions.get_subscription(user_obj.id)

    if subscription:
        subscription = subscription.as_dict()
    else:
        subscription = None

    return subscription
예제 #4
0
def news_subscription_update(context, data_dict):
    '''Update a subscription .

        :param notify_by_mail: Setting if user should be notified by mail for news.
        :type notify_by_mail: boolean


        :returns: the updated subscription object
        :rtype: dictionary

        '''

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

    news_subscription_schema = schema.news_subscription_schema()

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

    if errors:
        raise toolkit.ValidationError(errors)

    model = context['model']
    user = context['user']
    user_obj = model.User.get(user)

    notify_by_mail = data_dict.get('notify_by_mail', False)

    subscription = ckanextNewsSubscriptions.get_subscription(user_obj.id)

    if subscription is None:
        raise logic.NotFound

    subscription.notify_by_mail = notify_by_mail
    subscription.save()

    out = subscription.as_dict()
    return out