Exemple #1
0
    def update(self):
        """ This function overides the default behaviour for creating
            an object through a serializer.

            Updates a Transaccion object and the periodicidad nested object.
        """
        periodicidad = self.validated_data.pop('periodicidad')

        save_foreign_relationship([periodicidad], PeriodoSerializer,
                                  Periodo)[0]

        Transaccion.objects.filter(pk=self.instance.pk).update(
            **self.validated_data)
        return Transaccion.objects.get(pk=self.instance.pk)
Exemple #2
0
    def update(self):
        """ This function overides the default behaviour for creating
            an object through a serializer since Nested Updates are
            not implemented by DRF.

            Returns
            -------
            Updated Instance of Tutor model.
        """
        ingresos = self.validated_data.pop('tutor_ingresos', None)
        save_foreign_relationship(ingresos, IngresoSerializer, Ingreso,
                                  self.instance)
        Tutor.objects.filter(pk=self.instance.pk).update(**self.validated_data)
        return Tutor.objects.get(pk=self.instance.pk)
Exemple #3
0
    def create(self, familia):
        """ This function overides the default behaviour for creating
            an object through a serializer.

            This serializer can be called from a familia create method.
            If we are dealing with familia Transactions of from a Tutor
            create method if we are dealing with Tutor Transactions.

            This serializer dependes on a .models.Familia
            object instance. Since we are dealing with
            nested created objects, the familia must be
            created first and passed as parameter to the
            created function.

            Finally a Periodo object is created from the data passed
            throught the serializer.
        """
        periodicidad = self.validated_data.pop('periodicidad')

        periodo = save_foreign_relationship([periodicidad], PeriodoSerializer,
                                            Periodo)[0]

        self.validated_data['periodicidad'] = periodo
        self.validated_data['familia'] = familia

        return Transaccion.objects.create(**self.validated_data)
Exemple #4
0
    def update(self):
        """ This function overides the default behaviour for updating
            an object through a serializer.

            Updates nested relashionship with transaccion and then updates
            local information.
        """

        transaccion = self.validated_data.pop('transaccion')
        save_foreign_relationship([transaccion], TransaccionSerializer,
                                  Transaccion,
                                  self.instance.tutor.integrante.familia)

        Ingreso.objects.filter(pk=self.instance.pk).update(
            **self.validated_data)
        return Ingreso.objects.get(pk=self.instance.pk)
Exemple #5
0
    def create(self, integrante):
        """ This function overides the default behaviour for creating
            an object through a serializer.

            This serializer dependes on a .models.Integrante
            object instance. Since we are dealing with
            nested created objects, the integrante must be
            created first and passed as parameter to the
            created function.

            After creating the tutor object we use save_foreign_relationship
            to save ingreso objects that depend on tutor.
        """
        ingresos = self.validated_data.pop('tutor_ingresos', None)

        self.validated_data['integrante'] = integrante
        tutor = Tutor.objects.create(**self.validated_data)

        save_foreign_relationship(ingresos, IngresoSerializer, Ingreso, tutor)

        return tutor
Exemple #6
0
    def create(self):
        """ This function overides the default behaviour for creating
            an object through a serializer.

            Django Rest Framework does not implement saving nested
            serializers.

            Creates a Family instance. There are objects related to
            the family that depend upon the creation of a family
            instance first. This are the Integrantes and Comentarios.

            We first remove all Integrante, Comentario and Transaccion
            from the validated data. We create the family and then we
            create this objects that require the family instance.
        """
        integrantes = self.validated_data.pop('integrante_familia')
        comentarios = self.validated_data.pop('comentario_familia')
        transacciones = self.validated_data.pop('transacciones')

        family_instance = Familia.objects.create(**self.validated_data)

        save_foreign_relationship(integrantes, IntegranteSerializer,
                                  Integrante, family_instance)
        save_foreign_relationship(comentarios, ComentarioSerializer,
                                  Comentario, family_instance)
        save_foreign_relationship(transacciones, TransaccionSerializer,
                                  Transaccion, family_instance)

        return family_instance
Exemple #7
0
    def create(self, family):
        """ This function overides the default behaviour for creating
            an object through a serializer.

            This serializer's object dependes on a .models.Integrante
            object instance. Since we are dealing with nested
            created objects, the integrante must be created first
            and passed as parameter to the created function.

            The Integrante model has a two sepcialization which are
            indicated in the dictionary data as 'alumno_integrante'
            and 'tutor_integrante'. This means the Integrante is
            either a Tutor or an Alumno.

            This function removes that data from the validated_data
            dictionary, creates the Integrante and then creates the
            specialized instance of either Alumno or Tutor.

            Notes
            -----
            save_foreign_relationship does not create object on None
            object. Therefore if one of the specilizations is not
            indicated it is not created.
        """
        self.validated_data['familia'] = family

        alumno = self.validated_data.pop('alumno_integrante')
        tutor = self.validated_data.pop('tutor_integrante')
        self.validated_data['oficio'] = Oficio.objects.get(
            pk=self.validated_data['oficio']['id'])
        integrante = Integrante.objects.create(**self.validated_data)

        save_foreign_relationship([alumno], AlumnoSerializer, Alumno,
                                  integrante)
        save_foreign_relationship([tutor], TutorSerializer, Alumno, integrante)

        return integrante
Exemple #8
0
    def update(self):
        """ This function overides the default behaviour for creating
            an object through a serializer since Nested Updates are
            not implemented by DRF.

            Updates dependent object Alumno and Tutor.

            Returns
            -------
            Updated Instance of an Integrante model.
        """
        alumno = self.validated_data.pop('alumno_integrante')
        tutor = self.validated_data.pop('tutor_integrante')
        self.validated_data['oficio'] = Oficio.objects.get(
            pk=self.validated_data['oficio']['id'])

        save_foreign_relationship([alumno], AlumnoSerializer, Alumno,
                                  self.instance)
        save_foreign_relationship([tutor], TutorSerializer, Tutor,
                                  self.instance)

        Integrante.objects.filter(pk=self.instance.pk).update(
            **self.validated_data)
        return Integrante.objects.filter(pk=self.instance.pk)
Exemple #9
0
    def create(self, tutor):
        """ This function overides the default behaviour for creating
            an object through a serializer.

            This object depends on a nested relation with transaccion
            therefore it creates the object before saving.
        """

        transaccion = self.validated_data.pop('transaccion')
        transaccion_instance = save_foreign_relationship(
            [transaccion], TransaccionSerializer, Transaccion,
            tutor.integrante.familia)

        self.validated_data['transaccion'] = transaccion_instance[0]
        self.validated_data['tutor'] = tutor
        return Ingreso.objects.create(**self.validated_data)
Exemple #10
0
    def update(self):
        """ This function overides the default behaviour for creating
            an object through a serializer.

            Django Rest Framework does not implement saving nested
            serializers.

            Updates a familia object and all other objects that
            depend on it. Since the offline client will submit a
            complete JSON of the study each time.

            When an offline client submits an update of a study,
            new information can be created. save_foreign_relationship
            looks for the id in the object. If there is no data it will
            create the object.

            Integrantes and Transacciones has a non-destructive way of disactivating.
            This is donde by changing the is_active field.

            Comentario does not have this field, so all comentario instances
            that were not sent by the offline application most be removed.
        """
        integrantes = self.validated_data.pop('integrante_familia')
        comentarios = self.validated_data.pop('comentario_familia')
        transacciones = self.validated_data.pop('transacciones')

        Comentario.objects.filter(familia=self.instance).exclude(
            id__in=[comment.get('id') for comment in comentarios]).delete()

        save_foreign_relationship(integrantes, IntegranteSerializer,
                                  Integrante, self.instance)
        save_foreign_relationship(comentarios, ComentarioSerializer,
                                  Comentario, self.instance)
        save_foreign_relationship(transacciones, TransaccionSerializer,
                                  Transaccion, self.instance)

        Familia.objects.filter(pk=self.instance.pk).update(
            **self.validated_data)

        return Familia.objects.get(
            pk=self.instance.pk)  # Returns updated instance