from django.db import models from django_filters import rest_framework as filters from urllib.parse import urlsplit class DataLocation(models.Model): name = models.CharField(max_length=50, primary_key=True) uri = models.CharField(max_length=200) @staticmethod def insert_location_from_string(location_string): """ Insert a datalocation from a srm string (e.g. srm://surm:4321/path.tar) :param str location_string: SRM url :return: DataLocation object rtype: DataLocations """ _, netloc, *_ = urlsplit(location_string) dataloc = DataLocation(name=netloc, uri=location_string.rstrip('/')) dataloc.save() return dataloc class DataProduct(models.Model): obs_id = models.CharField(verbose_name="OBS_ID", max_length=15) oid_source = models.CharField(verbose_name='OBS_ID_SOURCE', max_length=15) dataproduct_source = models.CharField(max_length=20) dataproduct_type = models.CharField(max_length=50) project = models.CharField(max_length=15) location = models.ForeignKey(DataLocation, on_delete=models.DO_NOTHING) activity = models.CharField(max_length=50) surl = models.CharField(max_length=200) filesize = models.PositiveBigIntegerField() additional_meta = models.JSONField() @staticmethod def insert_dataproduct(obs_id, oid_source, dataproduct_source, dataproduct_type, project, activity, surl, filesize, additional_meta): scheme, netloc, *_ = urlsplit(surl) dp = DataProduct(obs_id=obs_id, oid_source=oid_source, dataproduct_source=dataproduct_source, dataproduct_type=dataproduct_type, project=project, location=DataLocation.insert_location_from_string('://'.join((scheme, netloc))), activity=activity, filesize=filesize, additional_meta=additional_meta, surl=surl ) dp.save() return dp class DataProductFilter(models.Model): field = models.CharField(max_length=20) name = models.CharField(max_length=20) lookup_type = models.CharField(max_length=100)