Merge branch 'master' of stigma:platforma
[redakcja.git] / apps / explorer / views.py
1 # -*- coding: utf-8 -*-
2 import urllib2
3
4 import logging
5 log = logging.getLogger('platforma.explorer.views')
6
7 from django.conf import settings
8 from django.contrib.auth.decorators import login_required, permission_required
9
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
15
16 from api.models import PullRequest
17
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)
24         # not authenticated
25         return HttpResponse( json.dumps({'result': 'access_denied', 'errors': ['Brak dostÄ™pu.']}) );
26     return view_with_auth
27
28 @login_required
29 def display_editor(request, path):
30     user = request.GET.get('user', request.user.username)
31     log.info(user)
32     
33     return direct_to_template(request, 'explorer/editor.html', extra_context={
34             'fileid': path,
35             'euser': user
36     })
37     
38 #
39 # View all files
40 #
41 def file_list(request):   
42     import api.forms
43     from api.resources import library_resource
44
45     bookform = api.forms.DocumentUploadForm()
46
47     # short-circut the api document list
48     doctree = library_resource.handler.read(request)
49     # print "DOCTREE:", doctree['documents']
50         
51     return direct_to_template(request, 'explorer/file_list.html', extra_context={
52         'filetree': doctree['documents'], 'bookform': bookform,
53     })
54
55 @permission_required('api.document.can_add')
56 def file_upload(request, repo):
57     from api.resources import library_resource
58     from api.forms import DocumentUploadForm
59     from django.http import HttpRequest, HttpResponseRedirect
60
61     response = library_resource.handler.create(request)
62
63     if isinstance(response, HttpResponse):
64         data = json.loads(response.content)
65         
66         if response.status_code == 201:
67             return HttpResponseRedirect( \
68                 reverse("editor_view", args=[ data['name'] ]) )
69         else:
70             bookform = DocumentUploadForm(request.POST, request.FILES)
71             bookform.is_valid()
72             
73             return direct_to_template(request, 'explorer/file_upload.html',
74                 extra_context={'bookform': bookform } )
75           
76
77 @login_required
78 def print_html(request, **kwargs):
79     from api.resources import document_html_resource
80
81     kwargs['stylesheet'] = 'legacy'
82     
83     output = document_html_resource.handler.read(request, **kwargs)
84
85     if isinstance(output, HttpResponse):
86         # errors = json.loads(output.content)
87         output.mimetype = "text/plain"
88         return output
89     
90     return direct_to_template(request, 'html4print.html',
91         extra_context={'output': output, 'docid': kwargs['docid']},
92         mimetype="text/html" )
93
94
95 def _add_references(message, issues):
96     return message + " - " + ", ".join(map(lambda issue: "Refs #%d" % issue['id'], issues))
97
98 def _get_issues_for_file(fileid):
99     uf = None
100     try:
101         uf = urllib2.urlopen(settings.REDMINE_URL + 'publications/issues/%s.json' % fileid)
102         return json.loads(uf.read())
103     except urllib2.HTTPError:
104         return []
105     finally:
106         if uf: uf.close()
107
108 # =================
109 # = Pull requests =
110 # =================
111 def pull_requests(request):    
112     objects = PullRequest.objects.order_by('status')
113
114     if not request.user.has_perm('explorer.book.can_share'):
115         objects = objects.filter(comitter=request.user)
116
117     
118     return direct_to_template(request, 'manager/pull_request.html', 
119         extra_context = {'objects': objects} )