Skip to content

Commit

Permalink
Merge pull request #6 from chnm/feature/model-revision
Browse files Browse the repository at this point in the history
Improved importing for subjects, places, and textiles
  • Loading branch information
hepplerj committed Jul 11, 2024
2 parents 2494864 + 9ac6977 commit 03e5669
Show file tree
Hide file tree
Showing 14 changed files with 439 additions and 90 deletions.
1 change: 1 addition & 0 deletions config/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
"django.middleware.clickjacking.XFrameOptionsMiddleware",
"wagtail.contrib.redirects.middleware.RedirectMiddleware",
"allauth.account.middleware.AccountMiddleware",
"django.middleware.locale.LocaleMiddleware",
]

ROOT_URLCONF = "config.urls"
Expand Down
13 changes: 7 additions & 6 deletions material/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@
)


class TextileInline(admin.TabularInline):
model = TextileType


class NamedActorsInline(admin.TabularInline):
model = NamedActor

Expand Down Expand Up @@ -55,7 +51,7 @@ class AreaAdmin(admin.ModelAdmin):
@admin.register(Place)
class PlaceAdmin(admin.ModelAdmin):
resource_class = PlaceResource
list_display = ["city", "country", "area"]
list_display = ["id", "city", "country", "area", "latitude", "longitude"]
inlines = [PlacesAliasInline]


Expand All @@ -78,10 +74,15 @@ class TextileRecordAdmin(ImportExportModelAdmin):
list_display = [
"id",
"year",
"price",
"currency",
"record_creator",
"source_reference",
]
search_fields = ["transcription", "summary_of_record"]
list_filter = ["year", "primary_subjects", "secondary_subjects"]
inlines = [TextileInline, NamedActorsInline, ImagesInline, ArchivalRecordInline]
inlines = [NamedActorsInline, ImagesInline, ArchivalRecordInline]
ordering = ["year"]


@admin.register(Image)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generated by Django 5.0.6 on 2024-07-10 13:02

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("material", "0021_alter_textilerecord_price"),
]

operations = [
migrations.RemoveField(
model_name="textiletype",
name="textile_record",
),
migrations.AddField(
model_name="textilerecord",
name="textile_types",
field=models.ManyToManyField(
blank=True, related_name="textile_records", to="material.textiletype"
),
),
]
17 changes: 17 additions & 0 deletions material/migrations/0023_alter_area_name.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 5.0.6 on 2024-07-10 13:30

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("material", "0022_remove_textiletype_textile_record_and_more"),
]

operations = [
migrations.AlterField(
model_name="area",
name="name",
field=models.CharField(blank=True, max_length=50, null=True, unique=True),
),
]
17 changes: 17 additions & 0 deletions material/migrations/0024_alter_subject_name.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 5.0.6 on 2024-07-10 13:42

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("material", "0023_alter_area_name"),
]

operations = [
migrations.AlterField(
model_name="subject",
name="name",
field=models.CharField(blank=True, max_length=350, null=True, unique=True),
),
]
17 changes: 17 additions & 0 deletions material/migrations/0025_alter_subject_name.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 5.0.6 on 2024-07-10 13:46

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("material", "0024_alter_subject_name"),
]

operations = [
migrations.AlterField(
model_name="subject",
name="name",
field=models.CharField(max_length=350, null=True, unique=True),
),
]
17 changes: 17 additions & 0 deletions material/migrations/0026_alter_place_country.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 5.0.6 on 2024-07-10 15:18

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("material", "0025_alter_subject_name"),
]

operations = [
migrations.AlterField(
model_name="place",
name="country",
field=models.CharField(blank=True, max_length=765, null=True),
),
]
17 changes: 17 additions & 0 deletions material/migrations/0027_alter_subject_name.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 5.0.6 on 2024-07-10 16:34

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("material", "0026_alter_place_country"),
]

operations = [
migrations.AlterField(
model_name="subject",
name="name",
field=models.CharField(blank=True, max_length=350, null=True, unique=True),
),
]
17 changes: 17 additions & 0 deletions material/migrations/0028_alter_area_name.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 5.0.6 on 2024-07-10 16:44

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("material", "0027_alter_subject_name"),
]

