def add_gpx(*, gpx_content, user): """ Create a new for_runners.models.GpxModel entry :param gpx_content: String content of the new gpx file :return: GpxModel instance """ try: gpxpy_instance = parse_gpx(gpx_content) except GPXException as err: log.exception(f"Invalid GPX Data: {err}") return identifier = get_identifier(gpxpy_instance) try: instance = GpxModel.objects.get_by_identifier(identifier) except GpxModel.DoesNotExist: log.debug("Create new track for user: %s", user) instance = GpxModel.objects.create(gpx=gpx_content, tracked_by=user) return instance else: if instance.tracked_by != user: log.error("Skip existing track: %s (Tracked by: %s)", instance, instance.tracked_by) else: log.info("Skip existing track: %s", instance) return
def add_gpx(self, gpx_content, user): """ Create a new for_runners.models.GpxModel entry :param gpx_content: String content of the new gpx file :return: GpxModel instance """ try: gpxpy_instance = parse_gpx(gpx_content) except GPXException as err: log.exception("Invalid GPX Data: %s" % err) return identifier = get_identifier(gpxpy_instance) qs = self.get_queryset() try: instance = qs.get_by_identifier(identifier) except self.model.DoesNotExist: log.debug("Create new track for user: %s", user) instance = self.create( gpx=gpx_content, tracked_by=user, ) return instance else: log.info("Skip existing track: %s", instance) return
def get_gpxpy_instance(self): try: return self._GPXPY_CACHE[self.pk] except KeyError: if self.gpx: gpxpy_instance = parse_gpx(content=self.gpx) if self.pk is not None: self._GPXPY_CACHE[self.pk] = gpxpy_instance return gpxpy_instance