Beispiel #1
0
    def patch (self, request, character_trait_id):
        character_trait = session.query(CharacterTrait).get(character_trait_id)
        trait = character_trait.trait
        for field, value in request.data.items():
            setattr(character_trait, field, value)
        self.character.date_last_edited = datetime.now()
        session.commit()

        return JsonResponse(
            {
                'character': self.character,
                'model': character_trait,
                'availabilities': self.recalculate_access(trait),
            },
            encoder = ModelEncoder
        )
Beispiel #2
0
    def delete (self, request, character_trait_id):
        character_trait = session.query(CharacterTrait).get(character_trait_id)
        trait = character_trait.trait
        self.character.date_last_edited = datetime.now()
        character_trait_attrs = character_trait.to_dict()

        # Delete the model and commit the change so that the deletion propagates
        # to any relationship collections that might reference this model.
        session.delete(character_trait)
        session.commit()

        return JsonResponse(
            {
                'character': self.character,
                'model': character_trait_attrs,
                'availabilities': self.recalculate_access(trait),
            },
            encoder = ModelEncoder
        )
Beispiel #3
0
    def post (self, request, character_trait_id):
        polymorphic_on = CharacterTrait.__mapper__.polymorphic_on
        polymorphic_value = request.data[polymorphic_on.name]
        mapper = CharacterTrait.__mapper__.polymorphic_map[polymorphic_value]
        Model = mapper.class_
        self.character.date_last_edited = datetime.now()

        # Create the new character trait and commit the addition so that the
        # model gets assigned an id.
        character_trait = Model(**request.data)
        session.add(character_trait)
        session.commit()
        trait = character_trait.trait

        return JsonResponse(
            {
                'character': self.character,
                'model': character_trait,
                'availabilities': self.recalculate_access(trait),
            },
            encoder = ModelEncoder
        )
    def handle (self, *args, **options):
        session.bind.echo = options['debug']
        filepath = options['filepath']

        model_info = {}

        with open(filepath, 'r') as data_file:
            content = ''.join(line.split('//')[0] for line in data_file)
            full_data = json.loads(content, object_pairs_hook = OrderedDict)

        for data_block in full_data:
            Model = get_model(data_block['application'], data_block['Model'])
            unique_indexes = {}

            # Prepare indeces of unique-constraints.
            for table in Model.__mapper__.tables:
                for constraint in table.constraints:
                    if isinstance(constraint, UniqueConstraint):
                        unique_indexes[constraint] = set()

            for datum in data_block['data']:
                datum = prepare(session, Model, datum)
                update_target = None

                for constraint, index in unique_indexes.items():
                    # Look for item in index.
                    key = key_from_constraint(constraint, datum)
                    if key in index:
                        self.stderr.write("ERROR: Second import of '{}' " +
                            "record with unique constraint:".format(Model))
                        for column, value in zip(constraint.columns, key):
                            self.stderr.write("       {} = {}".format(
                                column, value))
                        return

                    # Look for item in database.
                    target = session.query(Model).filter(
                        *filter_from_constraint(constraint, datum)).scalar()
                    if update_target and update_target != target:
                        self.stderr.write('ERROR: Second unique-index match does not yield the same model.')
                        self.stderr.write("       Model: {}".format(Model))
                        self.stderr.write("       constraint: {}".format(constraint))
                        self.stderr.write("       data: {}".format(datum))
                        return
                    update_target = target

                if update_target:
                    updated = False
                    for field, value in datum.items():
                        if getattr(update_target, field) != value:
                            updated = True
                            setattr(update_target, field, value)
                    if updated:
                        self.stdout.write("updating {}".format(update_target))
                else:
                    model = Model(**datum)
                    session.add(model)
                    self.stdout.write("adding {}".format(model))

            session.flush()

        session.commit()
Beispiel #5
0
 def process_response (self, request, response):
     session.commit()
     session.remove()
     return response
Beispiel #6
0
 def post (self, request):
     model = self.Model(**request.data)
     session.add(model)
     session.commit()
     return JsonResponse(model.to_dict(), encoder = ModelEncoder)
Beispiel #7
0
 def delete (self, request):
     session.delete(self.model)
     session.commit()
     return JsonResponse(None, safe = False)
Beispiel #8
0
 def patch (self, request):
     for fieldname, value in request.data.items():
         setattr(self.model, fieldname, value)
     session.commit()
     return self.get(request)