Add some templates. Refactor models, using inheritance.

portnov [2008-06-07 07:12:45]
Add some templates. Refactor models, using inheritance.
Filename
mgmt/models.py
mgmt/views.py
settings.py
templates/base.html
templates/main.html
templates/project.html
urls.py
diff --git a/mgmt/models.py b/mgmt/models.py
index b7ea265..fefad9d 100644
--- a/mgmt/models.py
+++ b/mgmt/models.py
@@ -2,38 +2,42 @@ from django.db import models
 from django.contrib.auth.models import User

 class Object(models.Model):
-  type = models.CharField(max_length=10)
-  oid = models.IntegerField()
-
-class Project(models.Model):
   name = models.CharField(max_length=64)
   text = models.TextField()
   created = models.DateTimeField()
+
+class Project(Object):
   repo = models.CharField(max_length=64)
   admins = models.ManyToManyField(User,related_name='admin_projects')
   team = models.ManyToManyField(User,related_name='in_projects')
+  def __unicode__(self):
+    return self.name
+  class Meta:
+    ordering = ("-created",)
   class Admin:
     pass

-class Bug(models.Model):
-  created = models.DateTimeField()
+class Bug(Object):
   fixed = models.DateTimeField(null=True)
   project = models.ForeignKey(Project)
   author = models.ForeignKey(User,related_name='created_bugs')
   confirmed = models.ForeignKey(User,related_name='confirmed_bugs',null=True)
   responsible = models.ForeignKey(User,related_name='resp_bugs',null=True)
   status = models.IntegerField()
-  title = models.CharField(max_length=64)
-  text = models.TextField()
+  def __unicode__(self):
+    return self.name
+  class Meta:
+    ordering = ("-created",)
   class Admin:
     pass

-class Document(models.Model):
-  created = models.DateTimeField()
+class Document(Object):
   project = models.ForeignKey(Project)
   author = models.ForeignKey(User)
-  title = models.CharField(max_length=64)
-  text = models.TextField()
+  def __unicode__(self):
+    return self.name
+  class Meta:
+    ordering = ("-created",)
   class Admin:
     pass

@@ -42,3 +46,7 @@ class Comment(models.Model):
   object = models.ForeignKey(Object)
   title = models.CharField(max_length=64,null=True)
   text = models.TextField()
+  def __unicode__(self):
+    return self.title
+  class Meta:
+    ordering = ("created",)
diff --git a/mgmt/views.py b/mgmt/views.py
index 60f00ef..f226f76 100644
--- a/mgmt/views.py
+++ b/mgmt/views.py
@@ -1 +1,15 @@
 # Create your views here.
+
+from django.shortcuts import render_to_response
+from models import *
+
+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
+  return render_to_response('project.html',
+      {'project': project,
+       'comments': comments})
diff --git a/settings.py b/settings.py
index 5539e59..7404970 100644
--- a/settings.py
+++ b/settings.py
@@ -62,6 +62,7 @@ ROOT_URLCONF = 'projects.urls'
 TEMPLATE_DIRS = (
     # Put strings here, like "/home/html/django_templates".
     # Always use forward slashes, even on Windows.
+    '/home/portnov/www/projects/templates',
 )

 INSTALLED_APPS = (
diff --git a/templates/base.html b/templates/base.html
new file mode 100644
index 0000000..ed1a13f
--- /dev/null
+++ b/templates/base.html
@@ -0,0 +1,30 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml" lang="ru">
+  <head>
+    <title>
+      {% block title %}
+      {% endblock %}
+    </title>
+    <meta name='author' content='Portnov'>
+    {% block head %}
+    {% endblock %}
+  </head>
+  <body>
+
+  <div id='body'>
+    <div id='header'>
+      {%block header%}
+      {%endblock%}
+    </div>
+
+    <div id='main'>
+      {% block main %}{% endblock %}
+    </div>
+
+    <div id='footer'>
+      {% block footer %}{% endblock %}
+    </div>
+  </div>
+
+  </body>
+</html>
diff --git a/templates/main.html b/templates/main.html
new file mode 100644
index 0000000..88968f0
--- /dev/null
+++ b/templates/main.html
@@ -0,0 +1,16 @@
+{% extends "base.html" %}
+
+{% block title %}Список проектов{% endblock %}
+
+{% 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>
+    {% endfor %}
+  </div>
+{% endblock %}
diff --git a/templates/project.html b/templates/project.html
new file mode 100644
index 0000000..5dec1e2
--- /dev/null
+++ b/templates/project.html
@@ -0,0 +1,28 @@
+{% extends "base.html" %}
+
+{% block title %}
+Проект: {{ project.name }}
+{% 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 %}
+
+{% endblock %}
diff --git a/urls.py b/urls.py
index a87c151..7c52a56 100644
--- a/urls.py
+++ b/urls.py
@@ -1,10 +1,9 @@
 from django.conf.urls.defaults import *
-from projects.views import current_time
+from mgmt.views import main,one_project

 urlpatterns = patterns('',
-    # Example:
-    # (r'^projects/', include('projects.apps.foo.urls.foo')),
-    (r'^time/', current_time),
+    (r'^$', main),
+    (r'^projects/(\d+)/$', one_project),

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