Exemplo n.º 1
0
 def post(self) -> dict:
     """Receive and process an abuse complaint."""
     messenger = self.request.registry.messenger
     messenger.send_abuse_complaint(url=self.request.validated['url'],
                                    remark=self.request.validated['remark'],
                                    user=get_user(self.request))
     return ''
Exemplo n.º 2
0
 def validator(node, value):
     user = get_user(request)
     if user is None or user != value['subject']:
         err = colander.Invalid(node,
                                msg='')  # msg='' workaround colander bug
         err['subject'] = 'Must be the currently logged-in user'
         raise err
Exemplo n.º 3
0
 def post(self) -> dict:
     """Receive and process an abuse complaint."""
     messenger = self.request.registry.messenger
     messenger.send_abuse_complaint(url=self.request.validated['url'],
                                    remark=self.request.validated['remark'],
                                    user=get_user(self.request))
     return ''
Exemplo n.º 4
0
 def post(self) -> dict:
     """Send a message to another user."""
     messenger = self.request.registry.messenger
     data = self.request.validated
     messenger.send_message_to_user(recipient=data['recipient'],
                                    title=data['title'],
                                    text=data['text'],
                                    from_user=get_user(self.request))
     return ''
Exemplo n.º 5
0
 def post(self) -> dict:
     """Send a message to another user."""
     messenger = self.request.registry.messenger
     data = self.request.validated
     messenger.send_message_to_user(recipient=data['recipient'],
                                    title=data['title'],
                                    text=data['text'],
                                    from_user=get_user(self.request))
     return ''
Exemplo n.º 6
0
 def post(self) -> dict:
     """Create new resource and get response data."""
     iresource = self.request.validated["content_type"]
     resource_type = iresource.__identifier__
     appstructs = self.request.validated.get("data", {})
     creator = get_user(self.request)
     resource = self.registry.create(
         resource_type, self.context, creator=creator, appstructs=appstructs, request=self.request
     )
     return self.build_post_response(resource)
Exemplo n.º 7
0
def _get_user_info(request: Request) -> (str, str):
    if not hasattr(request, 'authenticated_userid'):
        return ('', '')  # ease scripting without user and testing
    user = get_user(request)
    if user is None:
        return ('', '')
    else:
        user_name = get_sheet_field(user, IUserBasic, 'name')
        user_path = resource_path(user)
        return (user_name, user_path)
Exemplo n.º 8
0
def _get_user_info(request: Request) -> (str, str):
    if not hasattr(request, 'authenticated_userid'):
        return ('', '')  # ease scripting without user and testing
    user = get_user(request)
    if user is None:
        return ('', '')
    else:
        user_name = get_sheet_field(user, IUserBasic, 'name')
        user_path = resource_path(user)
        return (user_name, user_path)
Exemplo n.º 9
0
 def _create(self) -> IResource:
     validated = self.request.validated
     kwargs = dict(parent=self.context,
                   appstructs=validated.get('data', {}),
                   creator=get_user(self.request),
                   root_versions=validated.get('root_versions', []),
                   request=self.request,
                   is_batchmode=is_batchmode(self.request),
                   )
     iresource = validated['content_type']
     return self.content.create(iresource.__identifier__, **kwargs)
Exemplo n.º 10
0
def validate_user_headers(request: Request):
    """
    Validate the user headers.

    If the request has a 'X-User-Path' and/or 'X-User-Token' header, we
    ensure that the session takes belongs to the user and is not expired.
    """
    headers = request.headers
    if 'X-User-Path' in headers or 'X-User-Token' in headers:
        if get_user(request) is None:
            error = error_entry('header', 'X-User-Token', 'Invalid user token')
            request.errors.append(error)
Exemplo n.º 11
0
def validate_user_headers(request: Request):
    """
    Validate the user headers.

    If the request has a 'X-User-Path' and/or 'X-User-Token' header, we
    ensure that the session takes belongs to the user and is not expired.
    """
    headers = request.headers
    if "X-User-Path" in headers or "X-User-Token" in headers:
        if get_user(request) is None:
            error = error_entry("header", "X-User-Token", "Invalid user token")
            request.errors.append(error)
Exemplo n.º 12
0
def update_modification_date_modified_by(event):
    """Update the IMetadata fields `modified_by` and `modification_date`."""
    sheet = get_sheet(event.object, IMetadata, registry=event.registry)
    request = event.request
    appstruct = {}
    appstruct['modification_date'] = get_modification_date(event.registry)
    if request is not None:
        appstruct['modified_by'] = get_user(request)
    sheet.set(appstruct,
              send_event=False,
              request=request,
              omit_readonly=False,
              )
Exemplo n.º 13
0
def update_modification_date_modified_by(event):
    """Update the IMetadata fields `modified_by` and `modification_date`."""
    sheet = get_sheet(event.object, IMetadata, registry=event.registry)
    request = event.request
    appstruct = {}
    appstruct['modification_date'] = get_modification_date(event.registry)
    if request is not None:
        appstruct['modified_by'] = get_user(request)
    sheet.set(
        appstruct,
        send_event=False,
        request=request,
        omit_readonly=False,
    )
