pyazo: add API

parent 1665390f
Pipeline #3267 passed with stages
in 4 minutes and 38 seconds
"""django API AppConfig"""
from django.apps.config import AppConfig
class PyazoAPIConfig(AppConfig):
"""django API AppConfig"""
name = 'pyazo.api'
label = 'pyazo_api'
"""pyazo API Serializers"""
from django.contrib.auth.models import User
from django.shortcuts import reverse
from rest_framework.serializers import (HyperlinkedModelSerializer,
ReadOnlyField, SerializerMethodField)
from pyazo.core.models import Collection, Object, ObjectView
class UserSerializer(HyperlinkedModelSerializer):
"""User serializer (show basic information)"""
class Meta:
model = User
fields = ['first_name', 'last_name', 'username']
class ObjectSerializer(HyperlinkedModelSerializer):
"""Object serializer (showing URL to file and thumbnail)"""
file_url = SerializerMethodField()
thumbnail_url = SerializerMethodField()
md5 = ReadOnlyField()
sha256 = ReadOnlyField()
sha512 = ReadOnlyField()
mime_type = ReadOnlyField()
def get_file_url(self, obj):
"""Build absolute URL based on sha512"""
return self.context['request'].build_absolute_uri(
reverse('view_sha512', kwargs={'file_hash': obj.sha512}))
def get_thumbnail_url(self, obj):
"""Build absolute URL based on sha512"""
return self.context['request'].build_absolute_uri(
reverse('view_sha512', kwargs={'file_hash': obj.sha512})+'?thumb')
class Meta:
model = Object
fields = ['file', 'file_url', 'thumbnail_url', 'md5',
'sha256', 'sha512', 'collection', 'mime_type']
class ObjectViewSerializer(HyperlinkedModelSerializer):
"""Show basic information about View"""
class Meta:
model = ObjectView
fields = ['obj', 'viewee', 'viewee_ip', 'viewee_dns', 'viewee_date', 'viewee_user_agent']
class CollectionSerializer(HyperlinkedModelSerializer):
"""Collection Serializer"""
class Meta:
model = Collection
fields = ['name', 'owner']
"""pyazo API Urls"""
from django.urls import include, path
from rest_framework.routers import DefaultRouter
from rest_framework_swagger.views import get_swagger_view
from pyazo.api.viewsets import (CollectionViewSet, ObjectViewsSet,
ObjectViewViewSet, UserViewSet)
ROUTER = DefaultRouter()
ROUTER.register('objects', ObjectViewsSet)
ROUTER.register('object_views', ObjectViewViewSet)
ROUTER.register('collections', CollectionViewSet)
ROUTER.register('users', UserViewSet)
urlpatterns = [
path('v1/', include(ROUTER.urls)),
path('swagger/', get_swagger_view(title='pyazo API'))
]
"""pyazo API Viewsets"""
from django.contrib.auth.models import User
from rest_framework.viewsets import ModelViewSet, ReadOnlyModelViewSet
from pyazo.api.serializers import (CollectionSerializer, ObjectSerializer,
ObjectViewSerializer, UserSerializer)
from pyazo.core.models import Collection, Object, ObjectView
# pylint: disable=too-many-ancestors
class UserViewSet(ModelViewSet):
"""User viewset (only show current user for normal users, otherwise all users)"""
queryset = User.objects.none()
serializer_class = UserSerializer
def get_queryset(self):
if self.request.user.is_superuser:
return User.objects.all()
return User.objects.filter(pk=self.request.user.pk)
# pylint: disable=too-many-ancestors
class ObjectViewsSet(ModelViewSet):
"""Show all Objects if superuser, otherwise only objects owned by current user"""
queryset = Object.objects.all()
serializer_class = ObjectSerializer
def get_queryset(self):
if self.request.user.is_superuser:
return Object.objects.all()
return Object.objects.filter(user=self.request.user)
# pylint: disable=too-many-ancestors
class ObjectViewViewSet(ReadOnlyModelViewSet):
"""Show all Views related"""
queryset = ObjectView.objects.all()
serializer_class = ObjectViewSerializer
def get_queryset(self):
if self.request.user.is_superuser:
return ObjectView.objects.all()
return ObjectView.objects.filter(object_user=self.request.user)
# pylint: disable=too-many-ancestors
class CollectionViewSet(ModelViewSet):
"""Show all collections"""
queryset = Collection.objects.all()
serializer_class = CollectionSerializer
def get_queryset(self):
if self.request.user.is_superuser:
return Collection.objects.all()
return Collection.objects.filter(owner=self.request.user)
......@@ -103,7 +103,10 @@ INSTALLED_APPS = [
'allauth.socialaccount',
'allauth_passbook',
'pyazo.core.apps.PyazoCoreConfig',
'pyazo.api.apps.PyazoAPIConfig',
'raven.contrib.django.raven_compat',
'rest_framework',
'rest_framework_swagger',
]
AUTHENTICATION_BACKENDS = (
......
......@@ -6,13 +6,15 @@ from django.contrib.auth import views
from django.urls import include, path
from django.views.generic.base import RedirectView
from pyazo.api.urls import urlpatterns
from pyazo.core.views import clients, core, upload, view
admin.site.index_title = 'Pyazo Admin'
admin.site.index_title = 'pyazo Admin'
admin.site.site_title = 'pyazo'
urlpatterns = [
path('', RedirectView.as_view(url='overview/')),
path('api/', include(urlpatterns)),
path('overview/', core.IndexView.as_view(), name='index'),
path('admin/', admin.site.urls),
path('accounts/allauth/', include('allauth.urls')),
......
"""pyazo download views"""
from logging import getLogger
import os.path
from logging import getLogger
from urllib.parse import urlparse
from django.conf import settings
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment