From 5efea835d0896f62fbaa700ace51970096080b07 Mon Sep 17 00:00:00 2001 From: Nikolai Kristiansen Date: Mon, 8 Feb 2016 23:07:01 +0100 Subject: [PATCH] python3 compat and deps++ --- dpdapi/admin.py | 2 +- dpdapi/management/commands/import_valiases.py | 2 +- dpdapi/models.py | 10 +++-- dpdapi/urls.py | 2 +- dpdapi/views.py | 3 ++ dpdapisite/settings.py | 27 +++++++++-- dpdapisite/urls.py | 17 ++++--- fabfile.py | 45 ++----------------- requirements.txt | 14 +++--- 9 files changed, 58 insertions(+), 64 deletions(-) diff --git a/dpdapi/admin.py b/dpdapi/admin.py index aea6143..fe98ee3 100644 --- a/dpdapi/admin.py +++ b/dpdapi/admin.py @@ -17,4 +17,4 @@ class DomainAdmin(admin.ModelAdmin): @admin.register(User) class UserAdmin(admin.ModelAdmin): - pass \ No newline at end of file + pass diff --git a/dpdapi/management/commands/import_valiases.py b/dpdapi/management/commands/import_valiases.py index fba29cf..f5a3630 100644 --- a/dpdapi/management/commands/import_valiases.py +++ b/dpdapi/management/commands/import_valiases.py @@ -57,4 +57,4 @@ class Command(BaseCommand): self.stdout.write('Imported {}, skipped {} existing, total {}'.format( counts['imported'], counts['existing'], - len(new_aliases))) \ No newline at end of file + len(new_aliases))) diff --git a/dpdapi/models.py b/dpdapi/models.py index c58f369..e0e44ef 100644 --- a/dpdapi/models.py +++ b/dpdapi/models.py @@ -1,5 +1,6 @@ from __future__ import unicode_literals from django.db import models +from django.utils.encoding import python_2_unicode_compatible class BaseModel(models.Model): @@ -10,13 +11,14 @@ class BaseModel(models.Model): abstract = True +@python_2_unicode_compatible class Alias(BaseModel): source = models.EmailField(max_length=254) destination = models.EmailField(max_length=254) domain = models.ForeignKey('dpdapi.Domain') active = models.BooleanField(default=True) - def __unicode__(self): + def __str__(self): return "{}={}".format(self.source, self.destination) def save(self, *args, **kwargs): @@ -30,10 +32,11 @@ class Alias(BaseModel): unique_together = ('source', 'destination', 'domain') +@python_2_unicode_compatible class Domain(BaseModel): name = models.CharField(max_length=254, unique=True) - def __unicode__(self): + def __str__(self): return self.name def save(self, *args, **kwargs): @@ -42,12 +45,13 @@ class Domain(BaseModel): super(Domain, self).save(*args, **kwargs) +@python_2_unicode_compatible class User(BaseModel): email = models.EmailField(max_length=254, unique=True) password = models.CharField(max_length=109) domain = models.ForeignKey('dpdapi.Domain') - def __unicode__(self): + def __str__(self): return self.email def save(self, *args, **kwargs): diff --git a/dpdapi/urls.py b/dpdapi/urls.py index f70d09e..4da37cb 100644 --- a/dpdapi/urls.py +++ b/dpdapi/urls.py @@ -7,4 +7,4 @@ router = DefaultRouter() router.register(r'aliases', AliasViewSet) router.register(r'domains', DomainViewSet) router.register(r'users', UserViewSet) -urlpatterns = router.urls \ No newline at end of file +urlpatterns = router.urls diff --git a/dpdapi/views.py b/dpdapi/views.py index 55fb926..ec6f3ea 100644 --- a/dpdapi/views.py +++ b/dpdapi/views.py @@ -1,6 +1,7 @@ from __future__ import unicode_literals import logging import operator +from django.utils import six from django.db.models import Q from rest_framework.decorators import list_route from rest_framework.permissions import IsAuthenticated @@ -11,6 +12,8 @@ from dpdapi.filters import AliasRegexFilterBackend from dpdapi.models import Alias, Domain, User from dpdapi.serializers import AliasSerializer, AliasDeleteSerializer, DomainSerializer, UserSerializer +if six.PY3: + from functools import reduce logger = logging.getLogger(__name__) diff --git a/dpdapisite/settings.py b/dpdapisite/settings.py index 925545b..1866b80 100644 --- a/dpdapisite/settings.py +++ b/dpdapisite/settings.py @@ -20,7 +20,28 @@ SECRET_KEY = 'hlaf#yma(8s!cy-bf5e6kc&_5n_l1h!34c(j32-b3v8o7pg$5n' # SECURITY WARNING: don't run with debug turned on in production! DEBUG = True -TEMPLATE_DEBUG = True +TEMPLATES = [ + { + 'BACKEND': 'django.template.backends.django.DjangoTemplates', + 'DIRS': [ + # insert your TEMPLATE_DIRS here + ], + 'APP_DIRS': True, + 'OPTIONS': { + 'context_processors': [ + # Insert your TEMPLATE_CONTEXT_PROCESSORS here or use this + # list if you haven't customized them: + 'django.contrib.auth.context_processors.auth', + 'django.template.context_processors.debug', + 'django.template.context_processors.i18n', + 'django.template.context_processors.media', + 'django.template.context_processors.static', + 'django.template.context_processors.tz', + 'django.contrib.messages.context_processors.messages', + ], + }, + }, +] ALLOWED_HOSTS = [] @@ -93,6 +114,6 @@ REST_FRAMEWORK = { } try: - from local_settings import * + from .local_settings import * except ImportError: - pass \ No newline at end of file + pass diff --git a/dpdapisite/urls.py b/dpdapisite/urls.py index f4165e8..3d35aee 100644 --- a/dpdapisite/urls.py +++ b/dpdapisite/urls.py @@ -1,11 +1,14 @@ -from django.conf.urls import patterns, include, url +from django.conf.urls import include, url from django.contrib import admin from django.views.generic import RedirectView +from rest_framework import urls as rest_framework_urls -urlpatterns = patterns( - '', +from dpdapi import urls as dpdapi_urls + + +urlpatterns = [ url(r'^$', RedirectView.as_view(pattern_name='api-root')), - url(r'^api/', include('dpdapi.urls')), - url(r'^admin/', include(admin.site.urls)), - url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')) -) + url(r'^api/', include(dpdapi_urls)), + url(r'^admin/', admin.site.urls), + url(r'^api-auth/', include(rest_framework_urls, namespace='rest_framework')) +] diff --git a/fabfile.py b/fabfile.py index d77de0c..aaad216 100644 --- a/fabfile.py +++ b/fabfile.py @@ -1,9 +1,5 @@ -import logging -import sys - from contextlib import contextmanager as _contextmanager -from fabric.api import run, sudo, env, cd, prefix, lcd -from fabric.api import local as lrun +from fabric.api import run, sudo, env, cd, prefix env.use_ssh_config = True env.hosts = ['lynx.neuf.no'] @@ -14,50 +10,17 @@ env.activate = 'source {}/venv/bin/activate'.format(env.project_path) @_contextmanager def virtualenv(): - with cd(env.project_path): - with prefix(env.activate): - yield - - -def __git_validate_clean(): - with lcd(env.project_path): - if lrun('git diff-files', capture=True) != '': - logging.error("Your git-tree isn't clean! Commit your changes before deploying.") - sys.exit(1) - - -def test(): - lrun("python manage.py test dpdapi") - - -def commit(): - lrun("git add -p && git commit") - - -def push(): - lrun("git push") - - -def prepare_deploy(): - test() - commit() - push() - - -def gitstatus(): - # __git_validate_clean() - with virtualenv(): - # Get source - run('git branch -v') + with cd(env.project_path), prefix(env.activate): + yield def deploy(): - # __git_validate_clean() with virtualenv(): run('git pull') # Get source run('pip install -r requirements.txt') # install deps in virtualenv run('umask 022; python manage.py collectstatic --noinput') # Collect static run('python manage.py migrate') # Run DB migrations + # TODO: Supervisor # Reload gunicorn sudo('/etc/init.d/gunicorn reload', shell=False) diff --git a/requirements.txt b/requirements.txt index 123793d..38a191d 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,7 +1,7 @@ -Django>=1.7,<1.8 -django-extensions==1.4.9 -django-filter==0.9.1 -djangorestframework>=3.0,<3.1 -passlib==1.6.2 -mysqlclient==1.3.4 -Fabric==1.10.1 +Django>=1.9,<1.10 +djangorestframework>=3.3,<3.4 +django-extensions==1.6.1 +django-filter==0.12.0 +passlib==1.6.5 +mysqlclient==1.3.7 +Fabric==1.10.2