1 # -*- coding: utf-8 -*-
5 log = logging.getLogger('platforma.explorer.views')
7 from django.conf import settings
8 from django.contrib.auth.decorators import login_required, permission_required
10 from django.core.urlresolvers import reverse
11 from django.http import HttpResponse
12 from django.utils import simplejson as json
13 from django.views.generic.simple import direct_to_template
14 from django.contrib.auth.decorators import login_required
16 from api.models import PullRequest
18 def ajax_login_required(view):
19 """Similar ro @login_required, but instead of redirect,
20 just return some JSON stuff with error."""
21 def view_with_auth(request, *args, **kwargs):
22 if request.user.is_authenticated():
23 return view(request, *args, **kwargs)
25 return HttpResponse( json.dumps({'result': 'access_denied', 'errors': ['Brak dostępu.']}) );
29 def display_editor(request, path):
30 user = request.GET.get('user', request.user.username)
33 return direct_to_template(request, 'explorer/editor.html', extra_context={
41 def file_list(request):
43 from api.resources import library_resource
45 bookform = api.forms.DocumentUploadForm()
47 # short-circut the api document list
48 doctree = library_resource.handler.read(request)
49 # print "DOCTREE:", doctree['documents']
51 return direct_to_template(request, 'explorer/file_list.html', extra_context={
52 'filetree': doctree['documents'], 'bookform': bookform,
55 @permission_required('api.document.can_add')
56 def file_upload(request):
57 from api.resources import library_resource
58 from api.forms import DocumentUploadForm
59 from django.http import HttpRequest, HttpResponseRedirect
61 response = library_resource.handler.create(request)
63 if isinstance(response, HttpResponse):
64 data = json.loads(response.content)
66 if response.status_code == 201:
67 return HttpResponseRedirect( \
68 reverse("editor_view", args=[ data['name'] ]) )
70 bookform = DocumentUploadForm(request.POST, request.FILES)
73 return direct_to_template(request, 'explorer/file_upload.html',
74 extra_context={'bookform': bookform } )
78 def print_html(request, **kwargs):
79 from api.resources import document_html_resource
81 kwargs['stylesheet'] = 'legacy'
83 output = document_html_resource.handler.read(request, **kwargs)
85 if isinstance(output, HttpResponse):
86 # errors = json.loads(output.content)
87 output.mimetype = "text/plain"
90 return direct_to_template(request, 'html4print.html',
91 extra_context={'output': output, 'docid': kwargs['docid']},
92 mimetype="text/html" )
95 def _add_references(message, issues):
96 return message + " - " + ", ".join(map(lambda issue: "Refs #%d" % issue['id'], issues))
98 def _get_issues_for_file(fileid):
101 uf = urllib2.urlopen(settings.REDMINE_URL + 'publications/issues/%s.json' % fileid)
102 return json.loads(uf.read())
103 except urllib2.HTTPError:
111 def pull_requests(request):
112 objects = PullRequest.objects.order_by('-status', 'timestamp')
114 if not request.user.has_perm('explorer.book.can_share'):
115 objects = objects.filter(comitter=request.user)
117 return direct_to_template(request, 'manager/pull_request.html',
118 extra_context = {'objects': objects} )