Poprawienie requirements.txt (w tym podwyższenie wymaganej wersji Django do 1.1.1...
[redakcja.git] / apps / api / handlers / manage_handlers.py
1 # -*- encoding: utf-8 -*-
2
3 __author__= "Łukasz Rekucki"
4 __date__ = "$2009-09-25 15:49:50$"
5 __doc__ = "Module documentation."
6
7 from piston.handler import BaseHandler, AnonymousBaseHandler
8
9 from api.utils import hglibrary
10 from explorer.models import PullRequest
11 from api.response import *
12
13 class PullRequestListHandler(BaseHandler):
14     allowed_methods = ('GET',)
15
16     def read(self, request):
17         if request.user.has_perm('explorer.book.can_share'):
18             return PullRequest.objects.all()
19         else:
20             return PullRequest.objects.filter(commiter=request.user)
21
22
23 class PullRequestHandler(BaseHandler):
24     allowed_methods = ('GET', 'PUT')
25
26     def read(self, request, prq_id):
27         return PullRequest.objects.get(id=prq_id)
28
29     def update(self, request, prq_id):
30         """Change the status of request"""
31
32         if not request.user.has_perm('explorer.document.can_share'):
33             return AccessDenied().django_response("Insufficient priviliges")
34         
35         prq = PullRequest.objects.get(id=prq_id)
36
37         if not prq:
38             return EntityNotFound().django_response()
39
40
41         action = request.PUT.get('action', None)
42
43         if action == 'accept' and prq.status == 'N':
44             return self.accept_merge(prq)
45         elif action == 'reject' and prq.status in ['N', 'R']:
46             return self.reject_merge(prq)
47         else:
48             return BadRequest().django_response()
49
50
51     @hglibrary
52     def accept_merge(self, prq, lib):        
53         doc = lib.document( prq.document )
54         udoc = doc.take( prq.comitter.username )
55         success, changed = udoc.share(prq.comment)
56
57         if not success:
58             return EntityConflict().django_response()
59
60         doc = doc.latest()
61
62         prq.status = 'A'
63         prq.merged_revisions = unicode(doc.revision)
64         prq.save()
65         
66         return SuccessAllOk().django_response({
67             'status': prq.status
68         })
69
70     
71     def reject_merge(self, prq, lib):
72         prq.status = 'R'
73         prq.save()
74
75         return SuccessAllOk().django_response({
76             'status': prq.status
77         })
78         
79
80         
81
82
83         
84