Esempio n. 1
0
    def __getitem__(self, key):
        schema = self.model.schema()

        # lookup the articles information
        col = schema.column(key, raise_=False)
        if col:
            # return a reference for the collection
            if isinstance(col, orb.ReferenceColumn):
                record_id = self.model.select(where=Q(
                    self.model) == self.record_id,
                                              limit=1).values(col.name())[0]
                return ModelService(self.request,
                                    col.referenceModel(),
                                    record_id=record_id)

            # return the response information
            elif self.record_id:
                self.request.context = self
                if not self.permitted():
                    raise HTTPForbidden()
                else:
                    _, context = get_context(self.request, model=self.model)
                    record = self.model(self.record_id, context=context)
                    return ValueService(self.request, record.get(col))

            # columns are not directly accessible
            else:
                raise KeyError(key)

        # generate collector services
        lookup = schema.collector(key)
        if lookup:
            if not lookup.testFlag(lookup.Flags.Static):
                _, context = get_context(self.request, model=self.model)
                context.where = None
                record = self.model(self.record_id, context=context)
            else:
                record = self.model

            from .collection import CollectionService
            values, context = get_context(self.request, model=lookup.model())
            if values:
                where = orb.Query.build(values)
                context.where = where & context.where

            if isinstance(record, orb.Model):
                record.setContext(context)

            records = lookup(record, context=context)
            return CollectionService(self.request, records, parent=self)

        # make sure we're not trying to load a property we don't have
        elif self.record_id:
            raise KeyError(key)

        # otherwise, return a model based on the id
        return ModelService(self.request,
                            self.model,
                            parent=self,
                            record_id=key)
Esempio n. 2
0
    def __getitem__(self, key):
        schema = self.model.schema()

        # lookup the articles information
        col = schema.column(key, raise_=False)
        if col:
            # return a reference for the collection
            if isinstance(col, orb.ReferenceColumn):
                record_id = self.model.select(
                    where=Q(self.model) == self.record_id, limit=1
                ).values(col.name())[0]
                return ModelService(self.request,
                                    col.referenceModel(),
                                    record_id=record_id)

            # return the response information
            elif self.record_id:
                self.request.context = self
                if not self.permitted():
                    raise HTTPForbidden()
                else:
                    _, context = get_context(self.request, model=self.model)
                    record = self.model(self.record_id, context=context, delay=True)
                    return ValueService(self.request, record.get(col))

            # columns are not directly accessible
            else:
                raise KeyError(key)

        # generate collector services
        lookup = schema.collector(key)
        if lookup:
            if not lookup.testFlag(lookup.Flags.Static):
                _, context = get_context(self.request, model=self.model)
                context.where = None
                record = self.model(self.record_id, context=context, delay=True)
            else:
                record = self.model

            from .collection import CollectionService
            values, context = get_context(self.request, model=lookup.model())
            if values:
                where = orb.Query.build(values)
                context.where = where & context.where

            if isinstance(record, orb.Model):
                record.setContext(context)

            records = lookup(record, context=context)
            return CollectionService(self.request, records, parent=self)

        # make sure we're not trying to load a property we don't have
        elif self.record_id:
            raise KeyError(key)

        # otherwise, return a model based on the id
        return ModelService(self.request, self.model, parent=self, record_id=key)
Esempio n. 3
0
    def get(self):
        values, context = get_context(self.request, model=self.model)
        if context.returning == 'schema':
            return self.model.schema()
        elif self.record_id:
            try:
                record = self.model(self.record_id, context=context)
            except orb.errors.RecordNotFound:
                raise HTTPNotFound()
            action = self.get_record_action()
            if record is not None and action is not None:
                return action(record, self.request)
            else:
                return record

        else:

            action = self.get_model_action()
            if action:
                return action(self.request)

            else:
                # convert values to query parameters
                if values:
                    where = orb.Query.build(values)
                    context.where = where & context.where

                # grab search terms or query
                search_terms = self.request.params.get(
                    'terms') or self.request.params.get('q')

                if search_terms:
                    return self.model.search(search_terms, context=context)
                else:
                    return self.model.select(context=context)
