Commits

Jérémie Ducastel  committed a59c4fc Draft

Basic map CRUD (name, description)

  • Participants
  • Parent commits f926037

Comments (0)

Files changed (15)

 application: lsnrevival
-version: 2
+version: alpha1
 runtime: python27
 api_version: 1
 threadsafe: yes
 
 # unit tests
 # uneedtest
-- url: /_ah/unittest/.*
-  script: uneedtest.app
-# gae
-- url: /test.*
-  script: gaeunit.gaeunit.app
+#- url: /_ah/unittest/.*
+#  script: uneedtest.app
+## gae
+#- url: /test.*
+#  script: gaeunit.gaeunit.app
   
 # admin
-- url: /admin.*
-  script: main.app
-  login: admin
+#- url: /admin.*
+#  script: main.app
+#  login: admin
 
 - url: .*
   script: main.app
   version: "1.3"
 
 # profiling avec appstats
-builtins:
-  - appstats: on
+#builtins:
+#  - appstats: on

File game/models.py

 from google.appengine.ext import ndb
 from google.appengine.api import users as gusers
 
+from django.core.urlresolvers import reverse
+
 from lsnrevival.users.models import User
 
 # exceptions
 class Map(ndb.Model):
     """ for map selection in challenges """
     name = ndb.StringProperty(required=True)
+    description = ndb.TextProperty()
     # size width x length
     # capture(s)
 
+    @property
+    def permalink(self):
+        return reverse('web:map', args=[self.key.id()])
+
 
 class Challenge(ndb.Model):
     """ challenges issued, may be refused """

File users/templates/inc/register.form.html

     <fieldset>
         <legend>Register</legend>
         {{ register_form.as_p }}
+
+        <p>Already registered ? <a href="{% url users:login %}">Log in</a></p>
     </fieldset>
-</form>
+</form>

File web/forms.py

 
 # class et fonctions
 class MapForm(forms.Form):
-    name = forms.CharField()
+    name = forms.CharField(required=True)
+    description = forms.CharField(required=False, widget=forms.Textarea)
+
+    def __init__(self, data=None, instance=None, **kw):
+        super(MapForm, self).__init__(data, **kw)
+        self.instance = instance
+        if instance:
+            self.fields['name'].initial = instance.name
+            self.fields['description'].initial = instance.description
+
+    def save(self):
+        if not self.instance:
+            self.instance = Map()
+        self.instance.name = self.cleaned_data.get('name')
+        self.instance.description = self.cleaned_data.get('description')
+        self.instance.put()
+        return self.instance
 
 
 class ChallengeForm(forms.Form):

File web/templates/index.html

 {% load i18n %}
 
 {% block header %}<header class="big">
-    <h1>LSN Revival</h1>
+    <h1><a href="{% url web:index %}">LSN Revival</a></h1>
 </header>{% endblock %}
 
 {% block content %}<section class="content">
 {% endblock %}
 
 {% block footer %}
-    <nav class="global">
-        <a href="">Maps</a>
-    </nav>
-    {% if request.nuser %}
+<nav class="global">
+    <a href="{% url web:index %}">Home</a>
+    {% comment %}
+    <a href="">Space Marines</a>
+    <a href="">Machina</a>
+    <a href="">Spawn</a>
+    <a href="">Greys</a>
+    <a href="">Resources</a>
+    {% endcomment %}
+</nav>
+    {% if request.nuser.is_saved %}
+        <nav class="community">
+            <a href="{% url web:players %}">Players</a>
+            <a href="{% url web:maps %}">Maps</a>
+        </nav>
+
         <nav class="user">
             <a href="{% url web:user_index %}">User home</a>
+            <p>Profile</p>
+            <a href="{{ request.nuser.logout_url }}">logout</a>
         </nav>
     {% else %}
         {% include "inc/login.form.html" %}

File web/templates/login.html

+{% extends "index.html" %}
+
+{% block content %}<section class="content">
+    <section class="main">
+        {% include "inc/login.form.html" %}
+    </section>
+
+    <aside>
+        <p>Some help here ?</p>
+    </aside>
+{% endblock %}
+
+{% block footer %}{% endblock %}

File web/templates/maps.html

-{% extends "index.html" %}
-
-{% block content %}<section class="content">
-<header>Maps</header>{% for o in object_list %}
-<article>
-    <header>{{ o.name }}</header>
-    <p>Some description + screen(s)</p>
-    <p>If player connected, link to update form</p>
-</article>{% endfor %}
-
-{% if request.nuser %}<form method="post" action="">
-    {% csrf_token %}
-    <fieldset>
-        <legend>Register a map</legend>
-        {{ create_form.as_p }}
-        <p><button type="submit" disabled="disabled">Register</button></p>
-    </fieldset>
-</form>{% endif %}
-</section>
-{% endblock %}

File web/templates/maps/create.html

+{% extends "maps/index.html" %}
+
+{% block content %}
+<form action="{% url web:map_create %}" method="post">
+    {% csrf_token %}
+    <fieldset>
+        <legend>New map</legend>
+        {{ create_form.as_p }}
+        <p><button type="submit">Save</button></p>
+    </fieldset>
+</form>
+{% endblock %}

File web/templates/maps/index.html

