Skip to content

Commit

Permalink
feat: Update project structure and settings; add runtime file, flake8…
Browse files Browse the repository at this point in the history
… configuration, and custom error handling views
  • Loading branch information
AhmedNassar7 committed Dec 13, 2024
1 parent a25c5b8 commit 29d4ecf
Show file tree
Hide file tree
Showing 42 changed files with 1,627 additions and 510 deletions.
4 changes: 4 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[flake8]
max-line-length = 88
ignore = E203, E266, E501, W503
exclude = .git,__pycache__,docs/source/conf.py,old,build,dista
31 changes: 25 additions & 6 deletions .github/workflows/django.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,42 @@ on:

jobs:
build:

runs-on: ubuntu-latest
strategy:
max-parallel: 4
matrix:
python-version: [3.7, 3.8, 3.9]
python-version: [3.9]

steps:
- uses: actions/checkout@v4
- name: Checkout code
uses: actions/checkout@v4

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v3
with:
python-version: ${{ matrix.python-version }}
- name: Install Dependencies

- name: Install Poetry
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
pip install poetry
- name: Install Dependencies
run: |
poetry install
- name: Export requirements.txt for Deployment
run: |
poetry export --without-hashes -o requirements.txt
- name: Deploy to Render
env:
SECRET_KEY: ${{ secrets.SECRET_KEY }}
DATABASE_URL: ${{ secrets.DATABASE_URL }}
run: |
echo "Starting deployment to Render..."
render deploy --service-name egypt-metro
echo "Deployment complete!"
- name: Run Tests
run: |
python manage.py test
38 changes: 38 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# .pre-commit-config.yaml

# Repositories that contain hooks
repos:
# Black: Code formatter for Python
- repo: https://github.com/psf/black
rev: 23.3.0
hooks:
- id: black
args: ['--line-length=88'] # Customize the line length here if needed

# Flake8: Linter for Python
- repo: https://github.com/PyCQA/flake8
rev: 6.0.0
hooks:
- id: flake8
args:
- --max-line-length=88 # Customize the max line length if needed
# additional_dependencies:
# - flake8-bugbear # Optional: Adds additional linting checks
# - flake8-docstrings # Optional: Adds docstring linting checks

# Isort: Sorts imports alphabetically and automatically separates them into sections
# - repo: https://github.com/pre-commit/mirrors-isort
# rev: v5.12.0 # Use the latest valid version
# hooks:
# - id: isort
# args: ['--profile=black'] # Align with Black's formatting style
# additional_dependencies:
# - isort[app] # Optional: Install extra dependencies for app-specific sorting

# Pydocstyle: Checks compliance with Python docstring conventions
# - repo: https://github.com/PyCQA/pydocstyle
# rev: 6.1.1
# hooks:
# - id: pydocstyle
# args:
# - --convention=google # Use the Google style for docstrings (customize as needed)
1 change: 0 additions & 1 deletion Procfile

This file was deleted.

34 changes: 21 additions & 13 deletions apps/stations/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,45 @@
from django.contrib import admin
from .models import Line, Station, LineStation


class LineStationInline(admin.TabularInline):
"""
Inline admin for LineStation to manage the relationship between lines and stations.
"""
model = LineStation # model to display
extra = 1 # number of extra fields to display
fields = ["station", "order"] # fields to display
ordering = ["order"] # order by order

model = LineStation # model to display
extra = 1 # number of extra fields to display
fields = ["station", "order"] # fields to display
ordering = ["order"] # order by order


@admin.register(Line)
class LineAdmin(admin.ModelAdmin):
"""
Admin configuration for the Line model.
"""

list_display = ("name", "total_stations") # fields to display
search_fields = ("name",) # fields to search
inlines = [LineStationInline] # inline to display
search_fields = ("name",) # fields to search
inlines = [LineStationInline] # inline to display


@admin.register(Station)
class StationAdmin(admin.ModelAdmin):
"""
Admin configuration for the Station model.
"""

list_display = ("name", "is_interchange") # fields to display
search_fields = ("name",) # fields to search
inlines = [LineStationInline] # inline to display
search_fields = ("name",) # fields to search
inlines = [LineStationInline] # inline to display
fieldsets = (
(None, {
"fields": ("name", "latitude", "longitude"), # fields to display
}),
(
None,
{
"fields": ("name", "latitude", "longitude"), # fields to display
},
),
)


