Example #1
0
    def times(self, request):
        """Get times an object has changed.

        The object's created_at time will be added if not present.
        """
        # Validate input
        times = TimesChangedSerializer(data=request.data)
        if not times.is_valid():
            raise ValidationError(times.errors)
        vd = times.validated_data

        # Find object by type and identity
        query = Query(vd.get('model')) \
            .identity(vd.get('identity')) \
            .time(vd.get('time'))

        records = query.fetch()

        # Raise 404 if not found
        if not records:
            raise Http404()

        created_at = records[0][vd.get('model')]['created_at']

        # Build query to get times
        logger.debug("GETTING TIMES")
        times = self._times(vd['model'], vd['identity'])

        if created_at not in times:
            times.append(created_at)

        results = GenericSerializer({'data': vd, 'times': times})
        return Response(results.data)
Example #2
0
    def _exists(self, model, identity, time):
        """Check that an instance of a model exists at a time.

        :param model: Name of the model
        :type model: str
        :param identity: Identity of the instance of the model.
        :type identity: str
        :param time: Time to verify in milliseconds since epoch
        :type type: int
        """
        query = Query(model).identity(identity).time(time)
        records = query.fetch()
        logger.debug("Found {} matches for time {}".format(len(records), time))
        return len(records) > 0