+{% extends "index.html" %}
+
+{% block content %}<section class="content">
+<section class="main">
+    <header>Maps</header>{% for o in object_list %}
+    <article>
+        <header><a href="{{ o.permalink }}">{{ o.name }}</a></header>
+        {{ o.description|linebreaks }}
+    </article>{% endfor %}
+</section>
+
+<aside>
+{% if request.nuser %}<form method="post" action="{% url web:map_create %}">
+    {% csrf_token %}
+    <fieldset>
+        <legend>Register a map</legend>
+        {{ create_form.as_p }}
+        <p><button type="submit">Register</button></p>
+    </fieldset>
+</form>{% endif %}
+</aside>
+
+</section>
+{% endblock %}

File web/templates/maps/map.html

+{% extends "maps/index.html" %}
+
+{% block content %}
+<article>
+    <header>{{ object.name }}</header>
+    {{ object.description|linebreaks }}
+    <footer><a href="{% url web:map_update object.key.id %}">Update</a></footer>
+</article>
+{% endblock %}

File web/templates/maps/update.html

+{% extends "maps/index.html" %}
+
+{% block content %}
+<form action="{% url web:map_update object.key.id %}" method="post">
+    {% csrf_token %}
+    <fieldset>
+        <legend>Update map</legend>
+        {{ update_form.as_p }}
+        <p><button type="submit">Save</button></p>
+    </fieldset>
+</form>
+{% endblock %}

File web/templates/register.html

+{% extends "index.html" %}
+
+{% block content %}<section class="content">
+    <section class="main">
+        {% include "inc/register.form.html" %}
+    </section>
+
+    <aside>
+        <p>Some help here ?</p>
+    </aside>
+{% endblock %}
+
+{% block footer %}{% endblock %}

File web/templates/user/index.html

         {{ challenge_form.as_p }}
         <p>
             <button type="submit" disabled="disabled">Save and send challenge</button>
+            (Yes, this doesn't work yet)
         </p>
     </fieldset>
 </form>
 {% endblock %}
 
+{% comment %}
 {% block footer %}
 <nav class="global">
-    <a href="{% url web:index %}">Site home</a>
-    <a href="{% url web:maps %}">Maps</a>
+    <a href="{% url web:index %}">Home</a>
+    <a href="">Factions</a>
+    <a href="">Resources</a>
 </nav>
 
 <nav class="user">
     <a href="{% url web:user_index %}">User home</a>
     <a href="{% url web:players %}">Players</a>
+    <a href="{% url web:maps %}">Maps</a>
     <a href="{{ request.nuser.logout_url }}">logout</a>
 </nav>
 
-{% endblock %}
+{% endblock %}{% endcomment %}
     url(r'^$', 'index', name="index"),
     url(r'^home/$', 'user_index', name="user_index"),
     url(r'^players/$', 'players', name="players"),
+    # maps
     url(r'^maps/$', 'maps', name="maps"),
+    url(r'^maps/add/$', 'map_create', name="map_create"),
+    url(r'^map/([0-9]+)/$', 'map', name="map"),
+    url(r'^map/([0-9]+)/update/$', 'map_update', name="map_update"),
 
     # errors pages
     #url(r'^404/?$', 'error404', name="error404"),

File web/views.py

 from django.shortcuts import render_to_response, get_object_or_404, redirect, Http404
 from django.template import RequestContext
 from django.utils.translation import ugettext_lazy as _
-from django.http import HttpResponseRedirect, HttpResponse
+from django.http import HttpResponseRedirect, HttpResponse, Http404
 from django.core.urlresolvers import reverse
 
 from google.appengine.api import users as gusers
 # exceptions
 
 # class et fonctions
+def object_view(request, model_class, object_id, template):
+    object = model_class.get_by_id(int(object_id))
+    if not object:
+        raise Http404('could not find %s' % object_id)
+    return render_to_response(template,
+        RequestContext(request, locals()))
 
+def object_create_view(request, model_class, form_class, template):
+    if request.POST:
+        create_form = form_class(request.POST)
+        if create_form.is_valid():
+            object = create_form.save()
+            # redirection
+            return HttpResponseRedirect(object.permalink)
+    else:
+        create_form = form_class()
+    return render_to_response(template,
+        RequestContext(request, locals()))
+
+def object_update_view(request, model_class, object_id, form_class, template):
+    object = model_class.get_by_id(int(object_id))
+    if not object:
+        raise Http404('could not find %s' % object_id)
+    if request.POST:
+        update_form = form_class(request.POST, instance=object)
+        if update_form.is_valid():
+            object = update_form.save()
+            # redirection
+            return HttpResponseRedirect(object.permalink)
+    else:
+        update_form = form_class(instance=object)
+    return render_to_response(template,
+        RequestContext(request, locals()))
 
 
 # site pages
     return render_to_response('index.html',
         RequestContext(request, locals()))
 
+#def static_page(request, template):
+#    """ accueil public general """
+#    login_form = LoginForm()
+#    return render_to_response('index.html',
+#        RequestContext(request, locals()))
+
 @login_required
 def user_index(request):
     """ player home when logged """
     """ maps list """
     object_list = Map.query().order(Map.name).fetch()
     create_form = MapForm()
-    return render_to_response('maps.html',
+    return render_to_response('maps/index.html',
         RequestContext(request, locals()))
 
+def map_create(request):
+    """ register a new map """
+    return object_create_view(request, Map, MapForm, 'maps/create.html')
+
+def map(request, id):
+    """ single map view """
+    return object_view(request, Map, id, 'maps/map.html')
+
+def map_update(request, id):
+    """ """
+    return object_update_view(request, Map, id, MapForm, 'maps/update.html')
+
 def players(request):
     """ players list """
     players = User.query().fetch()