示例#1
0
def apache_graceful_restart(f):
    'example after_update function'
    out, err = Popen(['/usr/sbin/apachectl', '-k', 'graceful'],
                     stdout=PIPE,
                     stderr=PIPE).communicate()
    if err:
        h.flash_error(unicode(err))
def delete(content_type, content_item_id, comment_id):
    context = {'model': model, 'user': c.user}
    data_dict = {'id': content_item_id}

    # Auth check to make sure the user can see this content item
    helpers.check_content_access(content_type, context, data_dict)

    try:
        # Load the content item
        helpers.get_content_item(content_type, context, data_dict)
    except:
        abort(403)

    try:
        data_dict = {
            'id': comment_id,
            'content_type': content_type,
            'content_item_id': content_item_id
        }
        get_action('comment_delete')(context, data_dict)
    except Exception as e:
        log.debug(e)
        if e.error_dict and e.error_dict.get('message'):
            msg = e.error_dict['message']
        else:
            msg = str(e)
        h.flash_error(msg)

    return h.redirect_to(
        helpers.get_redirect_url(
            content_type,
            content_item_id if content_type == 'datarequest' else c.pkg.name,
            'comment_' + str(comment_id)))
 def get(self, dataset_id, resource_id):
     context = {'model': model, 'session': model.Session, 'user': c.user}
     pkg_dict = None
     resource = None
     permissions = None
     acl = None
     acl_dict=None
     acl_permission = None
     try:
         check_access('resource_acl_create', context, {
             'resource_id': resource_id
         })
     except NotAuthorized:
         abort(403, _('Unauthorized to create resource acl %s') % '')
     try:
         pkg_dict = get_action('package_show')(None, {'id': dataset_id})
         resource = get_action('resource_show')(None, {'id': resource_id})
         permissions = [{
             'text': u'None',
             'value': 'none'
         }, {
             'text': u'Read',
             'value': 'read'
         }]
         acl = request.params.get('id')
         if acl:
             acl_dict = get_action('resource_acl_show')(context, {
                 'id': acl
             })
             if acl_dict['auth_type'] == 'user':
                 auth = get_action('user_show')(
                     context, {
                         'id': acl_dict['auth_id']
                     })
             else:
                 auth = get_action('organization_show')(
                     context, {
                         'id': acl_dict['auth_id']
                     })
             acl_permission = acl_dict['permission']
     except NotAuthorized:
         abort(403)
     except NotFound:
         abort(404)
     except ValidationError as e:
         h.flash_error(e.error_summary)
     return render(
         'resource-authorizer-detail/acl_detail.html',
         extra_vars={
             'pkg_dict': pkg_dict,
             'resource': resource,
             'dataset_id': dataset_id,
             'resource_id': resource_id,
             'acl_dict': acl_dict,
             'acl_permission': acl_permission,
             'permissions': permissions
         })
