|
1 |
# fargo - document box
|
|
2 |
# Copyright (C) 2015-2016 Entr'ouvert
|
|
3 |
#
|
|
4 |
# This program is free software: you can redistribute it and/or modify it
|
|
5 |
# under the terms of the GNU Affero General Public License as published
|
|
6 |
# by the Free Software Foundation, either version 3 of the License, or
|
|
7 |
# (at your option) any later version.
|
|
8 |
#
|
|
9 |
# This program is distributed in the hope that it will be useful,
|
|
10 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
11 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
12 |
# GNU Affero General Public License for more details.
|
|
13 |
#
|
|
14 |
# You should have received a copy of the GNU Affero General Public License
|
|
15 |
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
16 |
|
|
17 |
import base64
|
|
18 |
|
|
19 |
from django.conf import settings
|
|
20 |
from django.core.files.base import ContentFile
|
|
21 |
from django.contrib.auth.models import User
|
|
22 |
from django.utils.text import slugify
|
|
23 |
|
|
24 |
from rest_framework import serializers, authentication
|
|
25 |
from rest_framework.decorators import permission_classes
|
|
26 |
from rest_framework.generics import GenericAPIView
|
|
27 |
from rest_framework.permissions import IsAdminUser
|
|
28 |
from rest_framework.response import Response
|
|
29 |
from rest_framework import permissions, status
|
|
30 |
|
|
31 |
from .models import Origin, Document, UserDocument, Validation
|
|
32 |
|
|
33 |
try:
|
|
34 |
from mellon.models import UserSAMLIdentifier
|
|
35 |
except ImportError:
|
|
36 |
UserSAMLIdentifier = None
|
|
37 |
|
|
38 |
|
|
39 |
class PushDocumentSerializer(serializers.Serializer):
|
|
40 |
origin = serializers.CharField(required=True)
|
|
41 |
file_b64_content = serializers.CharField(required=True)
|
|
42 |
file_name = serializers.CharField(required=False)
|
|
43 |
document_type = serializers.ChoiceField(required=True,
|
|
44 |
choices=[x.get('name') for x in settings.FARGO_DOCUMENT_TYPES])
|
|
45 |
user_email = serializers.CharField(required=False)
|
|
46 |
user_nameid = serializers.CharField(required=False)
|
|
47 |
|
|
48 |
def validate(self, data):
|
|
49 |
if not (data.get('user_email') or data.get('user_nameid')):
|
|
50 |
raise serializers.ValidationError('user must be specified')
|
|
51 |
return data
|
|
52 |
|
|
53 |
def validate_user_email(self, value):
|
|
54 |
try:
|
|
55 |
user = User.objects.get(email=value)
|
|
56 |
except User.DoesNotExist:
|
|
57 |
raise serializers.ValidationError('unknown user')
|
|
58 |
return value
|
|
59 |
|
|
60 |
def validate_user_nameid(self, value):
|
|
61 |
if UserSAMLIdentifier is None:
|
|
62 |
raise serializers.ValidationError('nameid lookups require django-mellon')
|
|
63 |
try:
|
|
64 |
UserSAMLIdentifier.objects.get(name_id=value)
|
|
65 |
except UserSAMLIdentifier.DoesNotExist:
|
|
66 |
raise serializers.ValidationError('unknown user')
|
|
67 |
return value
|
|
68 |
|
|
69 |
|
|
70 |
class PushDocument(GenericAPIView):
|
|
71 |
serializer_class = PushDocumentSerializer
|
|
72 |
permission_classes = (IsAdminUser,)
|
|
73 |
|
|
74 |
def post(self, request, format=None):
|
|
75 |
serializer = self.get_serializer(data=request.data)
|
|
76 |
if not serializer.is_valid():
|
|
77 |
response = {'result': 0, 'errors': serializer.errors}
|
|
78 |
return Response(response, status.HTTP_400_BAD_REQUEST)
|
|
79 |
|
|
80 |
data = serializer.validated_data
|
|
81 |
if data.get('user_email'):
|
|
82 |
user = User.objects.get(email=data.get('user_email'))
|
|
83 |
elif data.get('user_nameid'):
|
|
84 |
user = UserSAMLIdentifier.objects.get(name_id=data.get('user_nameid')).user
|
|
85 |
|
|
86 |
origin, created = Origin.objects.get_or_create(
|
|
87 |
slug=slugify(data.get('origin')),
|
|
88 |
defaults={'label': data.get('origin')})
|
|
89 |
|
|
90 |
document_file = ContentFile(base64.decodestring(data.get('file_b64_content')))
|
|
91 |
if data.get('file_name'):
|
|
92 |
document_file.name = data.get('file_name')
|
|
93 |
document = Document(content=document_file)
|
|
94 |
document.save()
|
|
95 |
|
|
96 |
user_document = UserDocument(
|
|
97 |
user=user,
|
|
98 |
filename=data.get('file_name'),
|
|
99 |
document=document,
|
|
100 |
origin=origin)
|
|
101 |
user_document.save()
|
|
102 |
|
|
103 |
response = {'result': 1}
|
|
104 |
response_status = status.HTTP_200_OK
|
|
105 |
return Response(response, response_status)
|
|
106 |
|
|
107 |
push_document = PushDocument.as_view()
|