From 0fd55a38c4873d5916a3553d7510b9308f4eee6d Mon Sep 17 00:00:00 2001 From: =?utf8?q?=C5=81ukasz=20Rekucki?= Date: Mon, 22 Mar 2010 13:47:17 +0100 Subject: [PATCH 1/1] Removed the global evil. --- apps/wiki/forms.py | 9 ++++----- apps/wiki/models.py | 4 ++-- apps/wiki/views.py | 13 +++++++++---- 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/apps/wiki/forms.py b/apps/wiki/forms.py index d816cf0e..db5998de 100644 --- a/apps/wiki/forms.py +++ b/apps/wiki/forms.py @@ -1,5 +1,5 @@ from django import forms -from wiki.models import Document, storage +from wiki.models import Document, getstorage class DocumentForm(forms.Form): @@ -15,12 +15,11 @@ class DocumentForm(forms.Form): self.fields['name'].initial = document.name self.fields['text'].initial = document.text self.fields['revision'].initial = document.revision() - - def get_storage(self): - return storage def save(self, document_author = 'anonymous'): - document = Document(self.get_storage(), name=self.cleaned_data['name'], text=self.cleaned_data['text']) + storage = getstorage() + + document = Document(storage, name=self.cleaned_data['name'], text=self.cleaned_data['text']) storage.put(document, author = document_author, diff --git a/apps/wiki/models.py b/apps/wiki/models.py index 94010419..8ec4d32c 100644 --- a/apps/wiki/models.py +++ b/apps/wiki/models.py @@ -67,5 +67,5 @@ class Document(object): return result -# Every time somebody says "let's have a global variable", God kills a kitten. -storage = DocumentStorage(settings.REPOSITORY_PATH) +def getstorage(): + return DocumentStorage(settings.REPOSITORY_PATH) diff --git a/apps/wiki/views.py b/apps/wiki/views.py index a2de0a05..78a19422 100644 --- a/apps/wiki/views.py +++ b/apps/wiki/views.py @@ -5,7 +5,7 @@ from django.views.generic.simple import direct_to_template from django.http import HttpResponse, Http404 from django.utils import simplejson as json -from wiki.models import storage, Document, DocumentNotFound +from wiki.models import Document, DocumentNotFound, getstorage from wiki.forms import DocumentForm from datetime import datetime from django.utils.encoding import smart_unicode @@ -33,7 +33,7 @@ class DateTimeEncoder(json.JSONEncoder): def document_list(request, template_name = 'wiki/document_list.html'): # TODO: find a way to cache "Storage All" return direct_to_template(request, template_name, extra_context = { - 'document_list': storage.all(), + 'document_list': getstorage().all(), 'last_docs': sorted(request.session.get("wiki_last_docs", {}).items(), key=operator.itemgetter(1), reverse = True) }) @@ -42,7 +42,7 @@ def document_list(request, template_name = 'wiki/document_list.html'): def document_detail(request, name, template_name = 'wiki/document_details.html'): print "Trying to get", repr(name) try: - document = storage.get(name) + document = getstorage().get(name) except DocumentNotFound: raise Http404 @@ -101,7 +101,8 @@ def document_gallery(request, directory): raise Http404 @never_cache -def document_diff(request, name, revA, revB): +def document_diff(request, name, revA, revB): + storage = getstorage() docA = storage.get(name, int(revA)) docB = storage.get(name, int(revB)) @@ -111,6 +112,8 @@ def document_diff(request, name, revA, revB): @never_cache def document_history(request, name): + storage = getstorage() + return HttpResponse( json.dumps(storage.history(name), cls=DateTimeEncoder), mimetype='application/json') @@ -120,6 +123,8 @@ import urllib, urllib2 @never_cache def document_publish(request, name, version): + storage = getstorage() + # get the document try: document = storage.get(name, revision = int(version)) -- 2.20.1