Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
  • 13 commits
  • 13 files changed
  • 0 comments
  • 1 contributor
6  mokumoku/template/404.html
... ...
@@ -1,11 +1,15 @@
1 1
 <!DOCTYPE html>
2  
-<html>
  2
+<html lang="ja">
3 3
 <head>
4 4
 <meta charset="UTF-8" />
  5
+<title>指定されたページは存在しません.</title>
5 6
 </head>
6 7
 <body>
7 8
 <h1>Not Found</h1>
8 9
 The request URL {{ request_path }} was not found in this server.
  10
+<footer>
  11
+    <p>Copyright 2012</p>
  12
+</footer>
9 13
 </body>
10 14
 </html>
11 15
 
15  mokumoku/template/500.html
... ...
@@ -0,0 +1,15 @@
  1
+<!DOCTYPE html>
  2
+<html lang="ja">
  3
+<head>
  4
+<meta charset="UTF-8" />
  5
+<title>サーバでエラーが発生しました.</title>
  6
+</head>
  7
+<body>
  8
+<h1>Internal Server Error</h1>
  9
+Some errors occur in server. 
  10
+<footer>
  11
+    <p>Copyright 2012</p>
  12
+</footer>
  13
+</body>
  14
+</html>
  15
+
16  mokumoku/template/three/base.html
... ...
@@ -0,0 +1,16 @@
  1
+<!DOCTYPE html>
  2
+<html lang="ja">
  3
+<head>
  4
+<meta charset="UTF-8" />
  5
+<title>{% block title %}〜Three〜 一日の目標を3つに絞ろう.{% endblock %}</title>
  6
+</head>
  7
+<body>
  8
+    <div id="content">
  9
+        {% block content %}{% endblock %}
  10
+    </div>
  11
+    <footer>
  12
+        <p>Copyright 2012</p>
  13
+    </footer>
  14
+</body>
  15
+</html>
  16
+
28  mokumoku/template/three/goal.html
... ...
@@ -1,13 +1,15 @@
1  
-<!DOCTYPE html>
2  
-<html>
3  
-<head>
4  
-<meta charset="UTF-8" />
5  
-</head>
6  
-<body>
7  
-<div>ID:{{ goal.id }}</div>
8  
-<div>Name:{{ goal.name}}</div>
9  
-<div>Date:{{ goal.date }}</div>
10  
-<div>Body:{{ goal.body }}</div>
11  
-</body>
12  
-</html>
13  
-
  1
+{% extends "three/base.html" %}
  2
+{% block title %}
  3
+    <title>〜Three〜 一日の目標を3つに絞ろう.</title>
  4
+{% endblock %}
  5
+{% block content %}
  6
+    {% for g in goal %}
  7
+        <ul>
  8
+                <li>Name:{{ g.name }}</li>
  9
+                <li>Date:{{ g.date }}</li>
  10
+                <li>Body:{{ g.body }}</li>
  11
+                <li><a href="/three/goals/{{ g.id }};delete">この目標を削除する</a></li>
  12
+        </ul>
  13
+        <br />
  14
+    {% endfor %}
  15
+{% endblock %}
18  mokumoku/template/three/goal_delete.html
... ...
@@ -0,0 +1,18 @@
  1
+{% extends "three/base.html" %}
  2
+{% block title %}
  3
+    <title>〜Three〜 一日の目標を3つに絞ろう.</title>
  4
+{% endblock %}
  5
+{% block content %}
  6
+    {% for g in goal %}
  7
+        <ul>
  8
+                <li>Name:{{ g.name }}</li>
  9
+                <li>Date:{{ g.date }}</li>
  10
+                <li>Body:{{ g.body }}</li>
  11
+        </ul>
  12
+        <form action="/three/goals/{{ goal_id }}/" method="post">{% csrf_token %}
  13
+            <input type="hidden" id="_method" name="_method" value="DELETE" />
  14
+            <input type="submit" value="目標を削除する" />
  15
+        </form>
  16
+        <br />
  17
+    {% endfor %}
  18
+{% endblock %}
10  mokumoku/template/three/goal_edit.html
... ...
@@ -0,0 +1,10 @@
  1
+{% extends "three/base.html" %}
  2
+{% block title %}
  3
+    <title>〜Three〜 一日の目標を3つに絞ろう.</title>
  4
+{% endblock %}
  5
+{% block content %}
  6
+    <form action="/three/goals/{{ goal_id }}/" method="post">{% csrf_token %}
  7
+        {{ form.as_p }}
  8
