Skip to content

Commit

Permalink
adding git actions tests
Browse files Browse the repository at this point in the history
added ls

testing dir change

added linting tools

added dev requirement file

updated install

testing tests

added cp of .env file

checking enviroment files

testing cat

fixed cat command

added django-simple-history to project

Add the following models to simple history
- Project
- Institution
- Language
- Subject
- DocumentFile
- Users

Updated tests

Added new Migrations

Cleaned up admin code while updating

added basic logging and updated environment variables

ordering

updated
  • Loading branch information
daniel-gray-tangent committed Apr 30, 2024
1 parent e3b78f0 commit a61a559
Show file tree
Hide file tree
Showing 19 changed files with 402 additions and 26 deletions.
11 changes: 11 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
SECRET_KEY='django-insecure-w!h85bp^$$e8gm%c23r!0%9i7yzd=6w$$s&ic+6!%306&kj8@k*5'
DEBUG=True
DB_HOST=db
DB_PORT=5432
DB_NAME=term_db
DB_USER=sadilar
DB_PASSWORD=sadilar
LOGGING_FILE=logs/debug.log
LOGGING_HANDLERS_LEVEL=INFO
LOGGING_LOGGERS_LEVEL=INFO
LOGGING_LOGGERS_DJANGO_LEVEL=INFO
11 changes: 11 additions & 0 deletions .env.testing
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
SECRET_KEY='django-insecure-w!h85bp^$$e8gm%c23r!0%9i7yzd=6w$$s&ic+6!%306&kj8@k*5'
DEBUG=True
DB_HOST=db
DB_PORT=5432
DB_NAME=term_db
DB_USER=sadilar
DB_PASSWORD=sadilar
LOGGING_FILE=logs/debug.log
LOGGING_HANDLERS_LEVEL=INFO
LOGGING_LOGGERS_LEVEL=INFO
LOGGING_LOGGERS_DJANGO_LEVEL=INFO
22 changes: 22 additions & 0 deletions .github/workflows/testing.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: Testing Django
on: [ pull_request, push ] # activates the workflow when there is a push or pull request in the repo
jobs:
test_project:
runs-on: ubuntu-latest # operating system your code will run on
steps:
- uses: actions/checkout@v2
- uses: actions/setup-python@v2
- name: Install Dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements-test.txt
- name: Run linting tools
run: |
cd app/
ruff format .
- name: Run Tests
run: |
cp .env.testing app/.env
cd app/
mkdir static_files
python manage.py test
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,4 @@ venv.bak/
app/static_files/
/app/documents/
app/media/
/app/logging/
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,16 @@ About the project:
3. Run `make run` to run the docker container
4. Run `make stop` to stop the docker container

## Production


### Plugins installed
#### Django Simple History

https://django-simple-history.readthedocs.io/en/latest/

#### Basic setup for production

### environment variables

