diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml new file mode 100644 index 0000000..4ae4509 --- /dev/null +++ b/.github/FUNDING.yml @@ -0,0 +1,15 @@ +# These are supported funding model platforms + +github: vporton # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2] +patreon: # Replace with a single Patreon username +open_collective: # Replace with a single Open Collective username +ko_fi: # Replace with a single Ko-fi username +tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel +community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry +liberapay: # Replace with a single Liberapay username +issuehunt: # Replace with a single IssueHunt username +lfx_crowdfunding: # Replace with a single LFX Crowdfunding project-name e.g., cloud-foundry +polar: # Replace with a single Polar username +buy_me_a_coffee: # Replace with a single Buy Me a Coffee username +thanks_dev: # Replace with a single thanks.dev username +custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2'] \ No newline at end of file diff --git a/language_test/__init__.py b/language_test/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/language_test/forms.py b/language_test/forms.py new file mode 100644 index 0000000..2d1b27c --- /dev/null +++ b/language_test/forms.py @@ -0,0 +1,5 @@ +from django import forms +from languages.forms import LanguageField + +class TestForm(forms.Form): + language = LanguageField() diff --git a/language_test/settings.py b/language_test/settings.py new file mode 100644 index 0000000..f7709b8 --- /dev/null +++ b/language_test/settings.py @@ -0,0 +1,122 @@ +""" +Django settings for language_test project. + +Generated by 'django-admin startproject' using Django 1.11.20. + +For more information on this file, see +https://docs.djangoproject.com/en/1.11/topics/settings/ + +For the full list of settings and their values, see +https://docs.djangoproject.com/en/1.11/ref/settings/ +""" + +import os + +# Build paths inside the project like this: os.path.join(BASE_DIR, ...) +BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) + + +# Quick-start development settings - unsuitable for production +# See https://docs.djangoproject.com/en/1.11/howto/deployment/checklist/ + +# SECURITY WARNING: keep the secret key used in production secret! +SECRET_KEY = 'kr09q(tihx%2+mg^z^4axqk+x&(ujtm(uv00==ljo-l+_8dx=@' + +# SECURITY WARNING: don't run with debug turned on in production! +DEBUG = True + +ALLOWED_HOSTS = [] + + +# Application definition + +INSTALLED_APPS = [ + 'django.contrib.admin', + 'django.contrib.auth', + 'django.contrib.contenttypes', + 'django.contrib.sessions', + 'django.contrib.messages', + 'django.contrib.staticfiles', + 'language_test', +] + +MIDDLEWARE = [ + 'django.middleware.security.SecurityMiddleware', + 'django.contrib.sessions.middleware.SessionMiddleware', + 'django.middleware.common.CommonMiddleware', + 'django.middleware.csrf.CsrfViewMiddleware', + 'django.contrib.auth.middleware.AuthenticationMiddleware', + 'django.contrib.messages.middleware.MessageMiddleware', + 'django.middleware.clickjacking.XFrameOptionsMiddleware', +] + +ROOT_URLCONF = 'language_test.urls' + +TEMPLATES = [ + { + 'BACKEND': 'django.template.backends.django.DjangoTemplates', + 'DIRS': [os.path.join(BASE_DIR, 'templates')] + , + 'APP_DIRS': True, + 'OPTIONS': { + 'context_processors': [ + 'django.template.context_processors.debug', + 'django.template.context_processors.request', + 'django.contrib.auth.context_processors.auth', + 'django.contrib.messages.context_processors.messages', + ], + }, + }, +] + +WSGI_APPLICATION = 'language_test.wsgi.application' + + +# Database +# https://docs.djangoproject.com/en/1.11/ref/settings/#databases + +DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.sqlite3', + 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), + } +} + + +# Password validation +# https://docs.djangoproject.com/en/1.11/ref/settings/#auth-password-validators + +AUTH_PASSWORD_VALIDATORS = [ + { + 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', + }, +] + + +# Internationalization +# https://docs.djangoproject.com/en/1.11/topics/i18n/ + +LANGUAGE_CODE = 'en-us' + +TIME_ZONE = 'UTC' + +USE_I18N = True + +USE_L10N = True + +USE_TZ = True + + +# Static files (CSS, JavaScript, Images) +# https://docs.djangoproject.com/en/1.11/howto/static-files/ + +STATIC_URL = '/static/' diff --git a/language_test/templates/language_test/test.html b/language_test/templates/language_test/test.html new file mode 100644 index 0000000..d21533d --- /dev/null +++ b/language_test/templates/language_test/test.html @@ -0,0 +1,8 @@ + +
+{{ form }}
+ + \ No newline at end of file diff --git a/language_test/urls.py b/language_test/urls.py new file mode 100644 index 0000000..f944ba0 --- /dev/null +++ b/language_test/urls.py @@ -0,0 +1,24 @@ +"""language_test URL Configuration + +The `urlpatterns` list routes URLs to views. For more information please see: + https://docs.djangoproject.com/en/1.11/topics/http/urls/ +Examples: +Function views + 1. Add an import: from my_app import views + 2. Add a URL to urlpatterns: url(r'^$', views.home, name='home') +Class-based views + 1. Add an import: from other_app.views import Home + 2. Add a URL to urlpatterns: url(r'^$', Home.as_view(), name='home') +Including another URLconf + 1. Import the include() function: from django.conf.urls import url, include + 2. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls')) +""" +from django.contrib import admin +from django.urls import re_path, path + +from language_test import views + +urlpatterns = [ + re_path(r'^admin/', admin.site.urls), + path('', views.TestView.as_view()) +] diff --git a/language_test/views.py b/language_test/views.py new file mode 100644 index 0000000..883bb21 --- /dev/null +++ b/language_test/views.py @@ -0,0 +1,9 @@ +from django.shortcuts import render +from django.views import View +from .forms import TestForm + + +class TestView(View): + def get(self, request): + form = TestForm() + return render(request, 'language_test/test.html', {'form': form}) \ No newline at end of file diff --git a/language_test/wsgi.py b/language_test/wsgi.py new file mode 100644 index 0000000..2ec0c57 --- /dev/null +++ b/language_test/wsgi.py @@ -0,0 +1,16 @@ +""" +WSGI config for language_test project. + +It exposes the WSGI callable as a module-level variable named ``application``. + +For more information on this file, see +https://docs.djangoproject.com/en/1.11/howto/deployment/wsgi/ +""" + +import os + +from django.core.wsgi import get_wsgi_application + +os.environ.setdefault("DJANGO_SETTINGS_MODULE", "language_test.settings") + +application = get_wsgi_application() diff --git a/languages/fields.py b/languages/fields.py index 871107f..f703262 100644 --- a/languages/fields.py +++ b/languages/fields.py @@ -8,6 +8,13 @@ def __init__(self, *args, **kwargs): # Local import so the languages aren't loaded unless they are needed. from .languages import LANGUAGES - kwargs.setdefault('max_length', 3) + # kwargs.setdefault('max_length', 3) + kwargs.setdefault('max_length', 255) kwargs.setdefault('choices', LANGUAGES) super(CharField, self).__init__(*args, **kwargs) + + def formfield(self, **kwargs): + from languages import forms + defaults = {'form_class': forms.LanguageField} + defaults.update(kwargs) + return super().formfield(**defaults) diff --git a/languages/forms.py b/languages/forms.py new file mode 100644 index 0000000..ab6692a --- /dev/null +++ b/languages/forms.py @@ -0,0 +1,32 @@ +from django import forms +from django.core.cache import caches + +sorted_languages = None +def _get_sorted_languages(): + from .languages import LANGUAGES + global sorted_languages + if sorted_languages: + return sorted_languages + sorted_languages = sorted(LANGUAGES, key=lambda p: p[1]) + return sorted_languages + +class _LanguageWidget(forms.Select): + def render(self, name, value, attrs=None, renderer=None): + cache = caches['default'] + result = cache.get(value or '') + if result is not None: + return result + result = super().render(name, value, attrs, renderer) + cache.set(value or '', result) + return result + +class LanguageField(forms.ChoiceField): + """ + A language field for Django forms. + """ + widget = _LanguageWidget + + def __init__(self, *args, **kwargs): + defaults = {'choices': _get_sorted_languages()} + defaults.update(kwargs) + super().__init__(*args, **defaults) diff --git a/languages/languages.py b/languages/languages.py index 7543c0d..a7c29b6 100644 --- a/languages/languages.py +++ b/languages/languages.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -from django.utils.translation import ugettext_lazy as _ +from django.utils.translation import gettext_lazy as _ LANGUAGES = ( ("aa", _(u"Afar")), diff --git a/languages/regenerate.py b/languages/regenerate.py index 59122bb..880f58e 100644 --- a/languages/regenerate.py +++ b/languages/regenerate.py @@ -11,7 +11,7 @@ import urllib2 TEMPLATE = u'''# -*- coding: utf-8 -*- -from django.utils.translation import ugettext_lazy as _ +from django.utils.translation import gettext_lazy as _ LANGUAGES = ( %(languages)s diff --git a/manage.py b/manage.py new file mode 100755 index 0000000..8ba2f6f --- /dev/null +++ b/manage.py @@ -0,0 +1,22 @@ +#!/usr/bin/env python3 +import os +import sys + +if __name__ == "__main__": + os.environ.setdefault("DJANGO_SETTINGS_MODULE", "language_test.settings") + try: + from django.core.management import execute_from_command_line + except ImportError: + # The above import may fail for some other reason. Ensure that the + # issue is really that Django is missing to avoid masking other + # exceptions on Python 2. + try: + import django + except ImportError: + raise ImportError( + "Couldn't import Django. Are you sure it's installed and " + "available on your PYTHONPATH environment variable? Did you " + "forget to activate a virtual environment?" + ) + raise + execute_from_command_line(sys.argv)