Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -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']
Empty file added language_test/__init__.py
Empty file.
5 changes: 5 additions & 0 deletions language_test/forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django import forms
from languages.forms import LanguageField

class TestForm(forms.Form):
language = LanguageField()
122 changes: 122 additions & 0 deletions language_test/settings.py
Original file line number Diff line number Diff line change
@@ -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/'
8 changes: 8 additions & 0 deletions language_test/templates/language_test/test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<html>
<head>
<title>Test</title>
</head>
<body>
<p>{{ form }}</p>
</body>
</html>
24 changes: 24 additions & 0 deletions language_test/urls.py
Original file line number Diff line number Diff line change
@@ -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())
]
9 changes: 9 additions & 0 deletions language_test/views.py
Original file line number Diff line number Diff line change
@@ -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})
16 changes: 16 additions & 0 deletions language_test/wsgi.py
Original file line number Diff line number Diff line change
@@ -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()
9 changes: 8 additions & 1 deletion languages/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
32 changes: 32 additions & 0 deletions languages/forms.py
Original file line number Diff line number Diff line change
@@ -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)
2 changes: 1 addition & 1 deletion languages/languages.py
Original file line number Diff line number Diff line change
@@ -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")),
Expand Down
2 changes: 1 addition & 1 deletion languages/regenerate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 22 additions & 0 deletions manage.py
Original file line number Diff line number Diff line change
@@ -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)