Skip to content

Commit

Permalink
🔨 refactor: solve-pylint-problems
Browse files Browse the repository at this point in the history
  • Loading branch information
ARYAN-NIKNEZHAD committed Aug 18, 2024
1 parent 856837c commit 6de7976
Show file tree
Hide file tree
Showing 16 changed files with 405 additions and 275 deletions.
13 changes: 0 additions & 13 deletions .pylintrc

This file was deleted.

143 changes: 94 additions & 49 deletions iranian_cities/admin.py
Original file line number Diff line number Diff line change
@@ -1,132 +1,177 @@
from typing import Any, Optional

from django.contrib import admin
from django.contrib.admin import widgets
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.mixins.base_inline import BaseTabularInline
from iranian_cities.mixins.dynamic_permission import (
DynamicInlineAdmin,
IranianCitiesAdminReadOnlyEnabled,
)
from iranian_cities.models import (
Province, County, District,
City, RuralDistrict, Village
City,
County,
District,
Province,
RuralDistrict,
Village,
)


class IranianCitiesAdmin(admin.ModelAdmin):
"""Custom admin model for Iranian cities with a custom form field for foreign key."""

def formfield_for_foreignkey(self, db_field: ForeignKey, request: HttpRequest, **kwargs: Any) -> Any:
def formfield_for_foreignkey(
self, db_field: ForeignKey, request: HttpRequest, **kwargs: Any
) -> Any:
"""Override the default form field for foreign keys to use ForeignKeyRawIdWidget."""
db: Optional[str] = kwargs.get('using')
kwargs['widget'] = widgets.ForeignKeyRawIdWidget(
db_field.remote_field, self.admin_site, using=db)
db: Optional[str] = kwargs.get("using")
kwargs["widget"] = widgets.ForeignKeyRawIdWidget(
db_field.remote_field, self.admin_site, using=db
)

if 'queryset' not in kwargs:
if "queryset" not in kwargs:
queryset = self.get_field_queryset(db, db_field, request)
if queryset is not None:
kwargs['queryset'] = queryset
kwargs["queryset"] = queryset

return db_field.formfield(**kwargs)


class CountyInline(BaseTabularInline):
"""Inline admin model for County."""

model = County
extra = 1
raw_id_fields = ['province']
raw_id_fields = ["province"]
max_num = 5


class DistrictInline(BaseTabularInline):
"""Inline admin model for District."""

model = District
extra = 1
raw_id_fields = ['county', 'province']
raw_id_fields = ["county", "province"]
max_num = 5


class CityInline(BaseTabularInline):
"""Inline admin model for City."""

model = City
extra = 1
raw_id_fields = ['district', 'county', 'province']
raw_id_fields = ["district", "county", "province"]
max_num = 5


class RuralDistrictInline(BaseTabularInline):
"""Inline admin model for RuralDistrict."""

model = RuralDistrict
extra = 1
raw_id_fields = ['district', 'county', 'province']
raw_id_fields = ["district", "county", "province"]
max_num = 5


class VillageInline(BaseTabularInline):
"""Inline admin model for Village."""

model = Village
extra = 1
raw_id_fields = ['rural_district', 'district', 'county', 'province']
raw_id_fields = ["rural_district", "district", "county", "province"]
max_num = 5


@admin.register(Province)
class ProvinceAdmin(IranianCitiesAdminReadOnlyEnabled, DynamicInlineAdmin, admin.ModelAdmin):
class ProvinceAdmin(
IranianCitiesAdminReadOnlyEnabled, DynamicInlineAdmin, admin.ModelAdmin
):
"""Admin model for Province."""
list_display = ['name', 'code']
search_fields = ['name', 'code']

list_display = ["name", "code"]
search_fields = ["name", "code"]


