Add views and corresponding templates.

portnov [2008-06-07 10:08:45]
Add views and corresponding templates.
Filename
mgmt/models.py
mgmt/views.py
templates/bug_body.html
templates/comments.html
templates/document.html
templates/main.html
templates/project.html
templates/project_body.html
templates/project_bugs.html
templates/project_docs.html
urls.py
diff --git a/mgmt/models.py b/mgmt/models.py
index fefad9d..a1cd1e2 100644
--- a/mgmt/models.py
+++ b/mgmt/models.py
@@ -42,6 +42,7 @@ class Document(Object):
     pass

 class Comment(models.Model):
+  author = models.ForeignKey(User)
   created = models.DateTimeField()
   object = models.ForeignKey(Object)
   title = models.CharField(max_length=64,null=True)
diff --git a/mgmt/views.py b/mgmt/views.py
index f226f76..a5b2fad 100644
--- a/mgmt/views.py
+++ b/mgmt/views.py
@@ -1,15 +1,80 @@
 # Create your views here.

+from datetime import datetime
+
 from django.shortcuts import render_to_response
+import django.newforms as forms
 from models import *

+class CommentForm(forms.Form):
+  title = forms.CharField()
+  text = forms.CharField(widget=forms.Textarea())
+
+class BugForm(forms.Form):
+  name = forms.CharField()
+  text = forms.CharField(widget=forms.Textarea())
+
 def main(request):
   projects = Project.objects.all()
   return render_to_response('main.html',{'projects': projects})

 def one_project(request,id):
   project = Project.objects.get(pk=id)
-  comments = project.comment_set
+  if request.method=='POST':
+    title = request.POST['title']
+    text  = request.POST['text']
+    c = Comment(created=datetime.now(),
+        author = request.user,
+        object = project,
+        title = title,
+        text = text)
+    c.save()
+  comments = Comment.objects.filter(object=project)
+  form = CommentForm()
   return render_to_response('project.html',
       {'project': project,
+       'comments': comments,
+       'form': form})
+
+def project_bugs(request,id):
+  project = Project.objects.get(pk=id)
+  if request.method=='POST':
+    name = request.POST['name']
+    text  = request.POST['text']
+    b = Bug(created=datetime.now(),
+        author = request.user,
+        project = project,
+        status = 0,
+        name = name,
+        text = text)
+    b.save()
+  bugs = Bug.objects.filter(project=project)
+  form = BugForm()
+  return render_to_response('project_bugs.html',
+      {'project': project,
+       'bugs': bugs,
+       'form': form})
+
+def project_documents(request,id):
+  project = Project.objects.get(pk=id)
+  docs = Document.objects.filter(project=project)
+  return render_to_response('project_docs.html',
+      {'project': project,
+       'docs': docs})
+
+def one_document(request,id):
+  doc = Document.objects.get(pk=id)
+  if request.method=='POST':
+    title = request.POST['title']
+    text  = request.POST['text']
+    c = Comment(created=datetime.now(),
+        author = request.user,
+        object = doc,
+        title = title,
+        text = text)
+    c.save()
+  comments = Comment.objects.filter(object=doc)
+  form = CommentForm()
+  return render_to_response('document.html',
+      {'document': doc,
        'comments': comments})
diff --git a/templates/bug_body.html b/templates/bug_body.html
new file mode 100644
index 0000000..dcd77df
--- /dev/null
+++ b/templates/bug_body.html
@@ -0,0 +1,4 @@
+      <div class='bug bug-state-{{bug.status}}'>
+        <p><strong>{{bug.name}}</strong></p>
+        <p>{{bug.text}}</p>
+      </div>
diff --git a/templates/comments.html b/templates/comments.html
new file mode 100644
index 0000000..6e8973e
--- /dev/null
+++ b/templates/comments.html
@@ -0,0 +1,17 @@
+{% if comments.count %}
+  <div class='comments'>
+    {% for comment in comments %}
+      <div class='comment'>
+        <p class='comment-title'>{{ comment.title }} от {{ comment.author }}, {{ comment.created }}</p>
+        <p>{{ comment.text }}</p>
+      </div>
+    {% endfor %}
+  </div>
+{% else %}
+  <p>Комментариев пока нет.</p>
+{% endif %}
+
+<form method='post' action='.'>
+  {{ form.as_p }}
+  <input type='submit' />
+</form>
diff --git a/templates/document.html b/templates/document.html
new file mode 100644
index 0000000..bb410db
--- /dev/null
+++ b/templates/document.html
@@ -0,0 +1,13 @@
+{% extends "base.html" %}
+
+{%block title%}Документ: {{document.name}}{%endblock%}
+
+{% block main %}
+
+<h2>{{document.name}}</h2>
+
+<div class='document'>
+  {{document.text}}
+</div>
+
+{% include "comments.html" %}
diff --git a/templates/main.html b/templates/main.html
index 88968f0..e78e541 100644
--- a/templates/main.html
+++ b/templates/main.html
@@ -4,13 +4,8 @@

 {% block main %}
   <div id='projects'>
-    {% for proj in projects %}
-    <div class='project'>
-      <p><strong><a href='/projects/{{ proj.id }}'>{{ proj.name }}</a></strong></p>
-      <p><strong>Репозиторий: </strong> {{ proj.repo }}</p>
-      <p>{{ proj.text }}</p>
-      <p class='date'>{{ proj.created }}</p>
-    </div>
+    {% for project in projects %}
+    {% include "project_body.html" %}
     {% endfor %}
   </div>
 {% endblock %}
diff --git a/templates/project.html b/templates/project.html
index 5dec1e2..dcdebba 100644
--- a/templates/project.html
+++ b/templates/project.html
@@ -5,24 +5,6 @@
 {% endblock %}

 {% block main %}
-<h2>{{ project.name }}</h2>
-<div class='project'>
-  <p><strong>Репозиторий: </strong> {{ project.repo }}</p>
-  <p>{{ project.text }}</p>
-  <p class='date'>{{ project.created }}</p>
-</div>
-
-{% if comments %}
-  <div class='comments'>
-    {% for comment in comments %}
-      <div class='comment'>
-        <p class='comment-title'>{{ comment.title }} от {{ comment.author }}, {{ comment.created }}</p>
-        <p>{{ comment.text }}</p>
-      </div>
-    {% endfor %}
-  </div>
-{% else %}
-  <p>Комментариев пока нет.</p>
-{% endif %}
-
+{% include "project_body.html" %}
+{% include "comments.html" %}
 {% endblock %}
diff --git a/templates/project_body.html b/templates/project_body.html
new file mode 100644
index 0000000..392b640
--- /dev/null
+++ b/templates/project_body.html
@@ -0,0 +1,11 @@
+
+    <div class='project'>
+      <p><strong><a href='/projects/{{ project.id }}'>{{ project.name }}</a></strong></p>
+      <p><strong>Репозиторий: </strong> {{ project.repo }}</p>
+      <p>{{ project.text }}</p>
+      <p class='date'>{{ project.created }}</p>
+      <p class='links'>
+        <a href='/projects/{{project.id}}/docs/'>Документация</a>
+        <a href='/projects/{{project.id}}/bugs/'>Баги</a>
+      </p>
+    </div>
diff --git a/templates/project_bugs.html b/templates/project_bugs.html
new file mode 100644
index 0000000..19cbf0c
--- /dev/null
+++ b/templates/project_bugs.html
@@ -0,0 +1,26 @@
+{% extends "base.html" %}
+
+{% block title %}Баги проекта {{project.name}} {% endblock %}
+
+{% block main %}
+
+{% include "project_body.html" %}
+
+<h3>Баги:</h3>
+
+{% if bugs %}
+  <div class='bugs'>
+    {% for bug in bugs %}
+    {% include "bug_body.html" %}
+    {% endfor %}
+  </div>
+{% else %}
+<p>В этом проекте нет багов.</p>
+{%endif%}
+
+<form method='post' action='.'>
+  {{form.as_p}}
+  <input type='submit'/>
+</form>
+
+{% endblock %}
diff --git a/templates/project_docs.html b/templates/project_docs.html
new file mode 100644
index 0000000..d07c218
--- /dev/null
+++ b/templates/project_docs.html
@@ -0,0 +1,18 @@
+{% extends "base.html" %}
+
+{% block title %}Документация по проекту {{project.name}}{% endblock %}
+
+{% block main %}
+
+  <h2>Проект: {{project.name}}</h2>
+  {%if docs%}
+    <ul class='documents'>
+    {% for doc in docs %}
+      <li><a href='/docs/{{doc.id}}/'>{{doc.name}}</a></li>
+    {% endfor %}
+    </ul>
+  {%else%}
+    <p>Документации пока нет.</p>
+  {%endif%}
+
+{% endblock %}
diff --git a/urls.py b/urls.py
index 7c52a56..65b4cbc 100644
--- a/urls.py
+++ b/urls.py
@@ -1,10 +1,12 @@
 from django.conf.urls.defaults import *
-from mgmt.views import main,one_project
+from mgmt.views import main,one_project,project_bugs,one_document,project_documents

 urlpatterns = patterns('',
     (r'^$', main),
     (r'^projects/(\d+)/$', one_project),
+    (r'^projects/(\d+)/bugs/$', project_bugs),
+    (r'^projects/(\d+)/docs/$', project_documents),
+    (r'^docs/(\d+)/$', one_document),

-    # Uncomment this for admin:
     (r'^admin/', include('django.contrib.admin.urls')),
 )
ViewGit