Ability to edit feature requests.

portnov [2008-06-14 16:53:50]
Ability to edit feature requests.
Filename
media/css/main.css
mgmt/rights.py
mgmt/views.py
templates/comments.html
templates/delete.html
templates/edit_request.html
templates/request.html
templates/requests.html
urls.py
diff --git a/media/css/main.css b/media/css/main.css
index c073000..0bdd163 100644
--- a/media/css/main.css
+++ b/media/css/main.css
@@ -181,9 +181,8 @@ ul.errorlist {
   padding: 1ex;
 }

-p.comment-title {
+div.comment-title {
   margin-top: 0;
-  font-weight: bold;
   border-bottom: 1px #888 solid;
 }

diff --git a/mgmt/rights.py b/mgmt/rights.py
index 63bea5d..774271e 100644
--- a/mgmt/rights.py
+++ b/mgmt/rights.py
@@ -18,24 +18,31 @@ def can_change_bug_state(user,bug,new_state):
   return False

 def can(user,action,object=None,target=None):
+  cls = object.__class__.__name__
   if action in ['comment','send bug','request']:
     return user.is_authenticated()
   if action == 'edit':
-    if object.__class__.__name__ == 'Project':
+    if cls == 'Project':
       return user in object.admins.iterator()
-    if object.__class__.__name__ == 'Bug':
+    if cls == 'Bug':
+      return user in object.project.team.iterator() or user.id == object.author.id
+    if cls == 'Document':
       return user in object.project.team.iterator()
-    if object.__class__.__name__ == 'Document':
+    if cls == 'Request':
       return user in object.project.team.iterator()
   if action == 'document':
     return user in object.team.iterator()
-  if object.__class__.__name__ == 'PrivateMsg':
+
+  if cls == 'PrivateMsg':
     if action == 'delete':
       return user.id == object.reciever.id
-  if object.__class__.__name__ == 'Request':
+  if cls == 'Comment':
+    if action == 'delete':
+      return user.id == object.author.id or user.is_staff
+  if cls == 'Request':
     if action == 'change_state':
       return user in object.project.admins.iterator()
-  if object.__class__.__name__ == 'Bug':
+  if cls == 'Bug':
     if action == 'set_priority':
       return user in object.project.admins.iterator() and not object.status.id in [1,4,5]
     if action == 'set_complexity':
diff --git a/mgmt/views.py b/mgmt/views.py
index 1c02a21..a924e96 100644
--- a/mgmt/views.py
+++ b/mgmt/views.py
@@ -439,12 +439,24 @@ def private_message(request,mid):
 def delete_private_message(request,msg):
   if request.method=='GET':
     return render_it('delete.html',
-      {'message': msg},
+      {'message': msg,
+       'referer': request.META['HTTP_REFERER']},
       request)
   elif request.method=='POST':
     msg.delete()
     return HttpResponseRedirect(reverse('mgmt.views.user_page',args=(request.user.username,)))

+@check_auth(Comment,'delete')
+def delete_comment(request,comment):
+  if request.method=='GET':
+    return render_it('delete.html',
+      {'message': comment,
+       'referer': request.META['HTTP_REFERER']},
+      request)
+  elif request.method=='POST':
+    comment.delete()
+    return HttpResponseRedirect('/')
+
 def requests(request,pid):
   project = Project.objects.get(pk=pid)
   rs = project.request_set.all()
@@ -455,6 +467,7 @@ def requests(request,pid):
      'requests': rs},
     request)

+@login_required
 def create_request(request,pid):
   project = Project.objects.get(pk=pid)
   if request.method == 'POST':
@@ -480,6 +493,29 @@ def create_request(request,pid):
        'form': form},
       request)

+@check_auth(Request,'edit')
+def edit_request(request,rq):
+  if request.method=='POST':
+    form = RequestForm(request.POST)
+    if form.is_valid():
+      title = form.cleaned_data['title']
+      text = form.cleaned_data['text']
+      rq.title = title
+      rq.text = text
+      rq.save()
+      return HttpResponseRedirect(reverse('mgmt.views.one_request',args=(rq.id,)))
+    else:
+      return render_it('edit_request.html',
+        {'request': rq,
+         'form': form},
+        request)
+  else:
+    form = RequestForm({'title': rq.name, 'text': rq.text})
+    return render_it('edit_request.html',
+      {'request': rq,
+       'form': form},
+      request)
+
 def one_request(request,rid):
   rq = Request.objects.get(pk=rid)
   if request.method=='POST':
@@ -491,8 +527,10 @@ def one_request(request,rid):
     state_form = RequestStateForm({'state': rq.status})
   else:
     state_form = None
+  can_edit = can(request.user,'edit',rq)
   return render_it('request.html',
       {'request': rq,
+       'can_edit': can_edit,
        'state_form': state_form},
       request)

