From 846b69463a94546a97e392f8f7ade04669df1537 Mon Sep 17 00:00:00 2001 From: Radek Czajka Date: Tue, 9 Oct 2012 12:10:51 +0200 Subject: [PATCH] New fnpdjango app with lots of common utils. --- contact/__init__.py | 2 +- contact/views.py | 2 +- events/__init__.py | 2 +- events/admin.py | 2 +- events/models.py | 2 +- events/search_indexes.py | 2 +- events/templates/events/event_list.html | 2 +- .../templatetags => fnpdjango}/__init__.py | 0 {migdal => fnpdjango}/middleware.py | 13 ++- contact/utils.py => fnpdjango/models.py | 0 .../templates/fnpdjango}/prevnext/next.html | 0 .../fnpdjango}/prevnext/previous.html | 0 .../fnpdjango}/prevnext/prevnext.html | 2 +- .../templates/fnpdjango}/share.html | 2 +- fnpdjango/templatetags/__init__.py | 0 .../templatetags/fnp_common.py | 0 fnpdjango/templatetags/fnp_markup.py | 14 +++ .../templatetags/fnp_prevnext.py | 6 +- .../templatetags/fnp_share.py | 2 +- fnpdjango/utils/__init__.py | 0 fnpdjango/utils/app.py | 30 +++++++ .../utils/models/__init__.py | 5 ++ fnpdjango/utils/models/translation.py | 57 ++++++++++++ fnpdjango/utils/settings.py | 24 +++++ fnpdjango/utils/text/__init__.py | 3 + fnpdjango/utils/text/slughifi.py | 56 ++++++++++++ fnpdjango/utils/text/textilepl.py | 22 +++++ fnpdjango/utils/urls.py | 40 +++++++++ fnpdjango/utils/views.py | 17 ++++ migdal/__init__.py | 5 +- migdal/admin.py | 4 +- migdal/api.py | 5 +- migdal/feeds.py | 4 +- migdal/forms.py | 6 +- migdal/helpers.py | 89 ------------------- migdal/models.py | 2 +- migdal/readme.md | 1 + migdal/search_indexes.py | 4 +- .../templates/comments/migdal/entry/list.html | 2 +- .../comments/migdal/entry/preview.html | 2 +- .../templates/migdal/entry/entry_detail.html | 2 +- migdal/templates/migdal/entry/entry_list.html | 2 +- .../templates/migdal/entry/entry_short.html | 2 +- migdal/templates/migdal/last_comments.html | 2 +- migdal/urls.py | 2 +- prawokultury/helpers.py | 77 ---------------- prawokultury/settings.d/30-apps.conf | 6 +- prawokultury/settings.d/35-search.conf | 3 +- prawokultury/settings.d/40-middleware.conf | 4 +- prawokultury/settings.d/50-contrib.conf | 2 +- prawokultury/settings.d/60-custom.conf | 2 +- prawokultury/static/css/search.css | 2 +- prawokultury/static/css/search.scss | 2 +- prawokultury/templates/base.html | 3 +- prawokultury/templatetags/markup_tags.py | 18 ---- prawokultury/urls.py | 4 +- realip_middleware.py | 7 -- 57 files changed, 327 insertions(+), 244 deletions(-) rename {prawokultury/templatetags => fnpdjango}/__init__.py (100%) rename {migdal => fnpdjango}/middleware.py (72%) rename contact/utils.py => fnpdjango/models.py (100%) mode change 100755 => 100644 rename {prawokultury/templates => fnpdjango/templates/fnpdjango}/prevnext/next.html (100%) rename {prawokultury/templates => fnpdjango/templates/fnpdjango}/prevnext/previous.html (100%) rename {prawokultury/templates => fnpdjango/templates/fnpdjango}/prevnext/prevnext.html (67%) rename {prawokultury/templates => fnpdjango/templates/fnpdjango}/share.html (97%) create mode 100755 fnpdjango/templatetags/__init__.py rename prawokultury/templatetags/common_tags.py => fnpdjango/templatetags/fnp_common.py (100%) create mode 100755 fnpdjango/templatetags/fnp_markup.py rename prawokultury/templatetags/prevnext.py => fnpdjango/templatetags/fnp_prevnext.py (75%) rename prawokultury/templatetags/share.py => fnpdjango/templatetags/fnp_share.py (78%) create mode 100644 fnpdjango/utils/__init__.py create mode 100755 fnpdjango/utils/app.py rename prawokultury/model_helpers.py => fnpdjango/utils/models/__init__.py (95%) mode change 100755 => 100644 create mode 100644 fnpdjango/utils/models/translation.py create mode 100644 fnpdjango/utils/settings.py create mode 100644 fnpdjango/utils/text/__init__.py create mode 100644 fnpdjango/utils/text/slughifi.py create mode 100644 fnpdjango/utils/text/textilepl.py create mode 100644 fnpdjango/utils/urls.py create mode 100644 fnpdjango/utils/views.py create mode 100644 migdal/readme.md delete mode 100644 prawokultury/helpers.py delete mode 100644 prawokultury/templatetags/markup_tags.py delete mode 100755 realip_middleware.py diff --git a/contact/__init__.py b/contact/__init__.py index 5e8bf90..0f043ed 100644 --- a/contact/__init__.py +++ b/contact/__init__.py @@ -42,7 +42,7 @@ some_template.html: """ -from prawokultury.helpers import AppSettings +from fnpdjango.utils.app import AppSettings class Settings(AppSettings): diff --git a/contact/views.py b/contact/views.py index 5d4a62a..e4dfeef 100644 --- a/contact/views.py +++ b/contact/views.py @@ -2,7 +2,7 @@ from django.contrib.auth.decorators import permission_required from django.http import Http404, HttpResponse from django.shortcuts import get_object_or_404, redirect, render from django.utils.translation import ugettext_lazy as _ -from prawokultury.helpers import serve_file +from fnpdjango.utils.views import serve_file from .forms import contact_forms from .models import Attachment diff --git a/events/__init__.py b/events/__init__.py index 9de4ba2..300aa93 100644 --- a/events/__init__.py +++ b/events/__init__.py @@ -1,4 +1,4 @@ -from prawokultury.helpers import AppSettings +from fnpdjango.utils.app import AppSettings class Settings(AppSettings): diff --git a/events/admin.py b/events/admin.py index 794fa52..b584b6f 100644 --- a/events/admin.py +++ b/events/admin.py @@ -4,8 +4,8 @@ # from django.conf import settings from django.contrib import admin +from fnpdjango.utils.models.translation import translated_fields from events.models import Event -from migdal.helpers import translated_fields class EventAdmin(admin.ModelAdmin): diff --git a/events/models.py b/events/models.py index e20ae53..aa36591 100644 --- a/events/models.py +++ b/events/models.py @@ -6,7 +6,7 @@ from django.conf import settings from django.core.exceptions import ValidationError from django.db import models from django.utils.translation import ugettext_lazy as _, ugettext -from migdal.helpers import add_translatable +from fnpdjango.utils.models.translation import add_translatable class Event(models.Model): diff --git a/events/search_indexes.py b/events/search_indexes.py index 9977957..2fe31cb 100644 --- a/events/search_indexes.py +++ b/events/search_indexes.py @@ -2,7 +2,7 @@ from django.conf import settings import datetime from haystack import indexes from events.models import Event -from migdal.helpers import add_translatable_index +from fnpdjango.utils.models.translation import add_translatable_index class EventIndex(indexes.RealTimeSearchIndex, indexes.Indexable): diff --git a/events/templates/events/event_list.html b/events/templates/events/event_list.html index 66c5184..b6dd773 100755 --- a/events/templates/events/event_list.html +++ b/events/templates/events/event_list.html @@ -1,6 +1,6 @@ {% extends "base.html" %} {% load url from future %} -{% load i18n pagination_tags prevnext %} +{% load i18n pagination_tags fnp_prevnext %} {% load events_tags %} {% block "body" %} diff --git a/prawokultury/templatetags/__init__.py b/fnpdjango/__init__.py similarity index 100% rename from prawokultury/templatetags/__init__.py rename to fnpdjango/__init__.py diff --git a/migdal/middleware.py b/fnpdjango/middleware.py similarity index 72% rename from migdal/middleware.py rename to fnpdjango/middleware.py index 7153562..21de872 100644 --- a/migdal/middleware.py +++ b/fnpdjango/middleware.py @@ -1,12 +1,17 @@ -# -*- coding: utf-8 -*- -# This file is part of PrawoKultury, licensed under GNU Affero GPLv3 or later. -# Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information. -# from django.utils import translation from django.conf import settings from django.http import Http404 +class SetRemoteAddrFromXRealIP(object): + """Sets REMOTE_ADDR from the X-Real-IP header, as set by Nginx.""" + def process_request(self, request): + try: + request.META['REMOTE_ADDR'] = request.META['HTTP_X_REAL_IP'] + except KeyError: + return None + + class URLLocaleMiddleware(object): """Decides which translation to use, based on path only.""" diff --git a/contact/utils.py b/fnpdjango/models.py old mode 100755 new mode 100644 similarity index 100% rename from contact/utils.py rename to fnpdjango/models.py diff --git a/prawokultury/templates/prevnext/next.html b/fnpdjango/templates/fnpdjango/prevnext/next.html similarity index 100% rename from prawokultury/templates/prevnext/next.html rename to fnpdjango/templates/fnpdjango/prevnext/next.html diff --git a/prawokultury/templates/prevnext/previous.html b/fnpdjango/templates/fnpdjango/prevnext/previous.html similarity index 100% rename from prawokultury/templates/prevnext/previous.html rename to fnpdjango/templates/fnpdjango/prevnext/previous.html diff --git a/prawokultury/templates/prevnext/prevnext.html b/fnpdjango/templates/fnpdjango/prevnext/prevnext.html similarity index 67% rename from prawokultury/templates/prevnext/prevnext.html rename to fnpdjango/templates/fnpdjango/prevnext/prevnext.html index 8a5cd04..fd3f9b3 100755 --- a/prawokultury/templates/prevnext/prevnext.html +++ b/fnpdjango/templates/fnpdjango/prevnext/prevnext.html @@ -1,4 +1,4 @@ -{% load prevnext %} +{% load fnp_prevnext %}

{% next_page %} {% previous_page %} diff --git a/prawokultury/templates/share.html b/fnpdjango/templates/fnpdjango/share.html similarity index 97% rename from prawokultury/templates/share.html rename to fnpdjango/templates/fnpdjango/share.html index 71b2c0e..c42e81b 100755 --- a/prawokultury/templates/share.html +++ b/fnpdjango/templates/fnpdjango/share.html @@ -1,4 +1,4 @@ -{% load i18n common_tags static %} +{% load i18n fnp_common static %} {% with encoded_url=url|build_absolute_uri:request|urlencode encoded_desc=description|urlencode %}

{% trans "Comments" %}

diff --git a/migdal/templates/comments/migdal/entry/preview.html b/migdal/templates/comments/migdal/entry/preview.html index 6d2b5ff..ae3eb92 100755 --- a/migdal/templates/comments/migdal/entry/preview.html +++ b/migdal/templates/comments/migdal/entry/preview.html @@ -1,6 +1,6 @@ {% extends "base.html" %} {% load comments gravatar i18n %} -{% load migdal_tags markup_tags %} +{% load migdal_tags fnp_markup %} {% block "main_menu" %} diff --git a/migdal/templates/migdal/entry/entry_detail.html b/migdal/templates/migdal/entry/entry_detail.html index 5827d08..92bf2dd 100755 --- a/migdal/templates/migdal/entry/entry_detail.html +++ b/migdal/templates/migdal/entry/entry_detail.html @@ -1,6 +1,6 @@ {% extends "base.html" %} {% load comments i18n %} -{% load common_tags migdal_tags share %} +{% load fnp_common migdal_tags fnp_share %} {% block "titleextra" %}{{ entry.title }} :: {% endblock %} diff --git a/migdal/templates/migdal/entry/entry_list.html b/migdal/templates/migdal/entry/entry_list.html index eede699..bd9bbf6 100755 --- a/migdal/templates/migdal/entry/entry_list.html +++ b/migdal/templates/migdal/entry/entry_list.html @@ -1,7 +1,7 @@ {% extends "base.html" %} {% load url from future %} {% load i18n %} -{% load pagination_tags prevnext %} +{% load pagination_tags fnp_prevnext %} {% load migdal_tags %} diff --git a/migdal/templates/migdal/entry/entry_short.html b/migdal/templates/migdal/entry/entry_short.html index 31dbf9e..7a5eb25 100755 --- a/migdal/templates/migdal/entry/entry_short.html +++ b/migdal/templates/migdal/entry/entry_short.html @@ -1,6 +1,6 @@ {% load i18n %} {% load comments %} -{% load migdal_tags share %} +{% load migdal_tags fnp_share %}
diff --git a/migdal/templates/migdal/last_comments.html b/migdal/templates/migdal/last_comments.html index f26cfea..8deba13 100755 --- a/migdal/templates/migdal/last_comments.html +++ b/migdal/templates/migdal/last_comments.html @@ -1,5 +1,5 @@ {% load gravatar %} -{% load markup_tags %} +{% load fnp_markup %}
    {% for comment in object_list %}
  • diff --git a/migdal/urls.py b/migdal/urls.py index 3a58123..310a648 100644 --- a/migdal/urls.py +++ b/migdal/urls.py @@ -7,7 +7,7 @@ from django.utils.translation import ugettext_lazy as _ from django.utils.translation import string_concat from migdal import feeds, app_settings from migdal.views import SearchPublishedView -from migdal.helpers import i18n_patterns +from fnpdjango.utils.urls import i18n_patterns pats = [] for t in app_settings.TYPES: diff --git a/prawokultury/helpers.py b/prawokultury/helpers.py deleted file mode 100644 index 9b943de..0000000 --- a/prawokultury/helpers.py +++ /dev/null @@ -1,77 +0,0 @@ -# -*- coding: utf-8 -*- -# This file is part of PrawoKultury, licensed under GNU Affero GPLv3 or later. -# Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information. -# -from django.conf import settings -from django.http import HttpResponse, HttpResponseRedirect -from textile import Textile - - -class TextilePL(Textile): - glyph_defaults = [(name, repl) - for (name, repl) in Textile.glyph_defaults - if name != 'txt_quote_double_open'] - glyph_defaults.append(('txt_quote_double_open', '„')) - - -def textile_pl(text): - return TextilePL().textile(text) - - -def textile_restricted_pl(text): - return TextilePL(restricted=True, lite=True, - noimage=True, auto_link=False).textile( - text, rel='nofollow') - - -class LazyUGettextLazy(object): - """You can use it to internationalize strings in settings. - - Just import this class as gettext. - """ - _ = lambda s: s - real = False - - def __init__(self, text): - self.text = text - - def __unicode__(self): - if not self.real: - from django.utils.translation import ugettext_lazy - LazyUGettextLazy._ = staticmethod(ugettext_lazy) - LazyUGettextLazy.real = True - return unicode(self._(self.text)) - - -class AppSettings(object): - """Allows specyfying custom settings for an app, with default values. - - Just subclass, set some properties and instantiate with a prefix. - Getting a SETTING from an instance will check for prefix_SETTING - in project settings if set, else take the default. The value will be - then filtered through _more_SETTING method, if there is one. - - """ - def __init__(self, prefix): - self._prefix = prefix - - def __getattribute__(self, name): - if name.startswith('_'): - return object.__getattribute__(self, name) - value = getattr(settings, - "%s_%s" % (self._prefix, name), - object.__getattribute__(self, name)) - more = "_more_%s" % name - if hasattr(self, more): - value = getattr(self, more)(value) - return value - - -def serve_file(url): - if settings.X_ACCEL_REDIRECT: - response = HttpResponse() - response['Content-Type'] = "" - response['X-Accel-Redirect'] = url - return response - else: - return HttpResponseRedirect(url) diff --git a/prawokultury/settings.d/30-apps.conf b/prawokultury/settings.d/30-apps.conf index 60b1f96..f466de7 100755 --- a/prawokultury/settings.d/30-apps.conf +++ b/prawokultury/settings.d/30-apps.conf @@ -1,13 +1,15 @@ INSTALLED_APPS = ( 'prawokultury', + 'fnpdjango', + #'events', 'migdal', 'contact', 'gravatar', 'south', - #'django.contrib.comments', - #'django_comments_xtd', + 'django.contrib.comments', + 'django_comments_xtd', 'pipeline', 'haystack', 'pagination', diff --git a/prawokultury/settings.d/35-search.conf b/prawokultury/settings.d/35-search.conf index 62b5799..15667a3 100644 --- a/prawokultury/settings.d/35-search.conf +++ b/prawokultury/settings.d/35-search.conf @@ -5,5 +5,4 @@ HAYSTACK_CONNECTIONS = { }, } -from django.conf import settings -HAYSTACK_DOCUMENT_FIELD = "body_%s" % settings.LANGUAGE_CODE +HAYSTACK_DOCUMENT_FIELD = "body_%s" % LANGUAGE_CODE diff --git a/prawokultury/settings.d/40-middleware.conf b/prawokultury/settings.d/40-middleware.conf index cde9886..530e34a 100755 --- a/prawokultury/settings.d/40-middleware.conf +++ b/prawokultury/settings.d/40-middleware.conf @@ -1,7 +1,7 @@ MIDDLEWARE_CLASSES = ( 'django.contrib.sessions.middleware.SessionMiddleware', #'django.middleware.locale.LocaleMiddleware', - 'migdal.middleware.URLLocaleMiddleware', + 'fnpdjango.middleware.URLLocaleMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', @@ -19,5 +19,5 @@ MIDDLEWARE_CLASSES += ( # Uncomment the next line for simple clickjacking protection: # 'django.middleware.clickjacking.XFrameOptionsMiddleware', 'pagination.middleware.PaginationMiddleware', - 'realip_middleware.SetRemoteAddrFromXRealIP', + 'fnpdjango.middleware.SetRemoteAddrFromXRealIP', ) diff --git a/prawokultury/settings.d/50-contrib.conf b/prawokultury/settings.d/50-contrib.conf index 27a516c..55c9811 100755 --- a/prawokultury/settings.d/50-contrib.conf +++ b/prawokultury/settings.d/50-contrib.conf @@ -1,4 +1,4 @@ -from prawokultury.helpers import textile_pl +from fnpdjango.utils.text.textilepl import textile_pl COMMENTS_APP = "django_comments_xtd" COMMENTS_XTD_CONFIRM_EMAIL = False diff --git a/prawokultury/settings.d/60-custom.conf b/prawokultury/settings.d/60-custom.conf index 6b26db5..9fec2d5 100755 --- a/prawokultury/settings.d/60-custom.conf +++ b/prawokultury/settings.d/60-custom.conf @@ -1,4 +1,4 @@ -from prawokultury.helpers import LazyUGettextLazy as gettext +from fnpdjango.utils.settings import LazyUGettextLazy as gettext from migdal.helpers import EntryType MIGDAL_TYPES = ( diff --git a/prawokultury/static/css/search.css b/prawokultury/static/css/search.css index 6bc635c..77451ce 100644 --- a/prawokultury/static/css/search.css +++ b/prawokultury/static/css/search.css @@ -1,5 +1,5 @@ #search-form { - margin-top: .4em; } + margin: .4em 0 0 0; } #search-form input { font-size: 1.1em; width: 16.1em; diff --git a/prawokultury/static/css/search.scss b/prawokultury/static/css/search.scss index 9b9bbe1..f018c15 100644 --- a/prawokultury/static/css/search.scss +++ b/prawokultury/static/css/search.scss @@ -1,5 +1,5 @@ #search-form { - margin-top: .4em; + margin: .4em 0 0 0; input { font-size: 1.1em; diff --git a/prawokultury/templates/base.html b/prawokultury/templates/base.html index 57ea892..6473962 100755 --- a/prawokultury/templates/base.html +++ b/prawokultury/templates/base.html @@ -1,7 +1,7 @@ {% load url from future %} {% load i18n static %} -{% load common_tags migdal_tags share %} +{% load fnp_common migdal_tags fnp_share %} {% load compressed %} @@ -27,6 +27,7 @@
diff --git a/prawokultury/templatetags/markup_tags.py b/prawokultury/templatetags/markup_tags.py deleted file mode 100644 index 9f1117e..0000000 --- a/prawokultury/templatetags/markup_tags.py +++ /dev/null @@ -1,18 +0,0 @@ -# -*- coding: utf-8 -*- -# This file is part of PrawoKultury, licensed under GNU Affero GPLv3 or later. -# Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information. -# -from django import template -from django.utils.safestring import mark_safe -from prawokultury import helpers - -register = template.Library() - - -@register.filter -def textile_pl(node): - return mark_safe(helpers.textile_pl(node)) - -@register.filter -def textile_restricted_pl(node): - return mark_safe(helpers.textile_restricted_pl(node)) diff --git a/prawokultury/urls.py b/prawokultury/urls.py index 7ba9209..049edac 100644 --- a/prawokultury/urls.py +++ b/prawokultury/urls.py @@ -6,10 +6,10 @@ from django.conf import settings from django.conf.urls import patterns, include, url from django.contrib import admin from django.contrib.staticfiles.urls import staticfiles_urlpatterns -from migdal.helpers import i18n_patterns -from migdal.urls import urlpatterns as migdal_urlpatterns from django.utils.translation import ugettext_lazy as _, string_concat +from fnpdjango.utils.urls import i18n_patterns from events.urls import urlpatterns as events_urlpatterns +from migdal.urls import urlpatterns as migdal_urlpatterns admin.autodiscover() diff --git a/realip_middleware.py b/realip_middleware.py deleted file mode 100755 index 9f97b7f..0000000 --- a/realip_middleware.py +++ /dev/null @@ -1,7 +0,0 @@ -class SetRemoteAddrFromXRealIP(object): - """Sets REMOTE_ADDR from the X-Real-IP header, as set by Nginx.""" - def process_request(self, request): - try: - request.META['REMOTE_ADDR'] = request.META['HTTP_X_REAL_IP'] - except KeyError: - return None -- 2.20.1