# Path: sqlviewer/glimpse/models.py
# class Version(models.Model):
#     number = models.IntegerField()
#     label = models.CharField(max_length=128, blank=False, null=False)
#     created_at = models.DateTimeField(auto_now_add=True)
#     model = models.ForeignKey(Model)
# 
#     def diagrams(self):
#         return Diagram.objects.filter(model_version=self).all()
# 
#     def save(self, *args, **kwargs):
#         if not self.number:
#             self.number = Version.objects.filter(model=self.model).count()
#         super(Version, self).save(*args, **kwargs)
# 
#     def to_json(self, shallow=False):
#         data = {"id": self.id,
#                 "number": self.number,
#                 "label": self.label}
#         if not shallow:
#             data['diagrams'] = [d.to_json(shallow=True) for d in self.diagrams()]
#         return data
# 
# class Table(UUIDModel):
#     name = models.CharField(max_length=128, blank=False, null=False)
#     comment = models.TextField(blank=True, null=True)
#     model_version = models.ForeignKey(Version)
# 
#     def columns(self):
#         return Column.objects.filter(table=self)
# 
#     def to_json(self, shallow=False):
#         data = {
#             "id": str(self.extid),
#             "name": self.name,
#             "comment": self.comment
#         }
# 
#         if not shallow:
#             data["columns"] = [col.to_json() for col in self.columns()]
#         return data
# 
# class Diagram(UUIDModel):
#     name = models.CharField(max_length=128, blank=False, null=False)
#     model_version = models.ForeignKey(Version)
# 
#     def layer_elements(self):
#         return LayerElement.objects.filter(diagram=self)
# 
#     def connection_elements(self):
#         return ConnectionElement.objects.filter(diagram=self)
# 
#     def table_elements(self):
#         return TableElement.objects.filter(layer_element__diagram=self)
# 
#     def to_json(self, shallow=False):
#         data = {'id': str(self.extid),
#                 'name': self.name}
#         if not shallow:
#             data['layers'] = [layer.to_json() for layer in self.layer_elements()]
#             data['connections'] = [conn.to_json() for conn in self.connection_elements()]
#             data['data'] = {
#                 'tables': [tel.table.to_json() for tel in self.table_elements()],
#                 'foreignKeys': [cel.foreignKey.to_json() for cel in self.connection_elements()]
#             }
#         return data
# 
# class Model(UUIDModel):
#     name = models.CharField(max_length=128, blank=False, null=False)
# 
#     def latest_version(self):
#         """
#         Returns the latest version of this model
#         :rtype: Version
#         """
#         return Version.objects.filter(model=self).order_by('-number').first()
# 
#     def versions(self):
#         """
#         Returns the list of versions bound to this model
#         :return: list of versions
#         :rtype: list of Version
#         """
#         return Version.objects.filter(model=self).all()
# 
#     def to_json(self, shallow=True):
#         data = {
#             "id": str(self.extid),
#             "name": self.name,
#         }
#         if not shallow:
#             data['versions'] = [v.to_json(shallow=True) for v in Version.objects.filter(model=self).all()]
#         return data
# 
#     def __str__(self):
#         return "{0}".format(self.name)
# 
# Path: sqlviewer/glimpse/services.py
# def get_diagram_data(diagram: Diagram) -> dict:
#     """
#     Get diagram data method for retrieval of diagram dictionary (json data)
#     Is cached to increase performance
#     :param Diagram diagram:
#     :return dict: diagram hierarchy with embedded json data
#     """
#     data = cache.get(diagram.id)
#     if not data:
#         data = diagram.to_json()
#         cache.set(diagram.id, data)
#     return data


from django.db.models import Q
from django.shortcuts import get_list_or_404
from rest_framework import status
from rest_framework.decorators import api_view
from rest_framework.generics import get_object_or_404
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework.views import APIView
from sqlviewer.glimpse.models import Version, Table, Diagram, Model
from sqlviewer.glimpse.services import get_diagram_data
    max_results = max(1, min(int(request.GET.get("size", 0)), 20))
    filters = {"model_version": version}
    if request.GET.get('name'):
        filters["name__contains"] = request.GET['name']

    results = Table.objects.filter(**filters)[:max_results]

    data = [t.to_json(shallow=True) for t in results]
    return Response(data=data, status=status.HTTP_200_OK)


class DiagramView(APIView):
    permission_classes = (IsAuthenticated,)

    def get(self, request, model_id, version_id, diagram_id, format=None):
        if not diagram_id:
            diagram_list = get_list_or_404(Diagram, model_version__id=version_id)
            data = [d.to_json() for d in diagram_list]
        else:
            diagram = get_object_or_404(Diagram, id=diagram_id)
            data = get_diagram_data(diagram)

        return Response(data=data, status=status.HTTP_200_OK)


class VersionView(APIView):
    permission_classes = (IsAuthenticated,)

    def get(self, request, model_id, version_id, format=None):
        if not version_id:

