Skip to content

Commit

Permalink
Merge pull request #20 from hmeza/django-2.2.15
Browse files Browse the repository at this point in the history
Changes to work with Django 2.2.15
  • Loading branch information
noxan authored Oct 6, 2023
2 parents 7b355be + b13f87b commit 886019b
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 35 deletions.
5 changes: 3 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ VENV_FOLDER=env
PIP_BIN=$(VENV_FOLDER)/bin/pip
PYTHON_BIN=$(VENV_FOLDER)/bin/python
COVERAGE_BINARY=$(VENV_FOLDER)/bin/coverage
DJANGO_SETTINGS_MODULE='referral.tests.settings'

all: environment requirements

Expand All @@ -12,9 +13,9 @@ requirements:
$(PIP_BIN) install -r requirements.txt

test: requirements
$(PYTHON_BINARY) env/bin/django-admin.py test --settings=referral.tests.settings
$(PYTHON_BINARY) env/bin/django-admin test --settings=referral.tests.settings

coverage: requirements
$(COVERAGE_BINARY) erase
$(COVERAGE_BINARY) run --branch --source=referral env/bin/django-admin.py test --settings=referral.tests.settings
$(COVERAGE_BINARY) run --branch --source=referral env/bin/django-admin test --settings=referral.tests.settings
$(COVERAGE_BINARY) html
2 changes: 1 addition & 1 deletion referral/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '1.1.0'
__version__ = '1.2.0'
9 changes: 2 additions & 7 deletions referral/middleware.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
from . import settings
from .models import Referrer
from django.utils.deprecation import MiddlewareMixin


class ReferrerMiddleware(object):
def __init__(self, get_response):
self.get_response = get_response

def __call__(self, request):
return self.get_response(request)

class ReferrerMiddleware(MiddlewareMixin):
def process_request(self, request):
if settings.GET_PARAMETER in request.GET:
referrer = None
Expand Down
7 changes: 4 additions & 3 deletions referral/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

import django.db.models.deletion
from django.db import models, migrations
from django.conf import settings