Expand All @@ -43,6 +50,7 @@ class LineStationAdmin(admin.ModelAdmin):
"""
Admin configuration for the LineStation model.
"""

list_display = ("line", "station", "order") # fields to display
list_filter = ("line",) # fields to filter
ordering = ["line", "order"] # order by line and order
list_filter = ("line",) # fields to filter
ordering = ["line", "order"] # order by line and order
5 changes: 3 additions & 2 deletions apps/stations/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from django.apps import AppConfig


class StationsConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'apps.stations'
default_auto_field = "django.db.models.BigAutoField"
name = "apps.stations"
98 changes: 79 additions & 19 deletions apps/stations/management/commands/populate_metro_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from django.db import transaction
from apps.stations.models import Line, Station, LineStation, ConnectingStation


class Command(BaseCommand):
help = "Populate metro lines, stations, and connections data"

Expand Down Expand Up @@ -41,7 +42,11 @@ class Command(BaseCommand):
{"name": "Hammamat El-Qobba", "latitude": 30.090, "longitude": 31.298},
{"name": "Saray El-Qobba", "latitude": 30.098, "longitude": 31.303},
{"name": "Hadayek El-Zaitoun", "latitude": 30.105, "longitude": 31.310},
{"name": "Helmeyet El-Zaitoun", "latitude": 30.115, "longitude": 31.314},
{
"name": "Helmeyet El-Zaitoun",
"latitude": 30.115,
"longitude": 31.314,
},
{"name": "El-Matareyya", "latitude": 30.121, "longitude": 31.315},
{"name": "Ain Shams", "latitude": 30.131, "longitude": 31.318},
{"name": "Ezbet El-Nakhl", "latitude": 30.139, "longitude": 31.324},
Expand All @@ -54,10 +59,18 @@ class Command(BaseCommand):
"stations": [
{"name": "El-Mounib", "latitude": 29.98139, "longitude": 31.21194},
{"name": "Sakiat Mekky", "latitude": 29.99556, "longitude": 31.20861},
{"name": "Omm El-Masryeen", "latitude": 30.00528, "longitude": 31.20806},
{
"name": "Omm El-Masryeen",
"latitude": 30.00528,
"longitude": 31.20806,
},
{"name": "El Giza", "latitude": 30.01056, "longitude": 31.20722},
{"name": "Faisal", "latitude": 30.01722, "longitude": 31.20389},
{"name": "Cairo University", "latitude": 30.02611, "longitude": 31.20111},
{
"name": "Cairo University",
"latitude": 30.02611,
"longitude": 31.20111,
},
{"name": "El Bohoth", "latitude": 30.03583, "longitude": 31.20028},
{"name": "Dokki", "latitude": 30.03833, "longitude": 31.21194},
{"name": "Opera", "latitude": 30.04222, "longitude": 31.22528},
Expand All @@ -70,25 +83,45 @@ class Command(BaseCommand):
{"name": "St. Teresa", "latitude": 30.08833, "longitude": 31.24556},
{"name": "Khalafawy", "latitude": 30.09806, "longitude": 31.24528},
{"name": "Mezallat", "latitude": 30.10500, "longitude": 31.24667},
{"name": "Kolleyyet El-Zeraa", "latitude": 30.11389, "longitude": 31.24861},
{"name": "Shubra El-Kheima", "latitude": 30.12250, "longitude": 31.24472},
{
"name": "Kolleyyet El-Zeraa",
"latitude": 30.11389,
"longitude": 31.24861,
},
{
"name": "Shubra El-Kheima",
"latitude": 30.12250,
"longitude": 31.24472,
},
],
},
{
"line": {"name": "Third Line", "color_code": "#0000FF"},
"stations": [
{"name": "Adly Mansour", "latitude": 30.14694, "longitude": 31.42139},
{"name": "El Haykestep", "latitude": 30.14389, "longitude": 31.40472},
{"name": "Omar Ibn El-Khattab", "latitude": 30.14056, "longitude": 31.39417},
{
"name": "Omar Ibn El-Khattab",
"latitude": 30.14056,
"longitude": 31.39417,
},
{"name": "Qobaa", "latitude": 30.13472, "longitude": 31.38389},
{"name": "Hesham Barakat", "latitude": 30.13111, "longitude": 31.37389},
{"name": "El-Nozha", "latitude": 30.12833, "longitude": 31.36000},
{"name": "Nadi El-Shams", "latitude": 30.12222, "longitude": 31.34389},
{"name": "Alf Maskan", "latitude": 30.11806, "longitude": 31.33972},
{"name": "Heliopolis Square", "latitude": 30.10806, "longitude": 31.33722},
{
"name": "Heliopolis Square",
"latitude": 30.10806,
"longitude": 31.33722,
},
{"name": "Haroun", "latitude": 30.10000, "longitude": 31.33278},
{"name": "Al-Ahram", "latitude": 30.09139, "longitude": 31.32639},
{"name": "Koleyet El-Banat", "latitude": 30.08361, "longitude": 31.32889},
{
"name": "Koleyet El-Banat",
"latitude": 30.08361,
"longitude": 31.32889,
},
{"name": "Stadium", "latitude": 30.07306, "longitude": 31.31750},
{"name": "Fair Zone", "latitude": 30.07333, "longitude": 31.30111},
{"name": "Abbassia", "latitude": 30.06972, "longitude": 31.28083},
Expand All @@ -103,14 +136,34 @@ class Command(BaseCommand):
{"name": "Sudan Street", "latitude": 30.06972, "longitude": 31.20472},
{"name": "Imbaba", "latitude": 30.07583, "longitude": 31.20750},
{"name": "El-Bohy", "latitude": 30.08222, "longitude": 31.21056},
{"name": "Al-Qawmeya Al-Arabiya", "latitude": 30.09333, "longitude": 31.20889},
{
"name": "Al-Qawmeya Al-Arabiya",
"latitude": 30.09333,
"longitude": 31.20889,
},
{"name": "Ring Road", "latitude": 30.09639, "longitude": 31.19972},
{"name": "Rod al-Farag Axis", "latitude": 30.10194, "longitude": 31.18417},
{
"name": "Rod al-Farag Axis",
"latitude": 30.10194,
"longitude": 31.18417,
},
{"name": "El-Tawfikeya", "latitude": 30.06528, "longitude": 31.20250},
{"name": "Wadi El-Nil", "latitude": 30.05833, "longitude": 31.20000},
{"name": "Gamaat El Dowal Al-Arabiya", "latitude": 30.05083, "longitude": 31.19972},
{"name": "Bulaq El-Dakroor", "latitude": 30.03611, "longitude": 31.19639},
{"name": "Cairo University", "latitude": 30.02611, "longitude": 31.20111},
{
"name": "Gamaat El Dowal Al-Arabiya",
"latitude": 30.05083,
"longitude": 31.19972,
},
{
"name": "Bulaq El-Dakroor",
"latitude": 30.03611,
"longitude": 31.19639,
},
{
"name": "Cairo University",
"latitude": 30.02611,
"longitude": 31.20111,
},
],
},
]
Expand All @@ -130,7 +183,9 @@ def handle(self, *args, **kwargs):
self.populate_lines_and_stations()
self.populate_connecting_stations()

self.stdout.write(self.style.SUCCESS("Metro data population completed successfully."))
self.stdout.write(
self.style.SUCCESS("Metro data population completed successfully.")
)

def clear_existing_data(self):
"""Clear existing LineStation and ConnectingStation data."""
Expand Down Expand Up @@ -177,10 +232,15 @@ def populate_connecting_stations(self):
lines = Line.objects.filter(name__in=conn_station["lines"])
for line in lines:
# Check if the ConnectingStation already exists
if not ConnectingStation.objects.filter(station=station, lines=line).exists():
connecting_station, created = ConnectingStation.objects.get_or_create(
station=station
)
if not ConnectingStation.objects.filter(
station=station, lines=line
).exists():
(
connecting_station,
created,
) = ConnectingStation.objects.get_or_create(station=station)
connecting_station.lines.add(line)
action = "Created" if created else "Already exists"
self.stdout.write(f"{action} ConnectingStation: {station.name} - {line.name}")
self.stdout.write(
f"{action} ConnectingStation: {station.name} - {line.name}"
)
Loading

0 comments on commit 29d4ecf

Please sign in to comment.