# Path: sqlviewer/glimpse/models.py
# class Version(models.Model):
#     number = models.IntegerField()
#     label = models.CharField(max_length=128, blank=False, null=False)
#     created_at = models.DateTimeField(auto_now_add=True)
#     model = models.ForeignKey(Model)
# 
#     def diagrams(self):
#         return Diagram.objects.filter(model_version=self).all()
# 
#     def save(self, *args, **kwargs):
#         if not self.number:
#             self.number = Version.objects.filter(model=self.model).count()
#         super(Version, self).save(*args, **kwargs)
# 
#     def to_json(self, shallow=False):
#         data = {"id": self.id,
#                 "number": self.number,
#                 "label": self.label}
#         if not shallow:
#             data['diagrams'] = [d.to_json(shallow=True) for d in self.diagrams()]
#         return data
# 
# class Table(UUIDModel):
#     name = models.CharField(max_length=128, blank=False, null=False)
#     comment = models.TextField(blank=True, null=True)
#     model_version = models.ForeignKey(Version)
# 
#     def columns(self):
#         return Column.objects.filter(table=self)
# 
#     def to_json(self, shallow=False):
#         data = {
#             "id": str(self.extid),
#             "name": self.name,
#             "comment": self.comment
#         }
# 
#         if not shallow:
#             data["columns"] = [col.to_json() for col in self.columns()]
#         return data
# 
# class Diagram(UUIDModel):
#     name = models.CharField(max_length=128, blank=False, null=False)
#     model_version = models.ForeignKey(Version)
# 
#     def layer_elements(self):
#         return LayerElement.objects.filter(diagram=self)
# 
#     def connection_elements(self):
#         return ConnectionElement.objects.filter(diagram=self)
# 
#     def table_elements(self):
#         return TableElement.objects.filter(layer_element__diagram=self)
# 
#     def to_json(self, shallow=False):
#         data = {'id': str(self.extid),
#                 'name': self.name}
#         if not shallow:
#             data['layers'] = [layer.to_json() for layer in self.layer_elements()]
#             data['connections'] = [conn.to_json() for conn in self.connection_elements()]
#             data['data'] = {
#                 'tables': [tel.table.to_json() for tel in self.table_elements()],
#                 'foreignKeys': [cel.foreignKey.to_json() for cel in self.connection_elements()]
#             }
#         return data
# 
# class Model(UUIDModel):
#     name = models.CharField(max_length=128, blank=False, null=False)
# 
#     def latest_version(self):
#         """
#         Returns the latest version of this model
#         :rtype: Version
#         """
#         return Version.objects.filter(model=self).order_by('-number').first()
# 
#     def versions(self):
#         """
#         Returns the list of versions bound to this model
#         :return: list of versions
#         :rtype: list of Version
#         """
#         return Version.objects.filter(model=self).all()
# 
#     def to_json(self, shallow=True):
#         data = {
#             "id": str(self.extid),
#             "name": self.name,
#         }
#         if not shallow:
#             data['versions'] = [v.to_json(shallow=True) for v in Version.objects.filter(model=self).all()]
#         return data
# 
#     def __str__(self):
#         return "{0}".format(self.name)
# 
# Path: sqlviewer/glimpse/services.py
# def get_diagram_data(diagram: Diagram) -> dict:
#     """
#     Get diagram data method for retrieval of diagram dictionary (json data)
#     Is cached to increase performance
#     :param Diagram diagram:
#     :return dict: diagram hierarchy with embedded json data
#     """
#     data = cache.get(diagram.id)
#     if not data:
#         data = diagram.to_json()
#         cache.set(diagram.id, data)
#     return data


# Path: sqlviewer/glimpse/api.py
from django.db.models import Q
from django.shortcuts import get_list_or_404
from rest_framework import status
from rest_framework.decorators import api_view
from rest_framework.generics import get_object_or_404
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework.views import APIView
from sqlviewer.glimpse.models import Version, Table, Diagram, Model
from sqlviewer.glimpse.services import get_diagram_data

    max_results = max(1, min(int(request.GET.get("size", 0)), 20))
    filters = {"model_version": version}
    if request.GET.get('name'):
        filters["name__contains"] = request.GET['name']

    results = Table.objects.filter(**filters)[:max_results]

    data = [t.to_json(shallow=True) for t in results]
    return Response(data=data, status=status.HTTP_200_OK)


class DiagramView(APIView):
    permission_classes = (IsAuthenticated,)

    def get(self, request, model_id, version_id, diagram_id, format=None):
        if not diagram_id:
            diagram_list = get_list_or_404(Diagram, model_version__id=version_id)
            data = [d.to_json() for d in diagram_list]
        else:
            diagram = get_object_or_404(Diagram, id=diagram_id)
            data = get_diagram_data(diagram)

        return Response(data=data, status=status.HTTP_200_OK)


class VersionView(APIView):
    permission_classes = (IsAuthenticated,)

    def get(self, request, model_id, version_id, format=None):
        if not version_id:

            version_list = get_list_or_404(Version, model__id=model_id)