Skip to content

Commit

Permalink
📚 🔨 doc: refactor&document-source-code
Browse files Browse the repository at this point in the history
  • Loading branch information
ARYAN-NIKNEZHAD committed Aug 4, 2024
1 parent ea66d2e commit 99d7242
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 32 deletions.
2 changes: 0 additions & 2 deletions iranian_cities/admin.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
from django.contrib import admin
from django.contrib.admin import widgets
from django.contrib.admin.options import InlineModelAdmin
from django.db.models import ForeignKey
from django.http import HttpRequest
from typing import Any, Optional
from iranian_cities.mixins.dynamic_permission import IranianCitiesAdminReadOnlyEnabled, DynamicInlineAdmin
from iranian_cities.conf import sage_iranian_cities_settings
from iranian_cities.mixins.base_inline import BaseTabularInline
from iranian_cities.models import (
Province, County, District,
Expand Down
4 changes: 2 additions & 2 deletions iranian_cities/checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def get_iranian_cities_settings() -> Dict[str, Any]:
sage_iranian_cities_admin_delete_readonly_enabled: bool = sage_iranian_cities_settings.IRANIAN_CITIES_ADMIN_DELETE_READONLY_ENABLED
sage_iranian_cities_admin_change_readonly_enabled: bool = sage_iranian_cities_settings.IRANIAN_CITIES_ADMIN_CHANGE_READONLY_ENABLED
sage_iranian_cities_admin_inline_enabled: bool = sage_iranian_cities_settings.IRANIAN_CITIES_ADMIN_INLINE_ENABLED

return {
"IRANIAN_CITIES_ADMIN_ADD_READONLY_ENABLED": sage_iranian_cities_admin_add_readonly_enabled,
"IRANIAN_CITIES_ADMIN_DELETE_READONLY_ENABLED": sage_iranian_cities_admin_delete_readonly_enabled,
Expand Down Expand Up @@ -75,5 +75,5 @@ def check_missing_configs(settings: Dict[str, Any]) -> None:
id=id,
)
)

return errors
2 changes: 1 addition & 1 deletion iranian_cities/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class SageIranianCitiesSettings:
_settings: Dict[str, bool]

def __init__(self):

self._settings = {}
for setting, default in DEFAULT_SETTINGS.items():
value: Any = getattr(settings, setting, default)
Expand Down
5 changes: 3 additions & 2 deletions iranian_cities/constants.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from typing import Dict

