From f93e908bb4b414f5e78e690649800831f37d5cfa Mon Sep 17 00:00:00 2001 From: nickcknapp Date: Tue, 29 Mar 2016 12:27:46 +0900 Subject: [PATCH 01/22] update settings.py for TEMPLATES and local setting --- .../project_template/project_name/settings.py | 67 ++++++++++++------- 1 file changed, 41 insertions(+), 26 deletions(-) diff --git a/drum/project_template/project_name/settings.py b/drum/project_template/project_name/settings.py index 85bb90b..e4ca336 100644 --- a/drum/project_template/project_name/settings.py +++ b/drum/project_template/project_name/settings.py @@ -191,12 +191,32 @@ # Package/module name to import the root urlpatterns from for the project. ROOT_URLCONF = "%s.urls" % PROJECT_APP -# Put strings here, like "/home/html/django_templates" -# or "C:/www/django/templates". -# Always use forward slashes, even on Windows. -# Don't forget to use absolute paths, not relative paths. -TEMPLATE_DIRS = (os.path.join(PROJECT_ROOT, "templates"),) - +TEMPLATES = [ + { + "BACKEND": "django.template.backends.django.DjangoTemplates", + "DIRS": [ + os.path.join(PROJECT_ROOT, "templates") + ], + "APP_DIRS": True, + "OPTIONS": { + "context_processors": [ + "django.contrib.auth.context_processors.auth", + "django.contrib.messages.context_processors.messages", + "django.template.context_processors.debug", + "django.template.context_processors.i18n", + "django.template.context_processors.static", + "django.template.context_processors.media", + "django.template.context_processors.request", + "django.template.context_processors.tz", + "mezzanine.conf.context_processors.settings", + "mezzanine.pages.context_processors.page", + ], + "builtins": [ + "mezzanine.template.loader_tags", + ], + }, + }, +] ################ # APPLICATIONS # @@ -225,21 +245,6 @@ #"mezzanine.mobile", ) -# List of processors used by RequestContext to populate the context. -# Each one should be a callable that takes the request object as its -# only parameter and returns a dictionary to add to the context. -TEMPLATE_CONTEXT_PROCESSORS = ( - "django.contrib.auth.context_processors.auth", - "django.contrib.messages.context_processors.messages", - "django.core.context_processors.debug", - "django.core.context_processors.i18n", - "django.core.context_processors.static", - "django.core.context_processors.media", - "django.core.context_processors.request", - "django.core.context_processors.tz", - "mezzanine.conf.context_processors.settings", - "mezzanine.pages.context_processors.page", -) # List of middleware classes to use. Order is important; in the request phase, # these middleware classes will be applied in the order given, and in the @@ -315,11 +320,21 @@ # Allow any settings to be defined in local_settings.py which should be # ignored in your version control system allowing for settings to be # defined per machine. -try: - from .local_settings import * -except ImportError as e: - if "local_settings" not in str(e): - raise e + +# Instead of doing "from .local_settings import *", we use exec so that +# local_settings has full access to everything defined in this module. +# Also force into sys.modules so it's visible to Django's autoreload. + +f = os.path.join(PROJECT_APP_PATH, "local_settings.py") +if os.path.exists(f): + import sys + import imp + module_name = "%s.local_settings" % PROJECT_APP + module = imp.new_module(module_name) + module.__file__ = f + sys.modules[module_name] = module + exec(open(f, "rb").read()) + #################### From a9174133fec29b4b37d827df714e8958002a89c5 Mon Sep 17 00:00:00 2001 From: nickcknapp Date: Mon, 4 Apr 2016 16:48:30 +1000 Subject: [PATCH 02/22] Added Minwoo Park to AUTHORS. --- AUTHORS | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/AUTHORS b/AUTHORS index 48f909d..3fc45b7 100644 --- a/AUTHORS +++ b/AUTHORS @@ -4,4 +4,5 @@ * Vitaliy Kharin * Alex Bendig * Rafael Capdevielle -* Micah Yoder \ No newline at end of file +* Micah Yoder +* Minwoo Park \ No newline at end of file From e79eab74065431610fae6c23e5de72f91c971b93 Mon Sep 17 00:00:00 2001 From: nickcknapp Date: Mon, 18 Apr 2016 16:52:38 +1000 Subject: [PATCH 03/22] Comment out pages settings. Closes #32. --- drum/project_template/project_name/settings.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/drum/project_template/project_name/settings.py b/drum/project_template/project_name/settings.py index e4ca336..5b42acd 100644 --- a/drum/project_template/project_name/settings.py +++ b/drum/project_template/project_name/settings.py @@ -209,7 +209,7 @@ "django.template.context_processors.request", "django.template.context_processors.tz", "mezzanine.conf.context_processors.settings", - "mezzanine.pages.context_processors.page", + # "mezzanine.pages.context_processors.page", ], "builtins": [ "mezzanine.template.loader_tags", @@ -270,7 +270,7 @@ "mezzanine.core.middleware.SitePermissionMiddleware", # Uncomment the following if using any of the SSL settings: # "mezzanine.core.middleware.SSLRedirectMiddleware", - "mezzanine.pages.middleware.PageMiddleware", + # "mezzanine.pages.middleware.PageMiddleware", "mezzanine.core.middleware.FetchFromCacheMiddleware", ) From 81615e9e24fe89ce94ef0704dee86fbd1b403f57 Mon Sep 17 00:00:00 2001 From: nickcknapp Date: Thu, 14 Jul 2016 11:48:39 +1000 Subject: [PATCH 04/22] Update settings module to latest Mezzanine. --- drum/project_template/project_name/settings.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/drum/project_template/project_name/settings.py b/drum/project_template/project_name/settings.py index 5b42acd..fbbf85f 100644 --- a/drum/project_template/project_name/settings.py +++ b/drum/project_template/project_name/settings.py @@ -1,6 +1,8 @@ from __future__ import absolute_import, unicode_literals import os + +from django import VERSION as DJANGO_VERSION from django.utils.translation import ugettext_lazy as _ @@ -218,6 +220,10 @@ }, ] +if DJANGO_VERSION < (1, 9): + del TEMPLATES[0]["OPTIONS"]["builtins"] + + ################ # APPLICATIONS # ################ @@ -298,7 +304,7 @@ ######## # Drum-specific Mezzanine settings -AUTH_PROFILE_MODULE = "links.Profile" +ACCOUNTS_PROFILE_MODEL = "links.Profile" SITE_TITLE = "Drum" RATINGS_RANGE = (-1, 1) RATINGS_ACCOUNT_REQUIRED = True From d4e82ae7fdd108eeffd6bfce08f2c1daf0c92b52 Mon Sep 17 00:00:00 2001 From: nickcknapp Date: Thu, 14 Jul 2016 11:52:13 +1000 Subject: [PATCH 05/22] Note the Keywords admin section being synonymous with tags. --- README.rst | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/README.rst b/README.rst index a2a8748..6f1b98b 100644 --- a/README.rst +++ b/README.rst @@ -94,12 +94,12 @@ given title is broken up into keywords, and if those keywords already exist as tags in the database, they're applied to the newly added link. This means that for auto-tagging to work, the tags must already exist -in the database. You can either add them manually via the admin, or -if you have a large number of existing links, you can use the -``auto_tag`` management command Drum provides, which will analyse the -titles of all your existing links, and provide tags it extracts from -them. This makes use of the `topia.termextract`_ package which -you'll first need to install:: +in the database. You can either add them manually via the admin (under +the "Keywords" section), or if you have a large number of existing +links, you can use the ``auto_tag`` management command Drum provides, +which will analyse the titles of all your existing links, and provide +tags it extracts from them. This makes use of the `topia.termextract`_ +package which you'll first need to install:: python manage.py auto_tag --generate=100 --assign --remove From d039270c2d9c81f8bbfd4994a9bde14b563ee5e8 Mon Sep 17 00:00:00 2001 From: nickcknapp Date: Fri, 15 Jul 2016 15:14:39 +1000 Subject: [PATCH 06/22] Add the AUTO_TAG_FUNCTION setting, allowing tag extraction to be customized. --- README.rst | 6 ++++++ drum/links/models.py | 9 ++++----- drum/links/utils.py | 16 ++++++++++++++-- 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/README.rst b/README.rst index 6f1b98b..9b2978b 100644 --- a/README.rst +++ b/README.rst @@ -113,6 +113,12 @@ all tags in the database to all links in the database, as would occur if they were newly created. The ``--remove`` option will cause all existing tags to be removed. +You can also define your own tag extraction function, if splitting the +title on spaces doesn't suffice. To do so, define the setting +``AUTO_TAG_FUNCTION`` which should contain a string with the Python +dotted path to your custom tag function. The function will be given an +unsaved ``Link`` object, and should return a sequence of tags to add. + Contributing ============ diff --git a/drum/links/models.py b/drum/links/models.py index 9e5d098..524490b 100644 --- a/drum/links/models.py +++ b/drum/links/models.py @@ -2,7 +2,6 @@ from future import standard_library from future.builtins import int -from re import sub, split from time import time from operator import ior from functools import reduce @@ -25,6 +24,7 @@ from mezzanine.core.request import current_request from mezzanine.generic.models import Rating, Keyword, AssignedKeyword from mezzanine.generic.fields import RatingField, CommentsField +from mezzanine.utils.importing import import_dotted_path from mezzanine.utils.urls import slugify @@ -54,10 +54,9 @@ def url(self): def save(self, *args, **kwargs): keywords = [] if not self.keywords_string and getattr(settings, "AUTO_TAG", False): - variations = lambda word: [word, - sub("^([^A-Za-z0-9])*|([^A-Za-z0-9]|s)*$", "", word), - sub("^([^A-Za-z0-9])*|([^A-Za-z0-9])*$", "", word)] - keywords = sum(map(variations, split("\s|/", self.title)), []) + func_name = getattr(settings, "AUTO_TAG_FUNCTION", + "drum.links.utils.auto_tag") + keywords = import_dotted_path(func_name)(self) super(Link, self).save(*args, **kwargs) if keywords: lookup = reduce(ior, [Q(title__iexact=k) for k in keywords]) diff --git a/drum/links/utils.py b/drum/links/utils.py index 5f0a834..c0d2bca 100644 --- a/drum/links/utils.py +++ b/drum/links/utils.py @@ -1,5 +1,6 @@ -from __future__ import division -from __future__ import unicode_literals +from __future__ import division, unicode_literals + +from re import sub, split from django.conf import settings from django.utils.timezone import now @@ -44,3 +45,14 @@ def order_by_score(queryset, score_fields, date_field, reverse=True): score = score_fields_sum / pow(age, scale) setattr(obj, "score", score) return sorted(queryset, key=lambda obj: obj.score, reverse=reverse) + + +def auto_tag(link_obj): + """ + Split's the link object's title into words. Default function for the + ``AUTO_TAG_FUNCTION`` setting. + """ + variations = lambda word: [word, + sub("^([^A-Za-z0-9])*|([^A-Za-z0-9]|s)*$", "", word), + sub("^([^A-Za-z0-9])*|([^A-Za-z0-9])*$", "", word)] + return sum(map(variations, split("\s|/", link_obj.title)), []) From ff2e18cbb5d6a88a5d4748e9ef8507cfaa1c5104 Mon Sep 17 00:00:00 2001 From: nickcknapp Date: Fri, 15 Jul 2016 15:15:02 +1000 Subject: [PATCH 07/22] Fix admin styling for link list. --- drum/links/templates/admin/links/link/change_list.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drum/links/templates/admin/links/link/change_list.html b/drum/links/templates/admin/links/link/change_list.html index f89b172..8f01665 100644 --- a/drum/links/templates/admin/links/link/change_list.html +++ b/drum/links/templates/admin/links/link/change_list.html @@ -2,7 +2,7 @@ {% block extrahead %} {{ block.super }}