fargo/fargo/oauth2/management/commands/oauth2-put-document.py

49 lines
2.0 KiB
Python

# fargo - document box
# Copyright (C) 2016-2019 Entr'ouvert
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import os
from django.core.management.base import BaseCommand
from django.core.files.base import ContentFile
from django.core.urlresolvers import reverse
from fargo.utils import make_url
from fargo.fargo.models import Document
from fargo.oauth2.models import OAuth2TempFile, OAuth2Client
class Command(BaseCommand):
help = 'Push documents inside fargo, returns URLs'
def add_arguments(self, parser):
parser.add_argument('--client-id', type=int)
parser.add_argument('redirect_uri')
parser.add_argument('paths', nargs='+')
def handle(self, redirect_uri, paths, client_id, **options):
client = OAuth2Client.objects.get(id=client_id)
for path in paths:
with open(path, 'rb') as file_object:
filename = os.path.basename(path)
f = ContentFile(file_object.read(), name=filename)
document = Document.objects.get_by_file(f)
oauth2_document = OAuth2TempFile.objects.create(
client=client,
document=document,
filename=filename)
uri = reverse('oauth2-put-document-authorize', args=[oauth2_document.pk])
self.stdout.write('https://localhost:8000' + make_url(uri, redirect_uri=redirect_uri))