Exemplo n.º 14
0
 def post(self) -> dict:
     """Create new resource and get response data."""
     iresource = self.request.validated['content_type']
     resource_type = iresource.__identifier__
     appstructs = self.request.validated.get('data', {})
     creator = get_user(self.request)
     resource = self.registry.create(
         resource_type,
         self.context,
         creator=creator,
         appstructs=appstructs,
         request=self.request,
     )
     return self.build_post_response(resource)
Exemplo n.º 15
0
 def post(self) -> dict:
     """Create new resource and get response data."""
     iresource = self.request.validated['content_type']
     resource_type = iresource.__identifier__
     appstructs = self.request.validated.get('data', {})
     creator = get_user(self.request)
     metric = self._get_post_metric_name(iresource)
     with statsd_timer(metric, rate=1, registry=self.registry):
         resource = self.content.create(resource_type,
                                        self.context,
                                        creator=creator,
                                        appstructs=appstructs,
                                        request=self.request,
                                        )
     return self.build_post_response(resource)
Exemplo n.º 16
0
 def post(self) -> dict:
     """Create new resource and get response data."""
     iresource = self.request.validated['content_type']
     resource_type = iresource.__identifier__
     appstructs = self.request.validated.get('data', {})
     creator = get_user(self.request)
     metric = self._get_post_metric_name(iresource)
     with statsd_timer(metric, rate=1, registry=self.registry):
         resource = self.content.create(
             resource_type,
             self.context,
             creator=creator,
             appstructs=appstructs,
             request=self.request,
         )
     return self.build_post_response(resource)
Exemplo n.º 17
0
    def post(self):
        """Create new resource and get response data.

        For :class:`adhocracy_core.interfaces.IItemVersion`:

        If a `new version` is already created in this transaction we don't want
        to create a new one. Instead we modify the existing one.

        This is needed to make :class:`adhocray_core.rest.batchview.BatchView`
        work.
        """
        batchmode = is_batchmode(self.request)
        validated = self.request.validated
        iresource = validated['content_type']
        resource_type = iresource.__identifier__
        appstructs = validated.get('data', {})
        creator = get_user(self.request)
        root_versions = validated.get('root_versions', [])
        last_new_version = validated.get('_last_new_version_in_transaction',
                                         None)
        metric = self._get_post_metric_name(iresource)
        with statsd_timer(metric, rate=1, registry=self.registry):
            if last_new_version is not None:  # only happens in batch request
                sheets = self.content.get_sheets_create(
                    last_new_version, self.request)
                appstructs = self.request.validated.get('data', {})
                for sheet in sheets:
                    name = sheet.meta.isheet.__identifier__
                    if name in appstructs:  # pragma: no branch
                        sheet.set(appstructs[name], request=self.request)
                resource = last_new_version
            else:
                resource = self.content.create(
                    resource_type,
                    self.context,
                    appstructs=appstructs,
                    creator=creator,
                    root_versions=root_versions,
                    request=self.request,
                    is_batchmode=batchmode,
                )
        return self.build_post_response(resource)
Exemplo n.º 18
0
    def post(self):
        """Create new resource and get response data.

        For :class:`adhocracy_core.interfaces.IItemVersion`:

        If a `new version` is already created in this transaction we don't want
        to create a new one. Instead we modify the existing one.

        This is needed to make :class:`adhocray_core.rest.batchview.BatchView`
        work.
        """
        batchmode = is_batchmode(self.request)
        validated = self.request.validated
        iresource = validated['content_type']
        resource_type = iresource.__identifier__
        appstructs = validated.get('data', {})
        creator = get_user(self.request)
        root_versions = validated.get('root_versions', [])
        last_new_version = validated.get('_last_new_version_in_transaction',
                                         None)
        metric = self._get_post_metric_name(iresource)
        with statsd_timer(metric, rate=1, registry=self.registry):
            if last_new_version is not None:  # only happens in batch request
                sheets = self.content.get_sheets_create(last_new_version,
                                                        self.request)
                appstructs = self.request.validated.get('data', {})
                for sheet in sheets:
                    name = sheet.meta.isheet.__identifier__
                    if name in appstructs:  # pragma: no branch
                        sheet.set(appstructs[name],
                                  request=self.request)
                resource = last_new_version
            else:
                resource = self.content.create(resource_type,
                                               self.context,
                                               appstructs=appstructs,
                                               creator=creator,
                                               root_versions=root_versions,
                                               request=self.request,
                                               is_batchmode=batchmode,
                                               )
        return self.build_post_response(resource)
Exemplo n.º 19
0
 def make_one(self, request):
     from adhocracy_core.utils import get_user
     return get_user(request)
Exemplo n.º 20
0
 def _validate_subject_is_current_user(self, node, value, request):
     user = get_user(request)
     if user is None or user != value['subject']:
         err = colander.Invalid(node)
         err['subject'] = 'Must be the currently logged-in user'
         raise err
Exemplo n.º 21
0
 def make_one(self, request):
     from adhocracy_core.utils import get_user
     return get_user(request)
Exemplo n.º 22
0
 def _validate_subject_is_current_user(self, node, value, request):
     user = get_user(request)
     if user is None or user != value['subject']:
         err = colander.Invalid(node)
         err['subject'] = 'Must be the currently logged-in user'
         raise err