Skip to content

Commit

Permalink
Fixing Middleware and added test for newer Django
Browse files Browse the repository at this point in the history
  • Loading branch information
mkalioby committed Jan 5, 2023
1 parent ef2353d commit 33c304c
Show file tree
Hide file tree
Showing 14 changed files with 285 additions and 3 deletions.
9 changes: 9 additions & 0 deletions ModelTracker/middleware.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
from ModelTracker.Tracker import ModelTracker


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

def __call__(self, request):
ModelTracker.thread.request = request
return self.get_response(request)

def process_request(self, request):
ModelTracker.thread.request = request

Expand Down
23 changes: 23 additions & 0 deletions ModelTracker/migrations/0004_auto_20230105_1841.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generated by Django 3.2 on 2023-01-05 18:41

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('ModelTracker', '0003_history_related_objects'),
]

operations = [
migrations.AlterField(
model_name='history',
name='new_state',
field=models.JSONField(default=dict),
),
migrations.AlterField(
model_name='history',
name='old_state',
field=models.JSONField(default=dict),
),
]
9 changes: 9 additions & 0 deletions TestApp/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
try:
from django.urls import re_path
except ModuleNotFoundError:
from django.conf.urls import re_path
from . import views

urlpatterns = [
re_path('^$',views.test,name="test"),
]
12 changes: 11 additions & 1 deletion TestApp/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
from django.shortcuts import render
from django.contrib.auth.decorators import login_required
from TestApp.models import employee
from django.http import HttpResponse

# Create your views here.
@login_required
def test(request):
e=employee()
e.name="Ahmed2"
e.address="Giza"
e.age=29
e.save()
return HttpResponse("Shall be saved by " + request.user.username)
Empty file added TestApp3/TestApp3/__init__.py
Empty file.
16 changes: 16 additions & 0 deletions TestApp3/TestApp3/asgi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""
ASGI config for TestApp3 project.
It exposes the ASGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/3.2/howto/deployment/asgi/
"""

import os

from django.core.asgi import get_asgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'TestApp3.settings')

application = get_asgi_application()
26 changes: 26 additions & 0 deletions TestApp3/TestApp3/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Generated by Django 3.2 on 2023-01-05 18:41

from django.db import migrations, models


class Migration(migrations.Migration):

initial = True

dependencies = [
]

operations = [
migrations.CreateModel(
name='employee',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=255)),
('address', models.CharField(max_length=255)),
('age', models.IntegerField()),
],
options={
'abstract': False,
},
),
]
Empty file.
9 changes: 9 additions & 0 deletions TestApp3/TestApp3/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from django.db import models

from ModelTracker import Tracker

class employee(Tracker.ModelTracker):
name=models.CharField(max_length=255)
address=models.CharField(max_length=255)
age=models.IntegerField()

128 changes: 128 additions & 0 deletions TestApp3/TestApp3/settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
"""
Django settings for TestApp3 project.
Generated by 'django-admin startproject' using Django 3.2.
For more information on this file, see
https://docs.djangoproject.com/en/3.2/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.2/ref/settings/
"""

from pathlib import Path

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.2/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'django-insecure-8y69cus367#@pr+mfw9l1!q27f(7x%%o+n%@4_eda3g)(76vr)'

# 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',
'TestApp3',
'ModelTracker'
]

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',
'ModelTracker.middleware.ModelTrackerMiddleware'
]

ROOT_URLCONF = 'TestApp3.urls'

TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'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 = 'TestApp3.wsgi.application'


# Database
# https://docs.djangoproject.com/en/3.2/ref/settings/#databases

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}


# Password validation
# https://docs.djangoproject.com/en/3.2/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/3.2/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/3.2/howto/static-files/

STATIC_URL = '/static/'

# Default primary key field type
# https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
22 changes: 22 additions & 0 deletions TestApp3/TestApp3/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
"""TestApp3 URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/3.2/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path
from . import views
urlpatterns = [
path('admin/', admin.site.urls),
path('test/', views.test, name="test"),
]
13 changes: 13 additions & 0 deletions TestApp3/TestApp3/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from django.shortcuts import render
from django.contrib.auth.decorators import login_required
from .models import employee
from django.http import HttpResponse

@login_required
def test(request):
e=employee()
e.name="Ahmed2"
e.address="Giza"
e.age=29
e.save()
return HttpResponse("Shall be saved by " + request.user.username)
16 changes: 16 additions & 0 deletions TestApp3/TestApp3/wsgi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""
WSGI config for TestApp3 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/3.2/howto/deployment/wsgi/
"""

import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'TestApp3.settings')

application = get_wsgi_application()
5 changes: 3 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

setup(
name='django-model-tracker',
version='2.0.3',
version='2.1.0b1',
description='Track Django Model Objects over time',
author='Mohamed El-Kalioby',
author_email = '[email protected]',
Expand All @@ -36,7 +36,8 @@
zip_safe=False, # because we're including static files
python_requires=">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*",
classifiers=[
"Development Status :: 5 - Production/Stable",
#"Development Status :: 5 - Production/Stable",
"Development Status :: 4 - Beta",
"Environment :: Web Environment",
"Framework :: Django",
"Framework :: Django :: 1.7",
Expand Down

0 comments on commit 33c304c

Please sign in to comment.