Skip to content

Commit

Permalink
TL-4 add modules, users apps & postgres connection
Browse files Browse the repository at this point in the history
  • Loading branch information
zeeshanrafiqrana committed Oct 19, 2023
1 parent c87c949 commit 7c09139
Show file tree
Hide file tree
Showing 21 changed files with 259 additions and 3 deletions.
4 changes: 3 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
SECRET_KEY='2w3e4r5t6y7u8i9o023e4r5t6y7u8i9o0-3e4r5t6y7'
DEBUG=True
SECRET_KEY='2w3e4r5t6y7u8i9o023e4r5t6y7u8i9o0-3e4r5t6y7'
DATABASE_URL=postgres://postgres:postgres@localhost:5432/propel_pro
1 change: 1 addition & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ name = "pypi"
[packages]
django = "==4.2.6"
python-environ = "==0.4.54"
psycopg2-binary = "==2.9.9"

[dev-packages]

Expand Down
78 changes: 77 additions & 1 deletion Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ Django PropelPro is your indispensable tool for launching Django projects with r

2. **Configuration**: Customize your project settings and secrets in the `.env` file.

```bash
cp .env.example .env
# Customize your settings in the .env file
```

3. **Database Setup**: Run migrations and create a superuser.

```bash
Expand Down
Empty file added apps/__init__.py
Empty file.
Empty file added apps/users/__init__.py
Empty file.
3 changes: 3 additions & 0 deletions apps/users/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.contrib import admin

# Register your models here.
7 changes: 7 additions & 0 deletions apps/users/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from django.apps import AppConfig


class UsersConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = "apps.users"
label = "users"
Empty file.
3 changes: 3 additions & 0 deletions apps/users/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.db import models

# Create your models here.
3 changes: 3 additions & 0 deletions apps/users/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.test import TestCase

# Create your tests here.
3 changes: 3 additions & 0 deletions apps/users/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.shortcuts import render

# Create your views here.
Empty file added modules/__init__.py
Empty file.
28 changes: 28 additions & 0 deletions modules/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Import necessary modules
from importlib import import_module
from pathlib import Path
from django.conf import settings

# WARNING: Do not remove or change this code snippet; it's essential for functionality

try:
# Define the directory where the modules are located
modules_dir = f"{settings.BASE_DIR}/modules/"

# Recursively search for 'admin.py' files within the modules directory
admins = list(Path(modules_dir).rglob('admin.py'))

# Iterate through the 'admin.py' files
for admin in admins:
# Extract the module name from the path
module_name, _ = admin.as_posix().split('/')[-2:]

# Check if the module name is not 'modules'
if not module_name == "modules":
# Dynamically import the 'admin' module for the current module
module = import_module(f"modules.{module_name}.admin")

# Import all items from the 'module' (admin module) into the current namespace
from module import * # noqa
except (ImportError, IndexError):
pass
5 changes: 5 additions & 0 deletions modules/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.apps import AppConfig


class ModulesConfig(AppConfig):
name = 'modules'
33 changes: 33 additions & 0 deletions modules/manifest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Import the pathlib module for working with file paths
from pathlib import Path

# Define the name of the modules package and the path to the modules directory
MODULES_PACKAGE_NAME = "modules"
MODULES_DIR = f"{Path.cwd()}/{MODULES_PACKAGE_NAME}/"

# Recursively search for 'apps.py' files within the modules directory
APPS = Path(MODULES_DIR).rglob("apps.py")


# Define a function to get a list of module names
def get_modules():
try:
# Initialize an empty list to store module names
modules = []

# Define the separator for Python package names (dot)
python_package_separator = "."

# Iterate through the 'apps.py' files found in the modules directory
for app in APPS:
# Convert the path to a Python package-style name
app_name = app.as_posix().replace(MODULES_DIR, f"{MODULES_PACKAGE_NAME}/")
app_name = python_package_separator.join(app_name.split("/")[:-1])

# Add the module name to the list
modules.append(app_name)

# Return the list of module names
return modules
except (ImportError, IndexError):
pass
Empty file added modules/migrations/__init__.py
Empty file.
1 change: 1 addition & 0 deletions modules/options.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "module_options": {} }
37 changes: 37 additions & 0 deletions modules/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Import necessary modules
from pathlib import Path
from django.conf import settings
from django.urls import path, include
from django.db.utils import ProgrammingError

# Initialize an empty list to hold URL patterns
urlpatterns = []

# This code snippet is designed to dynamically include URL patterns from Django modules.
# It should not be removed or modified for certain functionalities.

# Try to include URL patterns from dynamically discovered modules.
try:
# Define the directory where the modules are located
modules_dir = f"{settings.BASE_DIR}/modules/"

# Recursively search for 'urls.py' files within the modules directory
urls = Path(modules_dir).rglob("urls.py")

# Iterate through the discovered 'urls.py' files
for url in urls:
# Extract the module name from the path
module_name, _ = url.as_posix().split("/")[-2:]

# Check if the module name is not 'modules'
if not module_name == "modules":
# Convert module_name to a URL-friendly format
module_url = module_name.replace("_", "-")

# Include the module's URL patterns using the 'include' function
urlpatterns += [
path(f"{module_url}/", include(f"modules.{module_name}.urls"))
]
except (ImportError, IndexError, ProgrammingError):
# Handle exceptions that may occur during module discovery
pass
29 changes: 29 additions & 0 deletions modules/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Import necessary modules
import importlib
import json
from pathlib import Path

# Define the path to the global options JSON file
GLOBAL_OPTIONS_FILE_PATH = f"{Path.cwd()}/modules/options.json"


# Define a function to retrieve module-specific options
def get_options(module_slug, option_key):
# Open and read the global options JSON file
with open(GLOBAL_OPTIONS_FILE_PATH, "r") as f:
module_options = json.loads(f.read())

# Search for the specified module's options based on its slug
option_value = [
module.get(option_key)
for module in module_options["module_options"]
if module.get("slug") == module_slug
]

# Get the default value for the option by dynamically importing the module
default_value = getattr(
importlib.import_module(f"modules.{module_slug}.options"), option_key
)

# Return the option value if found, otherwise return the default value
return option_value[0] if option_value else default_value
22 changes: 21 additions & 1 deletion swiftstart/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import os
import environ
from pathlib import Path
from modules.manifest import get_modules


# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
Expand All @@ -25,7 +27,7 @@
SECRET_KEY = env("SECRET_KEY", default="VDOmDEnYFEpfljjzxJpMoGhATovkyKqzVIUxbpiN")

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
DEBUG = env.bool("DEBUG", default=False)

ALLOWED_HOSTS = []

Expand All @@ -41,6 +43,19 @@
'django.contrib.staticfiles',
]

DJANGO_LOCAL_APPS = [
"apps.users.apps.UsersConfig",
]

THIRD_PARTY_APPS = [

]

MODULES_APPS = get_modules()

INSTALLED_APPS += DJANGO_LOCAL_APPS + THIRD_PARTY_APPS + MODULES_APPS


MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
Expand Down Expand Up @@ -82,6 +97,11 @@
}
}

if env.str("DATABASE_URL", default=None):
DATABASES = {
'default': env.db()
}


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

0 comments on commit 7c09139

Please sign in to comment.