def edit(content_type, content_item_id, comment_id):
    context = {'model': model, 'user': c.user}
    data_dict = {'id': content_item_id}

    # Auth check to make sure the user can see this content item
    helpers.check_content_access(content_type, context, data_dict)

    try:
        # Load the content item
        helpers.get_content_item(content_type, context, data_dict)
    except:
        abort(403)

    if request.method == 'POST':
        data_dict = clean_dict(
            unflatten(
                tuplize_dict(
                    parse_params(
                        request_helpers.RequestHelper(
                            request).get_post_params()))))
        data_dict['id'] = comment_id
        data_dict['content_type'] = content_type
        data_dict['content_item_id'] = content_item_id
        success = False
        try:
            get_action('comment_update')(context, data_dict)
            success = True
        except ValidationError as ve:
            log.debug(ve)
            if ve.error_dict and ve.error_dict.get('message'):
                msg = ve.error_dict['message']
            else:
                msg = str(ve)
            h.flash_error(msg)
        except Exception as e:
            log.debug(e)
            abort(403)

        return h.redirect_to(
            helpers.get_redirect_url(
                content_type, content_item_id
                if content_type == 'datarequest' else c.pkg.name, 'comment_' +
                str(comment_id) if success else 'edit_' + str(comment_id)))

    return helpers.render_content_template(content_type)
 def post(self, dataset_id, resource_id):
     context = {'model': model, 'session': model.Session, 'user': c.user}
     try:
         check_access('resource_acl_create', context, {
             'resource_id': resource_id
         })
     except NotAuthorized:
         abort(403, _('Unauthorized to create resource acl %s') % '')
     try:
         pkg_dict = get_action('package_show')(None, {'id': dataset_id})
         resource = get_action('resource_show')(None, {'id': resource_id})
         permissions = [{
             'text': u'None',
             'value': 'none'
         }, {
             'text': u'Read',
             'value': 'read'
         }]
         data_dict = clean_dict(
             dict_fns.unflatten( 
                 tuplize_dict(parse_params(request.form))))
         acl = data_dict.get('id')
         if acl is None:
             data = {
                 'resource_id': resource_id,
                 'permission': data_dict['permission']
             }
             if data_dict['organization']:
                 group = model.Group.get(data_dict['organization'])
                 if not group:
                     message = _(u'Organization {org} does not exist.').format(
                         org=data_dict['organization'])
                     raise ValidationError(
                         {
                             'message': message
                         }, error_summary=message)
                 data['auth_type'] = 'org'
                 data['auth_id'] = group.id
             elif data_dict['username']:
                 user = model.User.get(data_dict['username'])
                 if not user:
                     message = _(u'User {username} does not exist.').format(
                         username=data_dict['username'])
                     raise ValidationError(
                         {
                             'message': message
                         }, error_summary=message)
                 data['auth_type'] = 'user'
                 data['auth_id'] = user.id
             get_action('resource_acl_create')(None, data)
         else:
             data = {'id': acl, 'permission': data_dict['permission']}
             get_action('resource_acl_patch')(None, data)
         return h.redirect_to(
             'resourceauthorizer.resource_acl',
             dataset_id=dataset_id,
             resource_id=resource_id
         )
     except NotAuthorized:
         abort(403)
     except NotFound:
         abort(404)
     except ValidationError as e:
         h.flash_error(e.error_summary)
def _add_or_reply(comment_type, content_item_id, content_type, parent_id=None):
    """
    Allows the user to add a comment to an existing dataset or datarequest
    :param comment_type: Either 'new' or 'reply'
    :param content_item_id:
    :param content_type: string 'dataset' or 'datarequest'
    :return:
    """
    content_type = 'dataset' if 'content_type' not in vars() else content_type

    context = {'model': model, 'user': c.user}

    data_dict = {'id': content_item_id}

    # Auth check to make sure the user can see this content item
    helpers.check_content_access(content_type, context, data_dict)

    try:
        # Load the content item
        helpers.get_content_item(content_type, context, data_dict)
    except:
        abort(403)

    if request.method == 'POST':
        data_dict = clean_dict(
            unflatten(
                tuplize_dict(
                    parse_params(
                        request_helpers.RequestHelper(
                            request).get_post_params()))))
        data_dict['parent_id'] = c.parent.id if 'parent' in dir(c) else None

        data_dict['url'] = '/%s/%s' % (content_type,
                                       content_item_id if content_type
                                       == 'datarequest' else c.pkg.name)

        success = False
        try:
            res = get_action('comment_create')(context, data_dict)
            success = True
        except ValidationError as ve:
            log.debug(ve)
            if ve.error_dict and ve.error_dict.get('message'):
                msg = ve.error_dict['message']
            else:
                msg = str(ve)
            h.flash_error(msg)
        except Exception as e:
            log.debug(e)
            abort(403)

        if success:
            email_notifications.notify_admins_and_comment_notification_recipients(
                helpers.get_org_id(content_type),
                toolkit.c.userobj,
                'notification-new-comment',
                content_type,
                helpers.get_content_item_id(content_type),
                res['thread_id'],
                res['parent_id'] if comment_type == 'reply' else None,
                res['id'],
                c.pkg_dict['title']
                if content_type == 'dataset' else c.datarequest['title'],
                res['content']  # content is the comment that has been cleaned up in the action comment_create
            )

            if notification_helpers.comment_notification_recipients_enabled():
                if comment_type == 'reply':
                    # Add the user who submitted the reply to comment notifications for this thread
                    notification_helpers.add_commenter_to_comment_notifications(
                        toolkit.c.userobj.id, res['thread_id'],
                        res['parent_id'])
                else:
                    # Add the user who submitted the comment notifications for this new thread
                    notification_helpers.add_commenter_to_comment_notifications(
                        toolkit.c.userobj.id, res['thread_id'], res['id'])

        return h.redirect_to(
            helpers.get_redirect_url(
                content_type, content_item_id if content_type == 'datarequest'
                else c.pkg.name, 'comment_' + str(res['id']) if success else
                ('comment_form' if comment_type == 'new' else 'reply_' +
                 str(parent_id))))

    return helpers.render_content_template(content_type)