+    <input type="submit" value="目標を更新する" />
  9
+    </form>
  10
+{% endblock %}
10  mokumoku/template/three/goal_new.html
... ...
@@ -0,0 +1,10 @@
  1
+{% extends "three/base.html" %}
  2
+{% block title %}
  3
+    <title>〜Three〜 一日の目標を3つに絞ろう.</title>
  4
+{% endblock %}
  5
+{% block content %}
  6
+    <form action="/three/goals/" method="post">{% csrf_token %}
  7
+        {{ form.as_p }}
  8
+    <input type="submit" value="目標を登録する" />
  9
+    </form>
  10
+{% endblock %}
10  mokumoku/template/three/goals.html
... ...
@@ -1,10 +0,0 @@
1  
-<!DOCTYPE html>
2  
-<html>
3  
-<head>
4  
-<meta charset="UTF-8" />
5  
-</head>
6  
-<body>
7  
-Hello. Three!
8  
-</body>
9  
-</html>
10  
-
20  mokumoku/three/apis.py
... ...
@@ -0,0 +1,20 @@
  1
+#!/usr/bin/env python
  2
+# -*- coding: utf-8 -*-
  3
+from django.http import HttpResponse
  4
+from django.views.decorators.http import require_http_methods
  5
+from three.models import Goal
  6
+
  7
+@require_http_methods(['GET', 'POST'])
  8
+def goals_list(request):
  9
+    if request.method == 'GET':
  10
+        _list = Goal.serialize_list('json')
  11
+        return HttpResponse(_list, mimetype='application/json')
  12
+    if request.method == 'POST':
  13
+        pass
  14
+        
  15
+def goals_detail(request, goal_id):
  16
+    if request.method == 'GET':
  17
+        _object = Goal.serialize_object('json', goal_id)
  18
+        return HttpResponse(_object, mimetype='application/json')
  19
+    if request.method == 'DELETE':
  20
+        pass
10  mokumoku/three/forms.py
... ...
@@ -0,0 +1,10 @@
  1
+#!/usr/bin/env python
  2
+# -*- coding: utf-8 -*-
  3
+
  4
+from django.forms import ModelForm
  5
+from three.models import Goal
  6
+
  7
+class GoalForm(ModelForm):
  8
+    class Meta:
  9
+        model = Goal
  10
+        fields = ('name', 'body')
12  mokumoku/three/models.py
... ...
@@ -1,3 +1,5 @@
  1
+from django.shortcuts import get_object_or_404, get_list_or_404
  2
+from django.core import serializers
1 3
 from django.db import models
2 4
 
3 5
 class Goal(models.Model):
@@ -14,3 +16,13 @@ def __unicode__(self):
14 16
     name:{0},
15 17
     date:{1},
