Skip to content
Snippets Groups Projects
Commit 61c65378 authored by Mattia Mancini's avatar Mattia Mancini
Browse files

Add DataproductFilter and Rename Dataproduct

parent 1c8948de
No related branches found
No related tags found
1 merge request!1Add datamodel
Pipeline #33420 canceled
from django.contrib import admin from django.contrib import admin
# Register your models here. # Register your models here.
from .models import LofarData from .models import DataProduct, DataProductFilter
admin.site.register(LofarData)
\ No newline at end of file admin.site.register(DataProduct)
admin.site.register(DataProductFilter)
from django.db import models from django.db import models
from django_filters import rest_framework as filters
class LofarData(models.Model):
sas_id = models.CharField(verbose_name="SAS_ID", max_length=15, blank=True, null=True)
def __str__(self):
return self.sas_id class DataLocations(models.Model):
name = models.CharField(max_length=50, primary_key=True)
uri = models.CharField(max_length=200)
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(DataLocations, on_delete=models.DO_NOTHING)
activity = models.CharField(max_length=50)
surl = models.CharField(max_length=200)
filesize = models.PositiveBigIntegerField()
additional_meta = models.JSONField()
class DataProductFilter(models.Model):
field = models.CharField(max_length=20)
name = models.CharField(max_length=20)
lookup_type = models.CharField(max_length=100)
from rest_framework import serializers from rest_framework import serializers
from .models import LofarData from .models import DataProduct
class LofarDataSerializer(serializers.ModelSerializer): class DataProductSerializer(serializers.ModelSerializer):
class Meta: class Meta:
model = LofarData model = DataProduct
fields = "__all__" fields = "__all__"
...@@ -34,7 +34,7 @@ ...@@ -34,7 +34,7 @@
</li> </li>
<li><a class="nav-link" href="{% url 'index' %}">Home</a></li> <li><a class="nav-link" href="{% url 'index' %}">Home</a></li>
<li><a class="nav-link" href="{% url 'lofardata' %}">LOFAR Data</a></li> <li><a class="nav-link" href="{% url 'dataproduct' %}">LOFAR Data</a></li>
{% if user.is_authenticated %} {% if user.is_authenticated %}
<a class="nav-link" href="{% url 'logout' %}" target="_blank">Logout {{ user.get_username }}</a> <a class="nav-link" href="{% url 'logout' %}" target="_blank">Logout {{ user.get_username }}</a>
......
...@@ -9,7 +9,8 @@ ...@@ -9,7 +9,8 @@
<tbody> <tbody>
<tr><td>atdb_host</td><td>{{ atdb_host }}</td></tr> <tr><td>atdb_host</td><td>{{ atdb_host }}</td></tr>
<tr><td>api</td><td><a href="{% url 'lofardata' %}">{% url 'lofardata' %}</a></td></tr> <tr><td>api</td><td><a href="{% url 'dataproduct' %}">{% url 'dataproduct' %}</a></td></tr>
<tr><td>api-schema</td><td><a href="{% url 'openapi-schema' %}">{% url 'openapi-schema' %}</a></td></tr>
</tbody> </tbody>
</table> </table>
......
from django.urls import include, path from django.urls import include, path
from django.contrib.auth import views as auth_views from django.contrib.auth import views as auth_views
from rest_framework.authtoken import views as rest_auth_views from rest_framework.authtoken import views as rest_auth_views
from rest_framework.schemas import get_schema_view
from . import views from . import views
...@@ -13,8 +14,12 @@ urlpatterns = [ ...@@ -13,8 +14,12 @@ urlpatterns = [
path('login/', auth_views.LoginView.as_view(template_name='registration/login.html')), path('login/', auth_views.LoginView.as_view(template_name='registration/login.html')),
# REST API # REST API
path('lofardata/', views.LofarDataView.as_view(), name='lofardata'), path('data/', views.DataProductView.as_view(), name='dataproduct'),
path('openapi/', get_schema_view(
title="LDV Specification",
description="API description",
version="0.0.1"
), name='openapi-schema'),
# GUI # GUI
#path('', views.IndexView.as_view(), name='index'), #path('', views.IndexView.as_view(), name='index'),
path('', views.index, name='index'), path('', views.index, name='index'),
......
from django.shortcuts import render
from django.shortcuts import render, redirect, reverse
from django.views.generic import ListView
from django.conf import settings from django.conf import settings
from rest_framework import generics, pagination from rest_framework import generics, pagination
from rest_framework.views import APIView
import django_filters
from django_filters import rest_framework as filters from django_filters import rest_framework as filters
from .models import LofarData from .models import DataProduct, DataProductFilter
from .serializers import LofarDataSerializer from .serializers import DataProductSerializer
# --- Filters ---
class LofarDataFilter(filters.FilterSet): class DynamicFilterSet(filters.FilterSet):
class Meta: class Meta:
model = LofarData filter_class = None
def __init__(self, *args, **kwargs):
self._load_filters()
super().__init__(*args, **kwargs)
def _load_filters(self):
print('Current filters are', self.base_filters)
if self.Meta.filter_class is None:
raise Exception('Define filter_class meta attribute')
print('Filter to be added', self.Meta.filter_class.objects.all())
for item in self.Meta.filter_class.objects.all():
field_obj = self.Meta.model._meta.get_field(item.field)
filter_class, *_ = self.filter_for_lookup(field_obj, item.lookup_type)
self.base_filters[item.name] = filter_class(item.field)
print('Final filters are', self.base_filters)
# --- Filters ---
class DataProductFilterSet(DynamicFilterSet):
class Meta:
model = DataProduct
filter_class = DataProductFilter
fields = { fields = {
'sas_id': ['exact', 'icontains'], 'obs_id': ['exact', 'icontains'],
} }
# ---------- GUI Views ----------- # ---------- GUI Views -----------
def index(request): def index(request):
...@@ -30,12 +49,15 @@ def index(request): ...@@ -30,12 +49,15 @@ def index(request):
# ---------- REST API views ---------- # ---------- REST API views ----------
class LofarDataView(generics.ListCreateAPIView): class DataProductView(generics.ListCreateAPIView):
model = LofarData model = DataProduct
serializer_class = LofarDataSerializer serializer_class = DataProductSerializer
queryset = LofarData.objects.all().order_by('sas_id') queryset = DataProduct.objects.all().order_by('obs_id')
# using the Django Filter Backend - https://django-filter.readthedocs.io/en/latest/index.html # using the Django Filter Backend - https://django-filter.readthedocs.io/en/latest/index.html
filter_backends = (filters.DjangoFilterBackend,) filter_backends = (filters.DjangoFilterBackend,)
filter_class = LofarDataFilter filter_class = DataProductFilterSet().__class__
def get_queryset(self):
return self.queryset.order_by('obs_id')
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment