almost finished with the addresses
This commit is contained in:
+30
-1
@@ -1,3 +1,32 @@
|
|||||||
from django.contrib import admin
|
from django.contrib import admin
|
||||||
|
from .models import Address, Province, City
|
||||||
|
|
||||||
# Register your models here.
|
|
||||||
|
class ProvinceAdmin(admin.ModelAdmin):
|
||||||
|
model = Province
|
||||||
|
list_display = (
|
||||||
|
"province_name",
|
||||||
|
"province_id",
|
||||||
|
)
|
||||||
|
|
||||||
|
admin.site.register(Province, ProvinceAdmin)
|
||||||
|
|
||||||
|
class CityAdmin(admin.ModelAdmin):
|
||||||
|
model = City
|
||||||
|
list_display = (
|
||||||
|
"city_name",
|
||||||
|
"city_local_id",
|
||||||
|
"province"
|
||||||
|
)
|
||||||
|
|
||||||
|
admin.site.register(City, CityAdmin)
|
||||||
|
|
||||||
|
class AddressAdmin(admin.ModelAdmin):
|
||||||
|
model = Address
|
||||||
|
list_display = [
|
||||||
|
"address_detail",
|
||||||
|
"province",
|
||||||
|
"city",
|
||||||
|
]
|
||||||
|
|
||||||
|
admin.site.register(Address, AddressAdmin)
|
||||||
@@ -0,0 +1,34 @@
|
|||||||
|
from rest_framework import serializers
|
||||||
|
from ..models import Address, City, Province
|
||||||
|
|
||||||
|
|
||||||
|
class ProvinceSerializer(serializers.ModelSerializer):
|
||||||
|
class Meta:
|
||||||
|
model = Province
|
||||||
|
fields = ["province_id", "province_name"]
|
||||||
|
|
||||||
|
|
||||||
|
class CitySerializer(serializers.ModelSerializer):
|
||||||
|
province_id = serializers.CharField(source="province.province_id")
|
||||||
|
class Meta:
|
||||||
|
model = City
|
||||||
|
fields = ["city_local_id", "city_name", "province_id"]
|
||||||
|
|
||||||
|
class AddressSerializer(serializers.ModelSerializer):
|
||||||
|
province_name = serializers.CharField(source="province.province_name", read_only=True)
|
||||||
|
city_name = serializers.CharField(source="city.city_name", read_only=True)
|
||||||
|
class Meta:
|
||||||
|
model = Address
|
||||||
|
fields = ["province", "province_name", "city", "city_name", "postal_code", "address_detail", "user", "created_at", "updated_at"]
|
||||||
|
read_only_fields = ["user", "province_name", "city_name", "created_at", "updated_at"]
|
||||||
|
|
||||||
|
def to_representation(self, instance):
|
||||||
|
rep = super().to_representation(instance)
|
||||||
|
request = self.context.get("request")
|
||||||
|
if not request.parser_context.get("kwargs"):
|
||||||
|
rep.pop("province_name")
|
||||||
|
rep.pop("city_name")
|
||||||
|
rep.pop("created_at")
|
||||||
|
rep.pop("updated_at")
|
||||||
|
|
||||||
|
return rep
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
from django.urls import path
|
||||||
|
from rest_framework.routers import DefaultRouter
|
||||||
|
from . import views
|
||||||
|
|
||||||
|
app_name = "address-api-urls"
|
||||||
|
|
||||||
|
router = DefaultRouter()
|
||||||
|
router.register("", views.AddressViewSet, basename="address-viewset")
|
||||||
|
|
||||||
|
urlpatterns = [
|
||||||
|
path("province/", views.ProvinceListAPIView.as_view(), name="get-provinces"),
|
||||||
|
path("province/<int:province_pk>/cities/", views.CityListAPIView.as_view(), name="get-cities")
|
||||||
|
]
|
||||||
|
|
||||||
|
urlpatterns += router.urls
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
from rest_framework.generics import ListAPIView
|
||||||
|
from rest_framework import viewsets
|
||||||
|
from rest_framework.permissions import IsAuthenticated
|
||||||
|
from . import serializers
|
||||||
|
from ..models import Province, City, Address
|
||||||
|
|
||||||
|
class ProvinceListAPIView(ListAPIView):
|
||||||
|
serializer_class = serializers.ProvinceSerializer
|
||||||
|
queryset = Province.objects.all()
|
||||||
|
|
||||||
|
# def get(self, request, *args, **kwargs):
|
||||||
|
# return Response()
|
||||||
|
|
||||||
|
|
||||||
|
class CityListAPIView(ListAPIView):
|
||||||
|
serializer_class = serializers.CitySerializer
|
||||||
|
|
||||||
|
def get_queryset(self):
|
||||||
|
province_id = self.kwargs["province_pk"]
|
||||||
|
return City.objects.filter(province_id=province_id)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
class AddressViewSet(viewsets.ModelViewSet):
|
||||||
|
permission_classes = [IsAuthenticated,]
|
||||||
|
serializer_class = serializers.AddressSerializer
|
||||||
|
|
||||||
|
def get_queryset(self):
|
||||||
|
user_id = self.request.user.id
|
||||||
|
return Address.objects.filter(user_id=user_id)
|
||||||
|
|
||||||
|
def perform_create(self, serializer):
|
||||||
|
serializer.save(
|
||||||
|
user_id=self.request.user.id
|
||||||
|
)
|
||||||
|
return super().perform_create(serializer)
|
||||||
@@ -0,0 +1,41 @@
|
|||||||
|
from django.core.management.base import BaseCommand #, CommandError
|
||||||
|
import json
|
||||||
|
from pathlib import Path
|
||||||
|
from ...models import Province, City
|
||||||
|
|
||||||
|
class Command(BaseCommand):
|
||||||
|
help = "push all provinces and cities to database"
|
||||||
|
|
||||||
|
def handle(self, *args, **options):
|
||||||
|
json_file_path = Path("/app/addresses/provinces_cities.json")
|
||||||
|
|
||||||
|
with open(json_file_path, "r", encoding="utf-8") as json_file:
|
||||||
|
all_data = json.load(json_file)
|
||||||
|
|
||||||
|
print("injecting all provinces to the database...")
|
||||||
|
|
||||||
|
for province_data in all_data:
|
||||||
|
province = Province.objects.get_or_create(
|
||||||
|
province_id=int(province_data.get("provinceId")),
|
||||||
|
province_name=str(province_data.get("provinceName")),
|
||||||
|
)
|
||||||
|
|
||||||
|
print("task complete :)")
|
||||||
|
|
||||||
|
print("injecting all the cities to the database ")
|
||||||
|
for city_data in all_data:
|
||||||
|
city = City.objects.get_or_create(
|
||||||
|
city_local_id=str(city_data.get("cityId")),
|
||||||
|
city_name=str(city_data.get("cityName")),
|
||||||
|
province_id=int(city_data.get("provinceId"))
|
||||||
|
)
|
||||||
|
|
||||||
|
print("task complete :)")
|
||||||
|
|
||||||
|
self.stdout.write(
|
||||||
|
self.style.SUCCESS(r"all the data successfully injected in the database :>")
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -0,0 +1,49 @@
|
|||||||
|
# Generated by Django 5.1.15 on 2026-05-12 23:43
|
||||||
|
|
||||||
|
import django.db.models.deletion
|
||||||
|
from django.conf import settings
|
||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
initial = True
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.CreateModel(
|
||||||
|
name='Province',
|
||||||
|
fields=[
|
||||||
|
('province_id', models.IntegerField(primary_key=True, serialize=False)),
|
||||||
|
('province_name', models.CharField(max_length=255)),
|
||||||
|
('created_at', models.DateTimeField(auto_now_add=True)),
|
||||||
|
('updated_at', models.DateTimeField(auto_now=True)),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
migrations.CreateModel(
|
||||||
|
name='City',
|
||||||
|
fields=[
|
||||||
|
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||||
|
('city_local_id', models.CharField(blank=True, max_length=3, null=True)),
|
||||||
|
('city_name', models.CharField(max_length=255)),
|
||||||
|
('created_at', models.DateTimeField(auto_now_add=True)),
|
||||||
|
('updated_at', models.DateTimeField(auto_now=True)),
|
||||||
|
('province', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='city', to='addresses.province')),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
migrations.CreateModel(
|
||||||
|
name='Address',
|
||||||
|
fields=[
|
||||||
|
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||||
|
('address_detail', models.TextField()),
|
||||||
|
('created_at', models.DateTimeField(auto_now_add=True)),
|
||||||
|
('updated_at', models.DateTimeField(auto_now=True)),
|
||||||
|
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
|
||||||
|
('city', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='city', to='addresses.city')),
|
||||||
|
('province', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='province', to='addresses.province')),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
]
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
# Generated by Django 5.1.15 on 2026-05-13 00:47
|
||||||
|
|
||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('addresses', '0001_initial'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.AddField(
|
||||||
|
model_name='address',
|
||||||
|
name='postal_code',
|
||||||
|
field=models.CharField(max_length=15, null=True),
|
||||||
|
),
|
||||||
|
]
|
||||||
+6
-4
@@ -1,5 +1,5 @@
|
|||||||
from django.db import models
|
from django.db import models
|
||||||
|
from config.settings import AUTH_USER_MODEL
|
||||||
|
|
||||||
class Province(models.Model):
|
class Province(models.Model):
|
||||||
province_id = models.IntegerField(primary_key=True)
|
province_id = models.IntegerField(primary_key=True)
|
||||||
@@ -9,25 +9,27 @@ class Province(models.Model):
|
|||||||
updated_at = models.DateTimeField(auto_now=True)
|
updated_at = models.DateTimeField(auto_now=True)
|
||||||
|
|
||||||
def __str__(self) -> str:
|
def __str__(self) -> str:
|
||||||
return f"{self.province_name}"
|
return f"{self.province_id}: {self.province_name}"
|
||||||
|
|
||||||
class City(models.Model):
|
class City(models.Model):
|
||||||
province = models.ForeignKey("Province", on_delete=models.CASCADE, related_name="city")
|
province = models.ForeignKey("Province", on_delete=models.CASCADE, related_name="city")
|
||||||
city_id = models.IntegerField(primary_key=True)
|
city_local_id = models.CharField(max_length=3, null=True, blank=True)
|
||||||
city_name = models.CharField(max_length=255)
|
city_name = models.CharField(max_length=255)
|
||||||
|
|
||||||
created_at = models.DateTimeField(auto_now_add=True)
|
created_at = models.DateTimeField(auto_now_add=True)
|
||||||
updated_at = models.DateTimeField(auto_now=True)
|
updated_at = models.DateTimeField(auto_now=True)
|
||||||
|
|
||||||
def __str__(self) -> str:
|
def __str__(self) -> str:
|
||||||
return f"{self.city_name}"
|
return f"{self.city_local_id}: {self.city_name}"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class Address(models.Model):
|
class Address(models.Model):
|
||||||
|
user = models.ForeignKey(AUTH_USER_MODEL, on_delete=models.CASCADE)
|
||||||
province = models.ForeignKey("Province", on_delete=models.CASCADE, related_name="province")
|
province = models.ForeignKey("Province", on_delete=models.CASCADE, related_name="province")
|
||||||
city = models.ForeignKey("City", on_delete=models.CASCADE, related_name="city")
|
city = models.ForeignKey("City", on_delete=models.CASCADE, related_name="city")
|
||||||
|
postal_code = models.CharField(max_length=15, null=True)
|
||||||
address_detail = models.TextField()
|
address_detail = models.TextField()
|
||||||
|
|
||||||
created_at = models.DateTimeField(auto_now_add=True)
|
created_at = models.DateTimeField(auto_now_add=True)
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,7 @@
|
|||||||
|
from django.urls import path, include
|
||||||
|
|
||||||
|
app_name = "addresses"
|
||||||
|
|
||||||
|
urlpatterns = [
|
||||||
|
path("", include("addresses.api.urls"), name="address-api")
|
||||||
|
]
|
||||||
Binary file not shown.
@@ -63,6 +63,7 @@ INSTALLED_APPS = [
|
|||||||
"drf_spectacular",
|
"drf_spectacular",
|
||||||
"drf_spectacular_sidecar",
|
"drf_spectacular_sidecar",
|
||||||
"corsheaders",
|
"corsheaders",
|
||||||
|
"addresses",
|
||||||
]
|
]
|
||||||
|
|
||||||
MIDDLEWARE = [
|
MIDDLEWARE = [
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ from django.urls import include, path
|
|||||||
from drf_spectacular.views import SpectacularAPIView, SpectacularRedocView, SpectacularSwaggerView
|
from drf_spectacular.views import SpectacularAPIView, SpectacularRedocView, SpectacularSwaggerView
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
|
path("api-auth/", include("rest_framework.urls")),
|
||||||
path("admin/", admin.site.urls),
|
path("admin/", admin.site.urls),
|
||||||
path("api/schema/", SpectacularAPIView.as_view(), name="schema"),
|
path("api/schema/", SpectacularAPIView.as_view(), name="schema"),
|
||||||
path("api/docs/swagger/", SpectacularSwaggerView.as_view(url_name="schema"), name="swagger-ui"),
|
path("api/docs/swagger/", SpectacularSwaggerView.as_view(url_name="schema"), name="swagger-ui"),
|
||||||
@@ -41,4 +42,5 @@ urlpatterns = [
|
|||||||
path("api/farmer-todos/", include("farmer_todos.urls")),
|
path("api/farmer-todos/", include("farmer_todos.urls")),
|
||||||
|
|
||||||
path("api/sensor-external-api/", include("device_hub.sensor_external_api_urls")),
|
path("api/sensor-external-api/", include("device_hub.sensor_external_api_urls")),
|
||||||
|
path("api/address/", include("addresses.urls"))
|
||||||
]
|
]
|
||||||
|
|||||||
Reference in New Issue
Block a user