16 18
     body:{2},'''.format(self.name, self.date, self.body)
  19
+
  20
+    @classmethod
  21
+    def serialize_list(cls, format, **options):
  22
+        _list = get_list_or_404(cls)
  23
+        return serializers.serialize(format, _list, **options)
  24
+
  25
+    @classmethod
  26
+    def serialize_object(cls, format, pk, **options):
  27
+        _object = [get_object_or_404(cls, pk=pk)]
  28
+        return serializers.serialize(format, _object, **options)
18  mokumoku/three/urls.py
... ...
@@ -1,10 +1,20 @@
1  
-from django.conf.urls.defaults import patterns, include, url
  1
+from django.conf.urls.defaults import patterns, url
2 2
 
3  
-urlpatterns = patterns('mokumoku.three.views',
  3
+urlpatterns = patterns('three.views',
4 4
 
5 5
     # app root(/)
6 6
     url(r'^$', 'index'),
  7
+    url(r'^goals/$', '_list'),
  8
+    url(r'^goals;new$', '_new'),
  9
+    url(r'^goals/(?P<goal_id>\d+)/$', '_detail'),
  10
+    url(r'^goals/(?P<goal_id>\d+);edit$', '_edit'),
  11
+    url(r'^goals/(?P<goal_id>\d+);delete$', '_delete'),
  12
+)
  13
+
  14
+urlpatterns += patterns('three.apis',
7 15
 
8  
-    url(r'^goals/$', 'goals'),
9  
-    url(r'^goals/(?P<goal_id>\d+)/$', 'goal'),
  16
+    # api
  17
+    url(r'^api/v1/goals/$', 'goals_list'),
  18
+    url(r'^api/v1/goals/(?P<goal_id>\d)/$', 'goals_detail'),
10 19
 )
  20
+
84  mokumoku/three/views.py
... ...
@@ -1,17 +1,43 @@
1 1
 # Create your views here.
2  
-from django.shortcuts import render_to_response, redirect, get_object_or_404
  2
+import datetime
  3
+
  4
+from django.shortcuts import render_to_response, redirect, get_object_or_404, get_list_or_404
  5
+from django.core.context_processors import csrf
  6
+from django.views.decorators.http import require_http_methods, require_GET
  7
+
3 8
 from three.models import Goal
  9
+from three.forms import GoalForm
  10
+
  11
+@require_http_methods(['GET', 'POST'])
  12
+def index(request):
  13
+    """index: This method for the url '/'(application root).
4 14
 
5  
-def goals(request):
  15
+    :param request:
  16
+    :return: a result from redirecting to 'goals/'
  17
+
  18
+    """
  19
+    return redirect(_list)
  20
+
  21
+@require_http_methods(['GET', 'POST'])
  22
+def _list(request):
6 23
     """goals: This method for the url 'goals/'.
7 24
 
8 25
     :param request:
9 26
     :return: a page for goal lists of specified user
10 27
 
11 28
     """
12  
-    return render_to_response('three/goals.html', mimetype='text/html')
  29
+    if request.method == 'GET':
  30
+        goal = get_list_or_404(Goal)
  31
+        return render_to_response('three/goal.html', {'goal': goal}, mimetype='text/html')
  32
+    if request.method == 'POST':
  33
+        goal = Goal(date=datetime.datetime.now())
  34
+        form = GoalForm(request.POST, instance=goal)
  35
+        form.save()
  36
+        if form.is_valid():
  37
+            return redirect(_list)
13 38
 
14  
-def goal(request, goal_id):
  39
+@require_http_methods(['GET', 'POST'])
  40
+def _detail(request, goal_id):
15 41
     """goal: This method for the url 'goal/'.
16 42
 
17 43
     :param request:
@@ -20,13 +46,53 @@ def goal(request, goal_id):
20 46
 
21 47
     """
22 48
     goal = get_object_or_404(Goal, pk=goal_id)
23  
-    return render_to_response('three/goal.html', {'goal':goal}, mimetype='text/html')
  49
+    if request.method == 'GET':
  50
+        return render_to_response('three/goal.html', {'goal': [goal]}, mimetype='text/html')
  51
+    if request.method == 'POST':
  52
+        if request.POST.has_key('_method') and request.POST['_method'] == 'DELETE':
  53
+            goal.delete()
  54
+            return redirect(_list)
  55
+        goal.date = datetime.datetime.now()
  56
+        form = GoalForm(request.POST, instance=goal)
  57
+        form.save()
  58
+        if form.is_valid():
  59
+            return redirect(_detail, goal_id=goal_id)
24 60
 
25  
-def index(request):
26  
-    """index: This method for the url '/'(application root).
  61
+@require_GET
  62
+def _new(request):
  63
+    """create
27 64
 
28 65
     :param request:
29  
-    :return: a result from redirecting to 'goals/'
  66
+    """
  67
+    if request.method == 'GET':
  68
+        form = GoalForm()
  69
+        c = {'form': form}
  70
+        c.update(csrf(request))
  71
+        return render_to_response('three/goal_new.html', c, mimetype='text/html')
  72
+
  73
+@require_GET
  74
+def _edit(request, goal_id):
  75
+    """goals_edit
  76
+
  77
+    :param request:
  78
+    """
  79
+    if request.method == 'GET':
  80
+        goal = get_object_or_404(Goal, pk=goal_id)
  81
+        form = GoalForm(instance=goal)
  82
+        c = {'form': form, 'goal_id':goal_id}
  83
+        c.update(csrf(request))
  84
+        return render_to_response('three/goal_edit.html', c, mimetype='text/html')
  85
+
  86
+@require_GET
  87
+def _delete(request, goal_id):
  88
+    """delete
30 89
 
  90
+    :param request:
  91
+    :param goal_id:
31 92
     """
32  
-    return redirect(goals)
  93
+    if request.method == 'GET':
  94
+        goal = get_object_or_404(Goal, pk=goal_id)
  95
+        c = {'goal': [goal], 'goal_id':goal_id}
  96
+        c.update(csrf(request))
  97
+        return render_to_response('three/goal_delete.html', c, mimetype='text/html')
  98
+

No commit comments for this range

Something went wrong with that request. Please try again.