Beispiel #1
0
def fetch_and_wrap_form(doc_id):
    # This logic is independent of couchforms; when it moves elsewhere,
    # please use the most appropriate alternative to get a DB handle.

    db = XFormInstance.get_db()
    doc = db.get(doc_id)
    if doc['doc_type'] in doc_types():
        return doc_types()[doc['doc_type']].wrap(doc)
    if doc['doc_type'] == "%s%s" % (XFormInstance.__name__, DELETED_SUFFIX):
        raise UnexpectedDeletedXForm(doc_id)
    raise ResourceNotFound(doc_id)
Beispiel #2
0
 def get_owner(self):
     if not self.owner_id:
         raise ConfigurationError('Owner ID missing')
     try:
         if self.owner_type == OWNER_TYPE_GROUP:
             return Group.get(self.owner_id)
         elif self.owner_type == OWNER_TYPE_LOCATION:
             return SQLLocation.objects.get(location_id=self.owner_id)
         elif self.owner_type == OWNER_TYPE_USER:
             user = CouchUser.get_by_user_id(self.owner_id)
             if user:
                 return user
             else:
                 raise ResourceNotFound()
         else:
             raise ConfigurationError(f'Unknown owner type {self.owner_type!r}')
     except (ResourceNotFound, SQLLocation.DoesNotExist):
         raise ConfigurationError(f'{self.owner_type.capitalize()} '
                                  f'{self.owner_id!r} does not exist')
Beispiel #3
0
 def get(cls, docid, rev=None, db=None, dynamic_properties=True):
     # copied and tweaked from the superclass's method
     if not db:
         db = cls.get_db()
     cls._allow_dynamic_properties = dynamic_properties
     # on cloudant don't get the doc back until all nodes agree
     # on the copy, to avoid race conditions
     extras = get_safe_read_kwargs()
     try:
         if cls == XFormInstance:
             doc = db.get(docid, rev=rev, **extras)
             if doc['doc_type'] in doc_types():
                 return doc_types()[doc['doc_type']].wrap(doc)
             try:
                 return XFormInstance.wrap(doc)
             except WrappingAttributeError:
                 raise ResourceNotFound(
                     "The doc with _id {} and doc_type {} can't be wrapped "
                     "as an XFormInstance".format(docid, doc['doc_type']))
         return db.get(docid, rev=rev, wrapper=cls.wrap, **extras)
     except ResourceNotFound:
         raise XFormNotFound(docid)
Beispiel #4
0
def lookup_by_property(domain, prop_name, val, scope, root=None):
    if root and not isinstance(root, basestring):
        root = root._id

    if prop_name == 'site_code':
        index_view = 'locations/prop_index_site_code'
    else:
        # this was to be backwards compatible with the api
        # if this ever comes up, please take a moment to decide whether it's
        # worth changing the API to raise a less nonsensical error
        # (or change this function to not sound so general!)
        raise ResourceNotFound('missing prop_index_%s' % prop_name)

    startkey = [domain, val]
    if scope == 'global':
        startkey.append(None)
    elif scope == 'descendant':
        startkey.append(root)
    elif scope == 'child':
        startkey.extend([root, 1])
    else:
        raise ValueError('invalid scope type')

    return set(row['id'] for row in Location.get_db().view(index_view, startkey=startkey, endkey=startkey + [{}]))
Beispiel #5
0
 def get(self, record_id):
     try:
         return self._getter(record_id)
     except (XFormNotFound, CaseNotFound, ObjectDoesNotExist):
         raise ResourceNotFound("missing")
Beispiel #6
0
def _domain_requires_auth(domain):
    domain_obj = Domain.get_by_name(domain, strict=True)
    if domain_obj:
        return domain_obj.secure_submissions
    else:
        raise ResourceNotFound('No domain with name %s' % domain)
Beispiel #7
0
 def get_cached_case_attachment(domain, _case_id, attachment_id):
     if case_id == _case_id and attachment_id in attachments:
         return MockCachedObject(attachments[attachment_id])
     else:
         raise ResourceNotFound()