@admin.register(County)
class CountyAdmin(IranianCitiesAdminReadOnlyEnabled, DynamicInlineAdmin, admin.ModelAdmin):
class CountyAdmin(
IranianCitiesAdminReadOnlyEnabled, DynamicInlineAdmin, admin.ModelAdmin
):
"""Admin model for County."""
list_display = ['name', 'code', 'province']
list_filter = ['province']
search_fields = ['name', 'code', 'province__name']
raw_id_fields = ['province']

list_display = ["name", "code", "province"]
list_filter = ["province"]
search_fields = ["name", "code", "province__name"]
raw_id_fields = ["province"]


@admin.register(City)
class CityAdmin(IranianCitiesAdminReadOnlyEnabled, DynamicInlineAdmin, admin.ModelAdmin):
class CityAdmin(
IranianCitiesAdminReadOnlyEnabled, DynamicInlineAdmin, admin.ModelAdmin
):
"""Admin model for City."""
list_display = [
'name', 'code', 'city_type',
'district', 'county', 'province'
]
list_filter = ['province']

list_display = ["name", "code", "city_type", "district", "county", "province"]
list_filter = ["province"]
search_fields = [
'name', 'code', 'city_type',
'district__name', 'county__name', 'province__name'
"name",
"code",
"city_type",
"district__name",
"county__name",
"province__name",
]
raw_id_fields = ['district', 'county', 'province']
raw_id_fields = ["district", "county", "province"]


@admin.register(District)
class DistrictAdmin(IranianCitiesAdminReadOnlyEnabled, DynamicInlineAdmin, admin.ModelAdmin):
class DistrictAdmin(
IranianCitiesAdminReadOnlyEnabled, DynamicInlineAdmin, admin.ModelAdmin
):
"""Admin model for District."""
list_display = ['name', 'code', 'county', 'province']
list_filter = ['province']
search_fields = ['name', 'code', 'county__name', 'province__name']
raw_id_fields = ['county', 'province']

list_display = ["name", "code", "county", "province"]
list_filter = ["province"]
search_fields = ["name", "code", "county__name", "province__name"]
raw_id_fields = ["county", "province"]


@admin.register(RuralDistrict)
class RuralDistrictAdmin(IranianCitiesAdminReadOnlyEnabled, DynamicInlineAdmin, admin.ModelAdmin):
class RuralDistrictAdmin(
IranianCitiesAdminReadOnlyEnabled, DynamicInlineAdmin, admin.ModelAdmin
):
"""Admin model for RuralDistrict."""
list_display = ['name', 'code', 'district', 'county', 'province']
list_filter = ['province']
search_fields = ['name', 'code', 'district__name', 'county__name', 'province__name']
raw_id_fields = ['district', 'county', 'province']

list_display = ["name", "code", "district", "county", "province"]
list_filter = ["province"]
search_fields = ["name", "code", "district__name", "county__name", "province__name"]
raw_id_fields = ["district", "county", "province"]


@admin.register(Village)
class VillageAdmin(IranianCitiesAdminReadOnlyEnabled, DynamicInlineAdmin, admin.ModelAdmin):
class VillageAdmin(
IranianCitiesAdminReadOnlyEnabled, DynamicInlineAdmin, admin.ModelAdmin
):
"""Admin model for Village."""

list_display = [
'name', 'code', 'village_type',
'rural_district', 'district', 'county', 'province'
"name",
"code",
"village_type",
"rural_district",
"district",
"county",
"province",
]
list_filter = ['province']
list_filter = ["province"]
search_fields = [
'name', 'code', 'rural_district__name',
'district__name', 'county__name', 'province__name'
"name",
"code",
"rural_district__name",
"district__name",
"county__name",
"province__name",
]
raw_id_fields = ['rural_district', 'district', 'county', 'province']
raw_id_fields = ["rural_district", "district", "county", "province"]
6 changes: 3 additions & 3 deletions iranian_cities/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@


class IranianCitiesConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'iranian_cities'
verbose_name = _('Iranian Cities')
default_auto_field = "django.db.models.BigAutoField"
name = "iranian_cities"
verbose_name = _("Iranian Cities")

def ready(self) -> None:
from . import checks
30 changes: 19 additions & 11 deletions iranian_cities/checks.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
from typing import Any, Dict, List

from django.core.checks import Error, register
from typing import List, Dict, Any

from iranian_cities.conf import sage_iranian_cities_settings
from iranian_cities.exc import (
IranianCitiesConfigurationError,
)
from iranian_cities.exc import IranianCitiesConfigurationError


@register()
def check_iranian_cities_config(app_configs: Dict[str, Any], **kwargs: Any) -> List[Error]:
def check_iranian_cities_config(
app_configs: Dict[str, Any], **kwargs: Any
) -> List[Error]:
"""
Check the Iranian Cities configuration for the application.
Expand Down Expand Up @@ -43,10 +44,18 @@ def check_iranian_cities_config(app_configs: Dict[str, Any], **kwargs: Any) -> L

def get_iranian_cities_settings() -> Dict[str, Any]:

sage_iranian_cities_admin_add_readonly_enabled: bool = sage_iranian_cities_settings.IRANIAN_CITIES_ADMIN_ADD_READONLY_ENABLED
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
sage_iranian_cities_admin_add_readonly_enabled: bool = (
sage_iranian_cities_settings.IRANIAN_CITIES_ADMIN_ADD_READONLY_ENABLED
)
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,
Expand All @@ -55,9 +64,8 @@ def get_iranian_cities_settings() -> Dict[str, Any]:
"IRANIAN_CITIES_ADMIN_INLINE_ENABLED": sage_iranian_cities_admin_inline_enabled,
}


def check_missing_configs(settings: Dict[str, Any]) -> None:
missing: List[str] = [key for key, value in settings.items() if value == None]
missing: List[str] = [key for key, value in settings.items() if value is None]
if missing:
raise IranianCitiesConfigurationError(
f"Iranian Cities configuration settings are missing: {', '.join(missing)}."
Expand Down
8 changes: 6 additions & 2 deletions iranian_cities/conf.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
from django.conf import settings
from typing import Any, Dict
from iranian_cities.exc import IranianCitiesConfigurationError

from django.conf import settings

from iranian_cities.constants import DEFAULT_SETTINGS
from iranian_cities.exc import IranianCitiesConfigurationError


class SageIranianCitiesSettings:
_settings: Dict[str, bool]
Expand All @@ -24,4 +27,5 @@ def __getattr__(self, item: str) -> bool:
f"'{self.__class__.__name__}' object has no attribute '{item}'"
)


sage_iranian_cities_settings = SageIranianCitiesSettings()
15 changes: 12 additions & 3 deletions iranian_cities/exc.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,17 @@ class SageError(Exception):
default_code: str = "E5000"
section_code: str = "SAGE"

def __init__(self, detail: Optional[str] = None, code: Optional[str] = None, section_code: Optional[str] = None):
def __init__(
self,
detail: Optional[str] = None,
code: Optional[str] = None,
section_code: Optional[str] = None,
):
self.detail: str = detail if detail is not None else self.default_detail
self.code: str = code if code is not None else self.default_code
self.section_code: str = section_code if section_code is not None else self.section_code
self.section_code: str = (
section_code if section_code is not None else self.section_code
)
self.error_id: str = str(uuid.uuid4())

def __str__(self) -> str:
Expand Down Expand Up @@ -69,6 +76,8 @@ class IranianCitiesConfigurationError(IranianCitiesError):
"""

status_code: int = 400
default_detail: str = "Invalid Sage Irianian Cities configuration. Please check your settings."
default_detail: str = (
"Invalid Sage Irianian Cities configuration. Please check your settings."
)
default_code: str = "E4001"
section_code: str = "CFG"
Loading

0 comments on commit 6de7976

Please sign in to comment.