please use .env.example as example
60 changes: 60 additions & 0 deletions app/app/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,17 @@
"""

import os
import sys
from pathlib import Path

import environ

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

# Take environment variables from .env file
environ.Env.read_env(os.path.join(BASE_DIR, ".env"))

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

Expand All @@ -40,6 +46,7 @@
"django.contrib.staticfiles",
"users",
"general",
"simple_history",
]
if DEBUG:
INSTALLED_APPS += [
Expand All @@ -57,6 +64,7 @@
"django.contrib.auth.middleware.AuthenticationMiddleware",
"django.contrib.messages.middleware.MessageMiddleware",
"django.middleware.clickjacking.XFrameOptionsMiddleware",
"simple_history.middleware.HistoryRequestMiddleware",
]

ROOT_URLCONF = "app.urls"
Expand Down Expand Up @@ -96,6 +104,9 @@
}
}

if "test" in sys.argv or "test_coverage" in sys.argv: # Covers regular testing and django-coverage
DATABASES["default"]["ENGINE"] = "django.db.backends.sqlite3"

# Password validation
# https://docs.djangoproject.com/en/5.0/ref/settings/#auth-password-validators

Expand Down Expand Up @@ -152,3 +163,52 @@
# https://docs.djangoproject.com/en/5.0/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"


if DEBUG:
LOGGING = {
"version": 1,
"disable_existing_loggers": False,
"handlers": {
"console": {
"class": "logging.StreamHandler",
},
},
"root": {
"handlers": ["console"],
"level": "DEBUG",
},
}
else:
LOGGING = {
"version": 1,
"disable_existing_loggers": False,
"handlers": {
"console": {
"class": "logging.StreamHandler",
},
"file": {
"level": os.environ.get("LOGGING_HANDLERS_LEVEL", "WARNING"),
"class": "logging.FileHandler",
"filename": os.environ.get("LOGGING_FILE", "logging/debug.log"),
"formatter": "verbose",
},
},
"root": {
"handlers": ["console", "file"],
"level": os.environ.get("LOGGING_LOGGERS_LEVEL", "WARNING"),
},
"loggers": {
"django": {
"handlers": ["file"],
"level": os.environ.get("LOGGING_LOGGERS_DJANGO_LEVEL", "WARNING"),
"propagate": True,
},
},
"formatters": {
"verbose": {
"format": "{asctime} {levelname} - {name} {module}.py (line: {lineno:d}). - {message}",
"style": "{",
},
},
}
49 changes: 34 additions & 15 deletions app/general/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,11 @@

from django.contrib import admin
from django.forms import HiddenInput, ModelForm, fields_for_model
from simple_history.admin import SimpleHistoryAdmin

from .models import DocumentFile, Institution, Language, Project, Subject


class ProjectAdminInline(admin.TabularInline):
model = Project
extra = 0


class DocumentFileForm(ModelForm):
class Meta:
model = DocumentFile
Expand Down Expand Up @@ -47,22 +43,45 @@ def clean(self):
return cleaned_data


class DocumentFileAdmin(admin.ModelAdmin):
class DocumentFileAdmin(SimpleHistoryAdmin):
list_display = ["title", "license", "document_type", "available"]
ordering = [
"license",
]
ordering = ["license"]
search_fields = ["title", "license", "document_type"]

form = DocumentFileForm
history_list_display = ["title", "license", "document_type", "available"]


class SubjectAdmin(SimpleHistoryAdmin):
search_fields = ["name"]
list_display = ["name"]
history_list_display = ["name"]


class LanguageAdmin(SimpleHistoryAdmin):
history_list_display = ["name", "iso_code"]
list_display = ["name", "iso_code"]


class ProjectAdminInline(admin.TabularInline):
model = Project
extra = 0


class ProjectAdmin(SimpleHistoryAdmin):
search_fields = ["name"]
list_display = ["name"]
history_list_display = ["name"]


class ProjectAdmin(admin.ModelAdmin):
class InstitutionAdmin(SimpleHistoryAdmin):
search_fields = ["name"]
list_display = ["name"]
inlines = [ProjectAdminInline]
history_list_display = ["name", "abbreviation"]


admin.site.register(Project)
admin.site.register(Institution, ProjectAdmin)
admin.site.register(Language)
admin.site.register(Subject)
admin.site.register(Project, ProjectAdmin)
admin.site.register(Institution, InstitutionAdmin)
admin.site.register(Language, LanguageAdmin)
admin.site.register(Subject, SubjectAdmin)
admin.site.register(DocumentFile, DocumentFileAdmin)
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
import django.core.validators
import django.db.models.deletion
import simple_history.models
from django.conf import settings
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('general', '0003_rename_institution_documentfile_institution_and_more'),
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]

operations = [
migrations.CreateModel(
name='HistoricalDocumentFile',
fields=[
('id', models.BigIntegerField(auto_created=True, blank=True, db_index=True, verbose_name='ID')),
('title', models.CharField(max_length=200)),
('url', models.URLField(blank=True, verbose_name='URL')),
('uploaded_file', models.TextField(blank=True, help_text='PDF files up to 10MB are allowed.', max_length=100, validators=[django.core.validators.FileExtensionValidator(['pdf'])])),
('available', models.BooleanField(default=True)),
('license', models.CharField(choices=[('(c)', 'All rights reserved'), ('CC0', 'No rights reserved'), ('CC BY', 'Creative Commons Attribution'), ('CC BY-SA', 'Creative Commons Attribution-ShareAlike'), ('CC BY-NC', 'Creative Commons Attribution-NonCommercial'), ('CC BY-NC-SA', 'Creative Commons Attribution-NonCommercial-ShareAlike')], default='(c)', help_text='\n <a\n href="https://creativecommons.org/share-your-work/cclicenses/"\n rel="noreferrer"\n target="_blank"\n >\n More information about Creative Commons licenses.\n </a>\'\n ', max_length=200)),
('mime_type', models.CharField(blank=True, help_text='This input will auto-populate.', max_length=200)),
('document_type', models.CharField(choices=[('Glossary', 'Glossary'), ('Policy', 'Policy')], max_length=200)),
('history_id', models.AutoField(primary_key=True, serialize=False)),
('history_date', models.DateTimeField(db_index=True)),
('history_change_reason', models.CharField(max_length=100, null=True)),
('history_type', models.CharField(choices=[('+', 'Created'), ('~', 'Changed'), ('-', 'Deleted')], max_length=1)),
('history_user', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='+', to=settings.AUTH_USER_MODEL)),
('institution', models.ForeignKey(blank=True, db_constraint=False, null=True, on_delete=django.db.models.deletion.DO_NOTHING, related_name='+', to='general.institution')),
],
options={
'verbose_name': 'historical document file',
'verbose_name_plural': 'historical document files',
'ordering': ('-history_date', '-history_id'),
'get_latest_by': ('history_date', 'history_id'),
},
bases=(simple_history.models.HistoricalChanges, models.Model),
),
migrations.CreateModel(
name='HistoricalInstitution',
fields=[
('id', models.BigIntegerField(auto_created=True, blank=True, db_index=True, verbose_name='ID')),
('name', models.CharField(db_index=True, max_length=200)),
('abbreviation', models.CharField(max_length=200)),
('url', models.URLField(blank=True, verbose_name='URL')),
('email', models.EmailField(blank=True, max_length=200)),
('logo', models.TextField(blank=True, max_length=100)),
('history_id', models.AutoField(primary_key=True, serialize=False)),
('history_date', models.DateTimeField(db_index=True)),
('history_change_reason', models.CharField(max_length=100, null=True)),
('history_type', models.CharField(choices=[('+', 'Created'), ('~', 'Changed'), ('-', 'Deleted')], max_length=1)),
('history_user', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='+', to=settings.AUTH_USER_MODEL)),
],
options={
'verbose_name': 'historical institution',
'verbose_name_plural': 'historical institutions',
'ordering': ('-history_date', '-history_id'),
'get_latest_by': ('history_date', 'history_id'),
},
bases=(simple_history.models.HistoricalChanges, models.Model),
),
migrations.CreateModel(
name='HistoricalLanguage',
fields=[
('id', models.BigIntegerField(auto_created=True, blank=True, db_index=True, verbose_name='ID')),
('name', models.CharField(db_index=True, max_length=150)),
('iso_code', models.CharField(db_index=True, help_text='The 2 or 3 letter code from ISO 639.', max_length=50, verbose_name='ISO code')),
('history_id', models.AutoField(primary_key=True, serialize=False)),
('history_date', models.DateTimeField(db_index=True)),
('history_change_reason', models.CharField(max_length=100, null=True)),
('history_type', models.CharField(choices=[('+', 'Created'), ('~', 'Changed'), ('-', 'Deleted')], max_length=1)),
('history_user', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='+', to=settings.AUTH_USER_MODEL)),
],
options={
'verbose_name': 'historical language',
'verbose_name_plural': 'historical languages',
'ordering': ('-history_date', '-history_id'),
'get_latest_by': ('history_date', 'history_id'),
},
bases=(simple_history.models.HistoricalChanges, models.Model),
),
migrations.CreateModel(
name='HistoricalProject',
fields=[
('id', models.BigIntegerField(auto_created=True, blank=True, db_index=True, verbose_name='ID')),
('name', models.CharField(max_length=200)),
('url', models.URLField(blank=True, verbose_name='URL')),
('logo', models.TextField(blank=True, max_length=100)),
('start_date', models.DateField(blank=True, null=True)),
('end_date', models.DateField(blank=True, null=True)),
('history_id', models.AutoField(primary_key=True, serialize=False)),
('history_date', models.DateTimeField(db_index=True)),
('history_change_reason', models.CharField(max_length=100, null=True)),
('history_type', models.CharField(choices=[('+', 'Created'), ('~', 'Changed'), ('-', 'Deleted')], max_length=1)),
('history_user', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='+', to=settings.AUTH_USER_MODEL)),
('institution', models.ForeignKey(blank=True, db_constraint=False, null=True, on_delete=django.db.models.deletion.DO_NOTHING, related_name='+', to='general.institution', verbose_name='institution')),
],
options={
'verbose_name': 'historical project',
'verbose_name_plural': 'historical projects',
'ordering': ('-history_date', '-history_id'),
'get_latest_by': ('history_date', 'history_id'),
},
bases=(simple_history.models.HistoricalChanges, models.Model),
),
migrations.CreateModel(
name='HistoricalSubject',
fields=[
('id', models.BigIntegerField(auto_created=True, blank=True, db_index=True, verbose_name='ID')),
('name', models.CharField(db_index=True, max_length=150)),
('history_id', models.AutoField(primary_key=True, serialize=False)),
('history_date', models.DateTimeField(db_index=True)),
('history_change_reason', models.CharField(max_length=100, null=True)),
('history_type', models.CharField(choices=[('+', 'Created'), ('~', 'Changed'), ('-', 'Deleted')], max_length=1)),
('history_user', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='+', to=settings.AUTH_USER_MODEL)),
],
options={
'verbose_name': 'historical subject',
'verbose_name_plural': 'historical subjects',
'ordering': ('-history_date', '-history_id'),
'get_latest_by': ('history_date', 'history_id'),
},
bases=(simple_history.models.HistoricalChanges, models.Model),
),
]
Loading

0 comments on commit a61a559

Please sign in to comment.