Esempio n. 4
0
    def post(self):
        if self.model is None:
            raise HTTPBadRequest()

        if isinstance(self.collection.collector(), orb.Pipe):
            through_model = self.collection.collector().throughModel()
            target_model = self.collection.collector().toModel()

            # check if the values provided are for the middle-table
            values, context = get_context(self.request, model=through_model)
            if not values:
                values, conext = get_context(self.request, model=target_model)
        else:
            values, context = get_context(self.request, model=self.model)

        return self.collection.create(values, context=context)
Esempio n. 5
0
 def get(self):
     if self.model is not None and not self.collection.isLoaded():
         values, context = get_context(self.request, model=self.model)
         if values:
             where = orb.Query.build(values)
             context.where = where & context.where
         return self.collection.refine(context=context)
     else:
         return self.collection
Esempio n. 6
0
 def post(self):
     if self.record_id:
         raise HTTPBadRequest()
     else:
         values, context = get_context(self.request, model=self.model)
         action = self.get_model_action()
         if action:
             # Since we're running a post, there is no record instance
             # to pass to action. Post actions should always be
             # classmethods.
             return action(self.request)
         else:
             record = self.model.create(values, context=context)
             return record.__json__()
Esempio n. 7
0
 def post(self):
     if self.record_id:
         raise HTTPBadRequest()
     else:
         values, context = get_context(self.request, model=self.model)
         action = self.get_model_action()
         if action:
             # Since we're running a post, there is no record instance
             # to pass to action. Post actions should always be
             # classmethods.
             return action(self.request)
         else:
             record = self.model.create(values, context=context)
             return record.__json__()
Esempio n. 8
0
 def delete(self):
     if self.record_id:
         values, context = get_context(self.request, model=self.model)
         if self.from_collection:
             record = self.model(self.record_id)
             return self.from_collection.remove(record, context=context)
         else:
             record = self.model(self.record_id, context=context)
             action = self.get_record_action()
             if action:
                 return action(record, self.request)
             else:
                 record.delete()
                 return record
     else:
         raise HTTPBadRequest()
Esempio n. 9
0
 def delete(self):
     if self.record_id:
         values, context = get_context(self.request, model=self.model)
         if self.from_collection:
             record = self.model(self.record_id)
             return self.from_collection.remove(record,
                                                context=context)
         else:
             record = self.model(self.record_id, context=context)
             action = self.get_record_action()
             if action:
                 return action(record, self.request)
             else:
                 record.delete()
                 return record
     else:
         raise HTTPBadRequest()
Esempio n. 10
0
    def put(self):
        if self.model is None:
            raise HTTPBadRequest()

        _, context = get_context(self.request, model=self.model)

        try:
            params = self.request.json_body
        except StandardError:
            params = self.request.params

        try:
            records = params.get('records') or params.get('ids') or ''
        except KeyError:
            raise HTTPBadRequest()
        else:
            if isinstance(records, (unicode, str)):
                records = records.split(',') if records else []

            # update the collection with the new records
            return self.collection.update(records, context=context)
Esempio n. 11
0
    def get(self):
        values, context = get_context(self.request, model=self.model)
        if context.returning == 'schema':
            return self.model.schema()

        elif self.record_id:
            try:
                record = self.model(self.record_id, context=context)
            except orb.errors.RecordNotFound:
                raise HTTPNotFound()

            action = self.get_record_action()
            if record is not None and action is not None:
                return action(record, self.request)
            else:
                return record.__json__()

        else:

            action = self.get_model_action()
            if action:
                return action(self.request)

            else:
                # convert values to query parameters
                if values:
                    where = orb.Query.build(values)
                    context.where = where & context.where

                # grab search terms or query
                search_terms = self.request.params.get('terms') or self.request.params.get('q')

                if search_terms:
                    return self.model.search(search_terms, context=context)
                else:
                    return self.model.select(context=context)
Esempio n. 12
0
 def _update(self):
     values, context = get_context(self.request, model=self.model)
     record = self.model(self.record_id, context=context)
     record.update(values)
     record.save()
     return record
Esempio n. 13
0
 def _update(self):
     values, context = get_context(self.request, model=self.model)
     record = self.model(self.record_id, context=context)
     record.update(values)
     record.save()
     return record