Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added whitespace-underscore translation

  • Loading branch information...
commit 102a76dbffd9cda9126951b2507fa08d773dde1d 1 parent b61d4ab
@emesik authored
View
17 README.rst
@@ -22,6 +22,23 @@ Requirements
with any other thumbnailing module or without one at all.*
https://github.com/sorl/sorl-thumbnail
+Settings
+--------
+
+The following settings configure Djiki's behavior:
+
+``DJIKI_IMAGES_PATH`` — path to images, relative to MEDIA_ROOT.
+
+``DJIKI_ALLOW_ANONYMOUS_EDITS`` — whether unauthorized users are
+able to edit pages.
+
+``DJIKI_SPACES_AS_UNDERSCORES`` — makes Djiki replace whitespaces in
+URLs by underscores. It's a choice between having nice or exact URLs.
+Depending on the settings, the adresses may look as
+``http://djiki.org/wiki/Main_Page`` or ``http://djiki.org/wiki/Main%20Page``
+This setting will also squash multiple spaces into one. It affects image
+names in the same way, too.
+
Images
------
View
8 djiki/forms.py
@@ -1,7 +1,7 @@
from django import forms
from django.utils.translation import ugettext as _
from diff_match_patch import diff_match_patch
-from . import models
+from . import models, utils
class PageEditForm(forms.ModelForm):
prev_revision = forms.ModelChoiceField(
@@ -95,17 +95,17 @@ class Meta:
def _get_name(self):
name = self.cleaned_data['name']
if not name:
- name = self.cleaned_data['file']
+ name = unicode(self.cleaned_data['file'])
return name
def clean(self):
- if models.Image.objects.filter(name=self._get_name()).exists():
+ if models.Image.objects.filter(name=utils.deurlize_title(self._get_name())).exists():
raise forms.ValidationError(_("An image of the same name already exists. Please enter "\
"different name."))
return self.cleaned_data
def save(self, *args, **kwargs):
- image = models.Image(name=self._get_name())
+ image = models.Image(name=utils.deurlize_title(self._get_name()))
image.save()
self.instance.image = image
super(NewImageUploadForm, self).save(*args, **kwargs)
View
3  djiki/models.py
@@ -60,6 +60,9 @@ class Image(models.Model, Versioned):
class Meta:
ordering = ('name',)
+ def __unicode__(self):
+ return self.name
+
class ImageRevision(Revision):
image = models.ForeignKey(Image, related_name='revisions')
View
5 djiki/parser.py
@@ -4,7 +4,7 @@
from django.core.urlresolvers import reverse
from django.template.loader import render_to_string
-from . import models
+from . import models, utils
class DjikiHtmlEmitter(HtmlEmitter):
image_params_re = re.compile(r'^(?:(?P<size>[0-9]+x[0-9]+)(?:\||$))?(?P<title>.*)$')
@@ -24,8 +24,9 @@ def image_emit(self, node):
raise NotImplementedError
else:
try:
- image = models.Image.objects.get(name=target)
+ image = models.Image.objects.get(name=utils.deurlize_title(target))
ctx['image'] = image
+ ctx['url_name'] = utils.urlize_title(image.name)
except models.Image.DoesNotExist:
pass
return render_to_string('djiki/parser/image.html', ctx)
View
2  djiki/templates/djiki/parser/image.html
@@ -1,6 +1,6 @@
{% load thumbnail %}
<div class="image">
- {% if image %}<a href="{% url djiki-image-view image.name %}">
+ {% if image %}<a href="{% url djiki-image-view url_name %}">
<img src="{% if size %}{% thumbnail image.last_revision.file size crop %}{% else %}{% thumbnail image.last_revision.file 912x912 crop %}{% endif %}" alt="{{ title }}" />
</a>
{% else %}
View
12 djiki/utils.py
@@ -0,0 +1,12 @@
+import re
+from django.conf import settings
+
+def urlize_title(title):
+ if settings.DJIKI_SPACES_AS_UNDERSCORES:
+ return re.sub(r'\s+', '_', title)
+ return title
+
+def deurlize_title(title):
+ if settings.DJIKI_SPACES_AS_UNDERSCORES:
+ return re.sub(r'[_\s]+', ' ', title)
+ return title
View
38 djiki/views.py
@@ -3,13 +3,17 @@
from django.http import HttpResponseRedirect, HttpResponseForbidden
from django.views.generic.simple import direct_to_template
from django.shortcuts import get_object_or_404
-from . import models, forms
+from . import models, forms, utils
def view(request, title, revision_pk=None):
+ url_title = utils.urlize_title(title)
+ if title != url_title:
+ return HttpResponseRedirect(reverse('djiki-page-view', kwargs={'title': url_title}))
+ page_title = utils.deurlize_title(title)
try:
- page = models.Page.objects.get(title=title)
+ page = models.Page.objects.get(title=page_title)
except models.Page.DoesNotExist:
- return direct_to_template(request, 'djiki/not_found.html', {'title': title})
+ return direct_to_template(request, 'djiki/not_found.html', {'title': page_title})
if revision_pk:
try:
revision = page.revisions.get(pk=revision_pk)
@@ -25,11 +29,15 @@ def view(request, title, revision_pk=None):
def edit(request, title):
if not settings.DJIKI_ALLOW_ANONYMOUS_EDITS and not request.user.is_authenticated():
return HttpResponseForbidden()
+ url_title = utils.urlize_title(title)
+ if title != url_title:
+ return HttpResponseRedirect(reverse('djiki-page-edit', kwargs={'title': url_title}))
+ page_title = utils.deurlize_title(title)
try:
- page = models.Page.objects.get(title=title)
+ page = models.Page.objects.get(title=page_title)
last_content = page.last_revision().content
except models.Page.DoesNotExist:
- page = models.Page(title=title)
+ page = models.Page(title=page_title)
last_content = ''
revision = models.PageRevision(page=page,
author=request.user if request.user.is_authenticated() else None)
@@ -60,13 +68,21 @@ def image_new(request):
return direct_to_template(request, 'djiki/image_edit.html', {'form': form})
def image_view(request, name):
- image = get_object_or_404(models.Image, name=name)
+ url_name = utils.urlize_title(name)
+ if name != url_name:
+ return HttpResponseRedirect(reverse('djiki-image-view', kwargs={'name': url_name}))
+ image_name = utils.deurlize_title(name)
+ image = get_object_or_404(models.Image, name=image_name)
return direct_to_template(request, 'djiki/image_view.html', {'image': image})
def image_edit(request, name):
if not settings.DJIKI_ALLOW_ANONYMOUS_EDITS and not request.user.is_authenticated():
return HttpResponseForbidden()
- image = get_object_or_404(models.Image, name=name)
+ url_name = utils.urlize_title(name)
+ if name != url_name:
+ return HttpResponseRedirect(reverse('djiki-image-edit', kwargs={'name': url_name}))
+ image_name = utils.deurlize_title(name)
+ image = get_object_or_404(models.Image, name=image_name)
revision = models.ImageRevision(image=image,
author=request.user if request.user.is_authenticated() else None)
form = forms.ImageUploadForm(data=request.POST or None, files=request.FILES or None,
@@ -75,10 +91,14 @@ def image_edit(request, name):
if form.is_valid():
form.save()
return HttpResponseRedirect(
- reverse('djiki-image-view', kwargs={'name': form.instance.image.name}))
+ reverse('djiki-image-view', kwargs={'name': url_name}))
return direct_to_template(request, 'djiki/image_edit.html', {'form': form})
def image_history(request, name):
- image = get_object_or_404(models.Image, name=name)
+ url_name = utils.urlize_title(name)
+ if name != url_name:
+ return HttpResponseRedirect(reverse('djiki-image-view', kwargs={'name': url_name}))
+ image_name = utils.deurlize_title(name)
+ image = get_object_or_404(models.Image, name=image_name)
history = image.revisions.order_by('-created')
return direct_to_template(request, 'djiki/image_history.html', {'image': image, 'history': history})
View
6 settings.py
@@ -106,6 +106,12 @@
DJIKI_IMAGES_PATH = 'djimages/' # relative to MEDIA_ROOT
DJIKI_ALLOW_ANONYMOUS_EDITS = True
+# The following switch will make all whitespaces appear as underscores
+# in URLs. If you want to have nice URLs, leave it enabled. If you wish
+# to keep distinction between space and underscore and have all page
+# names verbatim, disable it.
+DJIKI_SPACES_AS_UNDERSCORES = True
+
try:
execfile(os.path.join(PROJECT_ROOT, 'local_settings.py'))
except IOError:
View
2  urls.py
@@ -2,7 +2,7 @@
from django.conf.urls.defaults import *
urlpatterns = patterns('',
- (r'^$', 'django.views.generic.simple.redirect_to', {'url': u'/wiki/Main Page'}),
+ (r'^$', 'django.views.generic.simple.redirect_to', {'url': u'/wiki/Main_Page'}),
(r'^wiki/', include('djiki.urls')),
)
Please sign in to comment.
Something went wrong with that request. Please try again.