# Default values for permissions
DEFAULT_SETTINGS = {
DEFAULT_SETTINGS: Dict[str, bool] = {
"IRANIAN_CITIES_ADMIN_ADD_READONLY_ENABLED": True,
"IRANIAN_CITIES_ADMIN_DELETE_READONLY_ENABLED": True,
"IRANIAN_CITIES_ADMIN_CHANGE_READONLY_ENABLED": True,
"IRANIAN_CITIES_ADMIN_INLINE_ENABLED": True,
"IRANIAN_CITIES_ADMIN_INLINE_ENABLED": False,
}
44 changes: 26 additions & 18 deletions iranian_cities/fields.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
from typing import Any
from django.db import models

from iranian_cities.models import (
Province, County, District,
City, RuralDistrict, Village
)

from iranian_cities.models import Province, County, District, City, RuralDistrict, Village

class ProvinceField(models.ForeignKey):
description = 'Iranian Province'
"""A ForeignKey field for Iranian Provinces."""

def __init__(self, *args, **kwargs):
description: str = 'Iranian Province'

def __init__(self, *args: Any, **kwargs: Any) -> None:
defaults = {
'to': Province,
'on_delete': models.CASCADE
Expand All @@ -18,9 +16,11 @@ def __init__(self, *args, **kwargs):
super().__init__(*args, **defaults)

class CountyField(models.ForeignKey):
description = 'Iranian County'
"""A ForeignKey field for Iranian Counties."""

description: str = 'Iranian County'

def __init__(self, *args, **kwargs):
def __init__(self, *args: Any, **kwargs: Any) -> None:
defaults = {
'to': County,
'on_delete': models.CASCADE
Expand All @@ -29,9 +29,11 @@ def __init__(self, *args, **kwargs):
super().__init__(*args, **defaults)

class DistrictField(models.ForeignKey):
description = 'Iranian District'
"""A ForeignKey field for Iranian Districts."""

def __init__(self, *args, **kwargs):
description: str = 'Iranian District'

def __init__(self, *args: Any, **kwargs: Any) -> None:
defaults = {
'to': District,
'on_delete': models.CASCADE
Expand All @@ -40,9 +42,11 @@ def __init__(self, *args, **kwargs):
super().__init__(*args, **defaults)

class CityField(models.ForeignKey):
description = 'Iranian City'
"""A ForeignKey field for Iranian Cities."""

description: str = 'Iranian City'

def __init__(self, *args, **kwargs):
def __init__(self, *args: Any, **kwargs: Any) -> None:
defaults = {
'to': City,
'on_delete': models.CASCADE
Expand All @@ -51,9 +55,11 @@ def __init__(self, *args, **kwargs):
super().__init__(*args, **defaults)

class RuralDistrictField(models.ForeignKey):
description = 'Iranian RuralDistrict'
"""A ForeignKey field for Iranian Rural Districts."""

def __init__(self, *args, **kwargs):
description: str = 'Iranian Rural District'

def __init__(self, *args: Any, **kwargs: Any) -> None:
defaults = {
'to': RuralDistrict,
'on_delete': models.CASCADE
Expand All @@ -62,9 +68,11 @@ def __init__(self, *args, **kwargs):
super().__init__(*args, **defaults)

class VillageField(models.ForeignKey):
description = 'Iranian Village'
"""A ForeignKey field for Iranian Villages."""

description: str = 'Iranian Village'

def __init__(self, *args, **kwargs):
def __init__(self, *args: Any, **kwargs: Any) -> None:
defaults = {
'to': Village,
'on_delete': models.CASCADE
Expand Down
9 changes: 4 additions & 5 deletions iranian_cities/management/commands/generate_city.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import csv
import logging
from django.core.management import BaseCommand
from django.db import transaction, connections
from iranian_cities import data
from iranian_cities.models import Province, County, District, City, RuralDistrict, Village
from typing import List, Dict, Tuple, Union
Expand All @@ -12,7 +11,7 @@

class Command(BaseCommand):
"""Management command to generate and populate database tables for Iranian cities."""

help = 'Generate all data'

def add_arguments(self, parser) -> None:
Expand Down Expand Up @@ -61,7 +60,7 @@ def prompt_user(self) -> Tuple[bool, str]:
f"Your database currently has objects for the following tables: {data_present}. "
"\nDo you want to flush the tables? Type 'yes' to flush or 'no' to cancel the operation: "
).strip().lower()

if response not in ['yes', 'no']:
print("Invalid response. Please type 'yes' or 'no'.")
elif response == 'yes':
Expand All @@ -83,11 +82,11 @@ def prompt_user(self) -> Tuple[bool, str]:
result = True if response == 'yes' or state == "flush" else False
response_by_user = "cancel" if response == 'no' or state == "cancel" else "yes"
return result, response_by_user

result = False
response_by_user = "no"
return result, response_by_user


def flush_tables(self) -> None:
"""Delete all records from the relevant tables."""
Expand Down
4 changes: 2 additions & 2 deletions iranian_cities/mixins/base_location.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class BaseLocation(Model):
verbose_name=_("Code"),
help_text=_("The code representing the location."),
db_comment="This field stores the code for the location.",
unique=True
unique=True
)


Expand All @@ -28,4 +28,4 @@ class Meta:
ordering = ["id"]
indexes = [
Index(fields=["code"]),
]
]

0 comments on commit 99d7242

Please sign in to comment.