Skip to content

Commit

Permalink
WIP: start working on #25
Browse files Browse the repository at this point in the history
  • Loading branch information
b1rger committed Apr 24, 2023
1 parent 15539b8 commit 5f53f5e
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 3 deletions.
20 changes: 20 additions & 0 deletions apis_bibsonomy/api_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,29 @@
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework import viewsets, generics

from .models import Reference
from .utils import BibsonomyEntry
from .serializers import ReferenceSerializer, ReferenceQuerySerializer

class ReferenceList(APIView):
"""
API endpoint that allows references to be viewed.
"""
authentication_classes = []
permission_classes = []

def post(self, request, format=None):
serializer = ReferenceQuerySerializer(data=request.data, many=True)
references = []
if serializer.is_valid():
for item in serializer.validated_data:
print(item)
references.extend(list(Reference.objects.filter(**item)))
print(references)
reference_data = ReferenceSerializer(references, many=True).data
return Response(reference_data)


class SaveBibsonomyEntry(APIView):
Expand Down
12 changes: 12 additions & 0 deletions apis_bibsonomy/serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from .models import Reference
from rest_framework import serializers


class ReferenceSerializer(serializers.ModelSerializer):
class Meta:
model = Reference
fields = '__all__'

class ReferenceQuerySerializer(serializers.Serializer):
content_type_id = serializers.IntegerField()
object_id = serializers.IntegerField()
21 changes: 21 additions & 0 deletions apis_bibsonomy/templates/apis_bibsonomy/reference_list.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{% extends "base.html" %}
{% load bibsonomy_templatetags %}

{% block content %}
<ul>
{% for reference in object_list %}
<li>
{% with reference.bibtex|json_obj as bibtex %}
{{ bibtex.title }}
{{ reference.pages_start }}
{{ reference.pages_end }}
{{ reference.folio }}
{{ reference.notes }}
{{ reference.id }}
{% endwith %}
</li>
{% empty %}
<li>No references yet.</li>
{% endfor %}
</ul>
{% endblock content %}
9 changes: 9 additions & 0 deletions apis_bibsonomy/templates/base.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!DOCTYPE html>
<html>
<head>
<title>Bibsonomy</title>
</head>
<body>
{% block content %}{% endblock content %}
</body>
</html>
6 changes: 6 additions & 0 deletions apis_bibsonomy/templatetags/bibsonomy_templatetags.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import json

from django import template
from apis_bibsonomy.forms import ReferenceForm

Expand All @@ -16,3 +18,7 @@ def bibsonomy_list(content_type=None, object_pk=None, attribute_name=None):
c_dict = {'content_type': content_type, 'object_pk': object_pk, 'attribute_name': attribute_name}
form = ReferenceForm(**c_dict)
return {"form": form}

@register.filter
def json_obj(value):
return json.loads(value)
8 changes: 5 additions & 3 deletions apis_bibsonomy/urls.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
from rest_framework.routers import DefaultRouter
from django.urls import path
from . import api_views
from . import api_views, views
from . import autocompletes

app_name = 'apis_bibsonomy'

urlpatterns = [
path('save_get/', api_views.SaveBibsonomyEntry.as_view(), name='savegetbibsonomyentry'),
path('autocomplete/', autocompletes.BibsonomyAutocomplete.as_view(), name='bibsonomyautocomplete')
path('autocomplete/', autocompletes.BibsonomyAutocomplete.as_view(), name='bibsonomyautocomplete'),
path('references/', views.ReferenceListView.as_view(), name='referenceslist'),
path('references/<int:id>', views.ReferenceDetailView.as_view(), name='referencedetail'),
]

20 changes: 20 additions & 0 deletions apis_bibsonomy/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from django.views.generic.list import ListView
from django.db.models import Q

from .models import Reference


class ReferenceListView(ListView):
def get_queryset(self):
ctois = self.request.GET.getlist("ctoi")
q_objects = Q()
for ctoi in ctois:
ctoi = ctoi.split(":")
if len(ctoi) == 2:
q_objects |= Q(content_type__id=ctoi[0], object_id=ctoi[1])
refs = Reference.objects.filter(q_objects)
return refs


class ReferenceDetailView(DetailView):
model = Reference

0 comments on commit 5f53f5e

Please sign in to comment.