Commits

Alexandre Beaulieu  committed 0c1539b

Added common app. Finished achievement views.

  • Participants
  • Parent commits 0fa1a26

Comments (0)

Files changed (16)

File achievements/forms.py

+from django import forms
+
+class ImportForm(forms.Form):
+    file = forms.FileField(label='Import from CSV')

File achievements/helpers.py

+# Helper functions for the achievements application
+
+def csv2model(data, model, format, auto_save=False):
+"""
+    Parses a CSV file, extracts and saves models according to a
+    specific format. the format is a dictionary in the following
+    form:
+    
+    format = { 'map': ['field1', 'field2', ...],
+               'include': ['field2'], # None -> Include all
+               'exclude': ['field1'], # None -> Don't exclude
+               'delimiter': ','
+             }
+             
+     The keys 'include' and 'exclude' can be ommitted and will default
+     to None. The key 'delimiter' will default to ',' if ommitted.
+             
+     @param data an iterable structure of strings representing one
+                 line in the CSV file.
+     @param model the model to map the CSV data to.
+     @param format the format dictionary as described above.
+     @param auto_save whether to automatically save the models to
+                      the database.
+     
+     @return A list of successfully converted models. If auto_save
+             is enabled, then the database entries for the converted
+             models have also been created.
+"""
+    # Defaults
+    DELIMITER = ','
+    INCLUDE = None
+    EXCLUDE = None
+    if 'delimiter' in format:
+        DELIMTIER = format['delimiter']
+    if 'include' in format:
+        INCLUDE = format['include']
+    if 'exclude' in format:
+        EXCLUDE = format['exclude']
+        
+    # TODO: Implementation stub
+    
+    return []

File achievements/templates/achievements/base.html

+{% extends 'base.html' %}
+{% block title %}Achievements{% endblock %}
+{% block body  %}
+  <center>
+  {% block content %}
+  {% endblock %}
+  </center>
+{% endblock %}

File achievements/templates/achievements/import.html

+{% extends 'achievements/base.html' %}
+
+{% block content %}
+<h1>Import Achievement List</h1>
+{% endblock %}

File achievements/templates/achievements/index.html

-{% extends 'base.html' %}
+{% extends 'achievements/base.html' %}

File achievements/urls.py

     # Browse the list of achievements
     url(r'^$', 'achievements.views.list', name='achievement list'),
     # Import a list of achievements
-    #url(r'^import/$, 'achievements.views.import', name='import'),
+    url(r'^import/$', 'achievements.views.import_list', name='import'),
     # View a specific user's achievements
-    #url(r'^user/(?P<username>[a-z]+)/$', 'achievements.views.list', name='user achievements'),
+    url(r'^user/(?P<username>[a-z]+)/$', 'achievements.views.list', name='user achievements'),
 )

File achievements/views.py

-from django.http import HttpResponse
+from django.http import HttpResponseRedirect
 from django.shortcuts import render
 from achievements.models import Achievement, Award
+from achievements.forms import ImportForm
+from achievements import helpers
 
 def list(request, username=None):
     if username is not None:
         return render(request, 'achievements/index.html', {'achievements': achievements })
     else:
         return render(request, 'achievements/index.html', {'achievements': Achievement.objects.all()})
+
+def import_list(request):
+    if request.method == 'POST':
+        form = ImportForm(request.POST, request.FILES)
+        if form.is_valid():
+            models = helpers.csv2model(request.FILES['file'], Achievement, {'map': ['title', 'desc']}, auto_save=True)
+            return HttpResponseRedirect('/achievements/import/success', {'count': len(models), 'achievements': models})
+    else:
+        form = ImportForm()
+    return render('achievements/import.html', {'form': form})

File hard/__init__.py

Empty file added.

File hard/forms.py

+from django import forms
+
+class RegisterForm(forms.Form):
+    username       = forms.CharField(max_length=32)
+    password       = forms.PasswordInput()
+    password_check = forms.PasswordInput()

File hard/models.py

+from django.db import models
+
+# Create your models here.

File hard/templates/base.html

+<!DOCTYPE html>
+<html>
+  <head>
+    {% block meta_tags %}
+    {% endblock %}
+    <title>H.A.R.D. - {% block title %}{% endblock %}</title>
+  </head>
+  <body>
+  {% block body %}
+    <center><h1>Project H.A.R.D.</h1></center>
+  {% endblock %}
+  </body>
+</html>

File hard/tests.py

+"""
+This file demonstrates writing tests using the unittest module. These will pass
+when you run "manage.py test".
+
+Replace this with more appropriate tests for your application.
+"""
+
+from django.test import TestCase
+
+
+class SimpleTest(TestCase):
+    def test_basic_addition(self):
+        """
+        Tests that 1 + 1 always equals 2.
+        """
+        self.assertEqual(1 + 1, 2)

File hard/urls.py

+# Application: hard
+# Role: Glue together all other applications related to project HARD
+from django.conf.urls.defaults import patterns, include, url
+
+urlpatterns = patterns('',
+    url(r'^$', 'hard.views.index', name='global index'),
+)

File hard/views.py

+from django.http import HttpResponse
+from django.shortcuts import render
     'django.contrib.staticfiles',
     'django.contrib.admin',
 # -------------------------------
+    'HARD.hard', # Controller App
     'HARD.achievements',
 )
 

File templates/base.html

-<!DOCTYPE html>
-<html>
-  <head>
-    {% block meta_tags %}
-    {% endblock %}
-    <title>H.A.R.D. - {% block title %}{% endblock %}</title>
-  </head>
-  <body>
-  {% block body %}
-    <center><h1>Project H.A.R.D.</h1></center>
-  {% endblock %}
-  </body>
-</html>