Expand Down Expand Up @@ -34,7 +35,7 @@ class Migration(migrations.Migration):
('name', models.CharField(unique=True, max_length=255, verbose_name='Name')),
('description', models.TextField(null=True, verbose_name='Description', blank=True)),
('creation_date', models.DateTimeField(auto_now_add=True, verbose_name='Creation date')),
('campaign', models.ForeignKey(related_name='referrers', verbose_name='Campaign', blank=True, to='referral.Campaign', null=True)),
('campaign', models.ForeignKey(related_name='referrers', verbose_name='Campaign', blank=True, to='referral.Campaign', null=True, on_delete=django.db.models.deletion.PROTECT)),
],
options={
'ordering': ['name'],
Expand All @@ -47,8 +48,8 @@ class Migration(migrations.Migration):
name='UserReferrer',
fields=[
('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
('referrer', models.ForeignKey(related_name='users', verbose_name='Referrer', to='referral.Referrer')),
('user', models.OneToOneField(related_name='user_referrer', verbose_name='User', to=settings.AUTH_USER_MODEL)),
('referrer', models.ForeignKey(related_name='users', verbose_name='Referrer', to='referral.Referrer', on_delete=django.db.models.deletion.PROTECT)),
('user', models.OneToOneField(related_name='user_referrer', verbose_name='User', to=settings.AUTH_USER_MODEL, on_delete=django.db.models.deletion.PROTECT)),
],
options={
'ordering': ['referrer__name'],
Expand Down
18 changes: 18 additions & 0 deletions referral/migrations/0002_auto_20230623_2008.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 2.2.15 on 2023-06-23 20:08

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('referral', '0001_initial'),
]

operations = [
migrations.AlterField(
model_name='campaign',
name='pattern',
field=models.CharField(blank=True, help_text='All auto created referrers containing this pattern will be associated with this campaign', max_length=255, verbose_name='Referrer pattern'),
),
]
8 changes: 4 additions & 4 deletions referral/models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from django.db import models
from django.utils.translation import ugettext_lazy as _
from django.utils.translation import gettext_lazy as _

from .compat import User
from . import settings
Expand Down Expand Up @@ -34,7 +34,7 @@ class Referrer(models.Model):
name = models.CharField(_("Name"), max_length=255, unique=True)
description = models.TextField(_("Description"), blank=True, null=True)
creation_date = models.DateTimeField(_("Creation date"), auto_now_add=True)
campaign = models.ForeignKey(Campaign, verbose_name=_("Campaign"), related_name='referrers', blank=True, null=True)
campaign = models.ForeignKey(Campaign, verbose_name=_("Campaign"), related_name='referrers', blank=True, null=True, on_delete=models.PROTECT)

class Meta:
ordering = ['name']
Expand Down Expand Up @@ -71,8 +71,8 @@ def apply_referrer(self, user, request):


class UserReferrer(models.Model):
user = models.OneToOneField(User, verbose_name=_("User"), related_name='user_referrer')
referrer = models.ForeignKey(Referrer, verbose_name=_("Referrer"), related_name='users')
user = models.OneToOneField(User, verbose_name=_("User"), related_name='user_referrer', on_delete=models.PROTECT)
referrer = models.ForeignKey(Referrer, verbose_name=_("Referrer"), related_name='users', on_delete=models.PROTECT)

objects = UserReferrerManager()

Expand Down
5 changes: 1 addition & 4 deletions referral/tests/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,28 @@
from referral.compat import User
from referral.models import Campaign, Referrer, UserReferrer


class UserFactory(factory.django.DjangoModelFactory):
class Meta:
model = User


class CampaignFactory(factory.django.DjangoModelFactory):
class Meta:
model = Campaign

name = factory.Sequence(lambda n: "Test Campaign %s" % n)
description = "Some long test description"


class ReferrerFactory(factory.django.DjangoModelFactory):
class Meta:
model = Referrer

name = factory.Sequence(lambda n: "Test Referrer %s" % n)
description = "Some long test description"


class UserReferrerFactory(factory.django.DjangoModelFactory):
class Meta:
model = UserReferrer

user = factory.LazyAttribute(lambda a: UserFactory())
referrer = factory.LazyAttribute(lambda a: ReferrerFactory())

21 changes: 14 additions & 7 deletions referral/tests/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,20 @@
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

STATIC_ROOT = os.path.join(BASE_DIR, 'tests', 'static')

STATIC_URL = '/static/'

STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'tests', 'additional_static'),
("prefix", os.path.join(BASE_DIR, 'tests', 'additional_static')),
MIDDLEWARE = MIDDLEWARE_CLASSES

TEMPLATES = [
{
"BACKEND": "django.template.backends.django.DjangoTemplates",
"DIRS": [],
"APP_DIRS": True,
"OPTIONS": {
'context_processors': [
'django.contrib.messages.context_processors.messages',
"django.contrib.auth.context_processors.auth",
]
},
},
]

# Cache and database
Expand Down
11 changes: 4 additions & 7 deletions referral/tests/test_middlewares.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
from django.test import TestCase, RequestFactory
from django.test import TestCase
from django.http import HttpRequest

from referral.middleware import ReferrerMiddleware
from referral.models import Referrer
from referral import settings


class ReferrerMiddlewareTest(TestCase):
rf = RequestFactory()

def setUp(self):
self.request = self.rf.get('/custom_url')
# convert request.GET to mutable QueryDict instance
self.request.GET = self.request.GET.copy()
self.ref_middleware = ReferrerMiddleware(self.request)
self.request = HttpRequest()
self.request.session = {}
self.ref_middleware = ReferrerMiddleware(self.request)

def test_process_request_no_ref(self):
self.ref_middleware.process_request(self.request)
Expand Down

0 comments on commit 886019b

Please sign in to comment.