Skip to content

Commit

Permalink
Refactor project structure
Browse files Browse the repository at this point in the history
  • Loading branch information
pi-sigma committed Mar 24, 2024
1 parent 6edcdc1 commit b56a887
Show file tree
Hide file tree
Showing 69 changed files with 106 additions and 262 deletions.
3 changes: 3 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
static/* linguist-vendored
tests/* linguist-vendored
*.html linguist-detectable=false
9 changes: 5 additions & 4 deletions .github/workflows/django.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ jobs:
pip install -r requirements/ci.txt
- name: Run Bandit
run: |
bandit -r articles/ -x tests
bandit -r src/articles/ -x tests
bandit -r src/scraper/ -x tests
#
# Tests
#
Expand Down Expand Up @@ -58,9 +59,9 @@ jobs:
DJANGO_ENV: BASE
SECURE_SSL_REDIRECT: False
run: |
pytest articles/tests/unit/
pytest articles/tests/integration/
pytest scraper/tests/
pytest src/articles/tests/unit/
pytest src/articles/tests/integration/
pytest src/scraper/tests/
#
# Migrations
Expand Down
137 changes: 0 additions & 137 deletions articles/static/css/main.css

This file was deleted.

File renamed without changes.
4 changes: 2 additions & 2 deletions nous_aggregator/asgi.py → config/asgi.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""
ASGI config for nous_aggregator project.
ASGI config for nous-aggregator project.
It exposes the ASGI callable as a module-level variable named ``application``.
Expand All @@ -12,6 +12,6 @@
from django.core.asgi import get_asgi_application
from django.core.handlers.asgi import ASGIHandler

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'nous_aggregator.settings')
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'config.settings')

application: ASGIHandler = get_asgi_application()
4 changes: 2 additions & 2 deletions nous_aggregator/celery.py → config/celery.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

from celery import Celery

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "nous_aggregator.settings.local")
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings.local")

app = Celery("nous_aggregator")
app = Celery("config")

app.config_from_object("django.conf:settings", namespace="CELERY")

Expand Down
21 changes: 21 additions & 0 deletions config/scraper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
tasks = {
"magazines": {
"en": {
"schedule": 120,
"titles": [
"Al Jazeera",
"Associated Press",
"Christian Science Monitor",
"Consortium News",
"Current Affairs",
"New York Times",
"NPR",
"Reuters",
"The Atlantic",
"The Intercept",
"UPI",
"Wall Street Journal",
]
},
},
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@
DJANGO_ENV = config('DJANGO_ENV', default="")

match DJANGO_ENV:
case "":
from .base import *
case "BASE":
from .base import *
case "LOCAL":
from .local import *
case "":
case "PRODUCTION":
from .production import *
24 changes: 13 additions & 11 deletions nous_aggregator/settings/base.py → config/settings/base.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import os
import sys
from pathlib import Path
from typing import Any, Dict, List, Union
from typing import Any, Dict

from decouple import Csv, config

from scraper import tasks as scraper_tasks
from .. import scraper

# Build paths inside the project like this: BASE_DIR / 'subdir'.
# (modified because settings files are nested one level deeper)
BASE_DIR: Path = Path(__file__).resolve().parent.parent.parent
BASE_DIR = Path(__file__).resolve().parent.parent.parent

# Add "src" to Python path
PROJECT_DIR = os.path.join(BASE_DIR, "src")
sys.path.insert(0, PROJECT_DIR)

SECRET_KEY = config("SECRET_KEY", default="")

Expand Down Expand Up @@ -62,7 +65,7 @@
"django.middleware.clickjacking.XFrameOptionsMiddleware",
]

ROOT_URLCONF = "nous_aggregator.urls"
ROOT_URLCONF = "config.urls"

INTERNAL_IPS = [
"127.0.0.1",
Expand All @@ -84,7 +87,7 @@
},
]

WSGI_APPLICATION = "nous_aggregator.wsgi.application"
WSGI_APPLICATION = "config.wsgi.application"


# Logging
Expand Down Expand Up @@ -169,9 +172,8 @@

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/4.0/howto/static-files/

STATIC_URL = "static/"
STATIC_ROOT: str = os.path.join(BASE_DIR, 'static')
STATIC_URL: str = "static/"
STATIC_ROOT: str = f"{PROJECT_DIR}/staticfiles"


# Default primary key field type
Expand All @@ -188,7 +190,7 @@
CELERY_BEAT_SCHEDULE = {
"get_articles_en": {
"task": "articles.tasks.get_articles",
"schedule": scraper_tasks.magazines["en"]["schedule"],
"schedule": scraper.tasks["magazines"]["en"]["schedule"],
"kwargs": {
"language": "en",
}
Expand Down
File renamed without changes.
File renamed without changes.
6 changes: 3 additions & 3 deletions nous_aggregator/urls.py → config/urls.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""nous_aggregator URL Configuration
"""nous-aggregator URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/4.0/topics/http/urls/
Expand All @@ -15,7 +15,7 @@
"""
from typing import List

from django.conf import settings
from decouple import config
from django.contrib import admin
from django.urls import include, path
from django.urls.resolvers import URLResolver
Expand All @@ -28,7 +28,7 @@
]


if settings.DEBUG is True:
if config("DJANGO_ENV") == "LOCAL":
urlpatterns += [
path('__debug__/', include('debug_toolbar.urls')),
]
4 changes: 2 additions & 2 deletions nous_aggregator/wsgi.py → config/wsgi.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""
WSGI config for nous_aggregator project.
WSGI config for nous-aggregator project.
It exposes the WSGI callable as a module-level variable named ``application``.
Expand All @@ -12,6 +12,6 @@
from django.core.handlers.wsgi import WSGIHandler
from django.core.wsgi import get_wsgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'nous_aggregator.settings')
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'config.settings')

application: WSGIHandler = get_wsgi_application()
2 changes: 1 addition & 1 deletion heroku.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ release:
image: web

run:
web: gunicorn nous_aggregator.wsgi
web: gunicorn config.wsgi
worker:
command:
- python manage.py scrape
Expand Down
2 changes: 1 addition & 1 deletion manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

def main() -> None:
"""Run administrative tasks."""
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'nous_aggregator.settings')
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'config.settings')
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
Expand Down
16 changes: 14 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
# mypy
[tool.mypy]
python_version = 3.11
plugins = ["mypy_django_plugin.main"]
python_executable="home/pi-sigma/.virtualenvs/nous/bin/python"
ignore_missing_imports=true

[tool.django-stubs]
django_settings_module = "nous_aggregator.settings.local"
django_settings_module = "config.settings"

# pytest
[tool.pytest.ini_options]
DJANGO_SETTINGS_MODULE = "config.settings"

# pytype
[too.pytype]
python_version = 3.11
inputs = ["src/articles/management", "src/articles/scraper", "src/articles/*.py"]
disable = "import-error"
2 changes: 1 addition & 1 deletion requirements/dev.in
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ black
flake8
isort

# Debug tooling
# Development & debug tooling
django-debug-toolbar
django-extensions
ipython
Expand Down
Loading

0 comments on commit b56a887

Please sign in to comment.