diff --git a/templates/comments.html b/templates/comments.html
index 43ffa29..3aeddc5 100644
--- a/templates/comments.html
+++ b/templates/comments.html
@@ -4,11 +4,14 @@
   <div class='comments'>
     {% for comment in comments %}
       <div class='comment'>
-        <p class='comment-title'>
+        <div class='comment-title'>
+          <div class='links'>
+            <a href='{% url mgmt.views.delete_comment comment.id %}'>Удалить</a>
+          </div>
           <a name='comment-{{comment.id}}'>#</a>
           {{comment.title}} от <a href='{% url mgmt.views.user_page comment.author %}'>{{comment.author}}</a>, {{comment.created}}
-        </p>
-        <p>{{ comment.text|markdown|safe }}</p>
+        </div>
+        <div>{{comment.text|markdown|safe}}</div>
       </div>
     {% endfor %}
   </div>
diff --git a/templates/delete.html b/templates/delete.html
index 78eac08..c214ab9 100644
--- a/templates/delete.html
+++ b/templates/delete.html
@@ -1,14 +1,14 @@
 {% extends "base.html" %}

-{% block title %}Удалить {{message.name}}?{% endblock %}
+{% block title %}Удалить {% if message.name %}{{message.name}}{% else %}{{message.title}}{% endif %}?{% endblock %}

 {% block main %}

-<h2>Удалить {{message.name}}?</h2>
+<h2>Удалить {% if message.name %}{{message.name}}{% else %}{{message.title}}{% endif %}?</h2>

 <form method='post' action='.'>
   <p><input type='submit' value='Да'/>
-     <a href='{% url mgmt.views.user_page user.username %}'>Нет</a>
+     <a href='{{referer}}'>Нет</a>
   </p>
 </form>

diff --git a/templates/edit_request.html b/templates/edit_request.html
new file mode 100644
index 0000000..6d82e80
--- /dev/null
+++ b/templates/edit_request.html
@@ -0,0 +1,10 @@
+{% extends "base.html" %}
+{% load prj_filters %}
+
+{% block title %}Изменить запрос: {{rq.name}}{% endblock %}
+
+{% block main %}
+<h2>Изменить запрос: {{rq.name}}</h2>
+
+{% form form %}
+{% endblock %}
diff --git a/templates/request.html b/templates/request.html
index 6c2d154..2c2452a 100644
--- a/templates/request.html
+++ b/templates/request.html
@@ -7,7 +7,12 @@
 <div class='links'>
   <a href='{% url mgmt.views.requests request.project.id %}'>{{request.project.name}}</a>
 </div>
-<h2>Запрос функциональности: {{request.name}}</h2>
+{% if can_edit %}
+  <div class='links'>
+    <a href='{% url mgmt.views.edit_request request.id %}'>Изменить</a>
+  </div>
+{% endif %}
+<h2>Запрос функциональности #{{request.id}}: {{request.name}}</h2>

 <div class='request request-state-{{request.status}}'>
   {{request.text|markdown|safe}}
diff --git a/templates/requests.html b/templates/requests.html
index a024228..3c046d3 100644
--- a/templates/requests.html
+++ b/templates/requests.html
@@ -12,7 +12,7 @@
   {% endif %}
   {% for request in requests %}
   <div class='request request-state-{{request.status}}'>
-    <h3><a href='{% url mgmt.views.one_request request.id %}'>{{request.name}}</a></h3>
+    <h3><a href='{% url mgmt.views.one_request request.id %}'>#{{request.id}}: {{request.name}}</a></h3>
     {{request.text|markdown|safe}}
   </div>
   {% endfor %}
diff --git a/urls.py b/urls.py
index ed777f5..3697a0c 100644
--- a/urls.py
+++ b/urls.py
@@ -16,11 +16,13 @@ urlpatterns = patterns('',
     (r'^bugs/report/(\d+)/$', 'mgmt.views.bug_report'),
     (r'^bugs/$', "mgmt.views.all_bugs"),
     (r'^requests/(\d+)/$', 'mgmt.views.one_request'),
+    (r'^requests/(\d+)/edit/$', 'mgmt.views.edit_request'),
     (r'^requests/create/(\d+)/$', 'mgmt.views.create_request'),
     (r'^users/(\w+)/$', 'mgmt.views.user_page'),
     (r'^message/(\d+)/$', 'mgmt.views.private_message'),
     (r'^message/(\d+)/delete/$', 'mgmt.views.delete_private_message'),
     (r'^preview/$', 'mgmt.views.message_preview'),
+    (r'^comments/(\d+)/delete/$', 'mgmt.views.delete_comment'),
     (r'^my/$', 'mgmt.views.my_page'),

     (r'git/', include('pygit.urls')),
ViewGit