コード例 #1
0
ファイル: utils.py プロジェクト: binary1230/ubersystem
 def from_sessionized(cls, d):
     if is_listy(d):
         return [cls.from_sessionized(t) for t in d]
     elif isinstance(d, dict):
         assert d['_model'] in {'Attendee', 'Group'}
         if d['_model'] == 'Group':
             return cls.from_sessionized_group(d)
         else:
             return cls.from_sessionized_attendee(d)
     else:
         return d
コード例 #2
0
 def to_sessionized(cls, m):
     from uber.models import Attendee, Group
     if is_listy(m):
         return [cls.to_sessionized(t) for t in m]
     elif isinstance(m, dict):
         return m
     elif isinstance(m, Attendee):
         return m.to_dict(Attendee.to_dict_default_attrs + ['promo_code'] +
                          list(Attendee._extra_apply_attrs_restricted))
     elif isinstance(m, Group):
         return m.to_dict(Group.to_dict_default_attrs + ['attendees'] +
                          list(Group._extra_apply_attrs_restricted))
     else:
         raise AssertionError('{} is not an attendee or group'.format(m))
コード例 #3
0
 def success(result):
     response = {'jsonrpc': '2.0', 'id': id, 'result': result}
     log.debug(
         'Returning success message: {}', {
             'jsonrpc':
             '2.0',
             'id':
             id,
             'result':
             len(result)
             if is_listy(result) else str(result).encode('utf-8')
         })
     cherrypy.response.status = 200
     return response
コード例 #4
0
ファイル: api.py プロジェクト: magfest/residue
def normalize_sort(model, sort):
    if sort and isinstance(sort, six.string_types):
        first_char = sort.lstrip()[0]
        if first_char == '[' or first_char == '{':
            sort = json.loads(sort)

    if isinstance(sort, six.string_types):
        return [{'field': _extract_sort_field(model, sort), 'dir': 'asc'}]
    elif is_listy(sort):
        sorters = []
        for s in sort:
            sorters.extend(normalize_sort(model, s))
        return sorters
    elif isinstance(sort, dict):
        field = sort.get('property', sort.get('fields', sort.get('field', [])))
        direction = sort.get('direction', sort.get('dir', 'asc')).lower()
        return [{'field': _extract_sort_field(model, field), 'dir': direction}]
    return [{'field': 'id', 'dir': 'asc'}]
コード例 #5
0
ファイル: orm.py プロジェクト: magfest/residue
    def _merge_relations(self,
                         name,
                         value,
                         validator=lambda self, name, val: True):
        attr = getattr(self.__class__, name)
        if (not isinstance(attr, InstrumentedAttribute)
                or not isinstance(attr.property, RelationshipProperty)):
            return

        session = orm.Session.object_session(self)
        assert session, "cannot call _merge_relations on objects not attached to a session"

        property = attr.property
        relation_cls = property.mapper.class_

        # e.g., if this a Team with many Players, and we're handling the attribute name
        # "players," we want to set the team_id on all dictionary representations of those players.
        backref_id_name = self.one_to_many_foreign_key_column_name(name)
        original_value = getattr(self, name)

        if is_listy(original_value):
            new_insts = []
            if value is None:
                value = []

            if isinstance(value, six.string_types):
                value = [value]

            for i in value:
                if backref_id_name is not None and isinstance(
                        i, dict) and not i.get(backref_id_name):
                    i[backref_id_name] = self.id
                relation_inst = relation_cls._create_or_fetch(
                    session, i,
                    **{backref_id_name: self.id} if backref_id_name else {})
                if isinstance(i, dict):
                    if relation_inst._sa_instance_state.identity:
                        validator = _crud_write_validator
                    else:
                        validator = _crud_create_validator
                    relation_inst.from_dict(i, validator)
                new_insts.append(relation_inst)

            relation = original_value
            remove_insts = [
                stale_inst for stale_inst in relation
                if stale_inst not in new_insts
            ]

            for stale_inst in remove_insts:
                relation.remove(stale_inst)
                if property.cascade.delete_orphan:
                    session.delete(stale_inst)

            for new_inst in new_insts:
                if new_inst.id is None or new_inst not in relation:
                    relation.append(new_inst)

        elif isinstance(value, (collections.Mapping, six.string_types)):
            if backref_id_name is not None and not value.get(backref_id_name):
                # if this is a dictionary, it's possible we're going to be
                # creating a new thing, if so, we'll add a backref to the
                # "parent" if one isn't already set
                value[backref_id_name] = self.id

            relation_inst = relation_cls._create_or_fetch(session, value)
            stale_inst = original_value
            if stale_inst is None or stale_inst.id != relation_inst.id:
                if stale_inst is not None and property.cascade.delete_orphan:
                    session.delete(stale_inst)

            if isinstance(value, collections.Mapping):
                relation_inst.from_dict(value, validator)
                session.flush([
                    relation_inst
                ])  # we want this this to be queryable for other things

            setattr(self, name, relation_inst)

        elif value is None:
            # the first branch handles the case of setting a many-to-one value
            # to None. So this is for the one-to-one-mapping case
            # Setting a relation to None is nullifying the relationship, which
            # has potential side effects in the case of cascades, etc.
            setattr(self, name, value)
            stale_inst = original_value
            if stale_inst is not None and property.cascade.delete_orphan:
                session.delete(stale_inst)

        else:
            raise TypeError('merging relations on {1} not support for values '
                            'of type: {0.__class__.__name__} '
                            '(value: {0})'.format(value, name))
コード例 #6
0
ファイル: server.py プロジェクト: magfest/ubersystem
 def success(result):
     response = {'jsonrpc': '2.0', 'id': id, 'result': result}
     log.debug('Returning success message: {}', {
         'jsonrpc': '2.0', 'id': id, 'result': len(result) if is_listy(result) else str(result).encode('utf-8')})
     cherrypy.response.status = 200
     return response