operations = [
migrations.AlterField(
model_name="area",
name="name",
field=models.CharField(blank=True, max_length=255, null=True, unique=True),
),
]
22 changes: 22 additions & 0 deletions material/migrations/0029_place_latitude_place_longitude.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Generated by Django 5.0.6 on 2024-07-11 13:52

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("material", "0028_alter_area_name"),
]

operations = [
migrations.AddField(
model_name="place",
name="latitude",
field=models.FloatField(blank=True, null=True),
),
migrations.AddField(
model_name="place",
name="longitude",
field=models.FloatField(blank=True, null=True),
),
]
52 changes: 43 additions & 9 deletions material/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,54 @@

class Area(models.Model):
id = models.AutoField(primary_key=True)
name = models.CharField(max_length=50, unique=True)
name = models.CharField(max_length=255, unique=True, blank=True, null=True)

def __str__(self) -> str:
return str(self.name)
if self.name is not None:
return str(self.name)
else:
return "No area name provided"


class Place(models.Model):
id = models.AutoField(primary_key=True)
city = models.CharField(max_length=765, blank=True, null=True, unique=True)
country = models.CharField(max_length=765, blank=True, null=True, unique=True)
country = models.CharField(max_length=765, blank=True, null=True)
area = models.ForeignKey(
Area, on_delete=models.CASCADE, related_name="places", blank=True, null=True
)
latitude = models.FloatField(blank=True, null=True)
longitude = models.FloatField(blank=True, null=True)

def __str__(self) -> str:
return f"{self.city}, {self.country}"
if self.country is None:
return str(self.city)
elif self.city is None and self.country is None:
return "No place name provided"
else:
return f"{self.city}, {self.country}"

# Automatically derive the latitude/longitude from the available data.
def save(self, *args, **kwargs):
if self.city is not None and self.country is not None:
from geopy.geocoders import Nominatim

geolocator = Nominatim(user_agent="textiles")
location = geolocator.geocode(f"{self.city}, {self.country}")
if location is not None:
self.latitude = location.latitude
self.longitude = location.longitude
elif self.country is not None:
from geopy.geocoders import Nominatim

geolocator = Nominatim(user_agent="textiles")
location = geolocator.geocode(self.country)
if location is not None:
self.latitude = location.latitude
self.longitude = location.longitude
else:
pass
super(Place, self).save(*args, **kwargs)

class Meta:
unique_together = (
Expand All @@ -30,7 +62,7 @@ class Meta:

class Subject(models.Model):
id = models.AutoField(primary_key=True)
name = models.CharField(max_length=350, unique=True)
name = models.CharField(max_length=350, unique=True, null=True, blank=True)

def __str__(self) -> str:
return str(self.name)
Expand All @@ -48,6 +80,11 @@ class TextileRecord(models.Model):

id = models.AutoField(primary_key=True)
year = models.IntegerField(blank=True, null=True)
textile_types = models.ManyToManyField(
"TextileType",
related_name="textile_records",
blank=True,
)
textile_specifications = models.CharField(blank=True, null=True, max_length=255)
circulation = models.CharField(
blank=True, null=True, choices=CIRCULATION_CHOICES, max_length=2
Expand Down Expand Up @@ -105,7 +142,7 @@ class TextileRecord(models.Model):
updated_at = models.DateTimeField(auto_now=True)

def __str__(self) -> str:
return f"{self.year} - {self.associated_textile}"
return f"{self.year}"


class TextileType(models.Model):
Expand All @@ -116,9 +153,6 @@ class TextileType(models.Model):
]

id = models.AutoField(primary_key=True)
textile_record = models.ForeignKey(
TextileRecord, on_delete=models.CASCADE, related_name="textile_type"
)
textile_type_selection = models.CharField(
blank=True, null=True, choices=TYPE_CHOICES, max_length=2
)
Expand Down
Loading

0 comments on commit 03e5669

Please sign in to comment.