added categories

This commit is contained in:
Alexander Karpov 2023-05-24 13:32:17 +03:00
parent 19134f6cd8
commit 2e809f9bc1
5 changed files with 102 additions and 9 deletions

View File

@ -30,7 +30,7 @@
res = {
"title": info["name"],
"parser_source": "mkrf.ru",
"region": Region.objects.get(title=r_name),
# "region": Region.objects.get(title=r_name),
"lat": info["address"]["mapPosition"]["coordinates"][0],
"lon": info["address"]["mapPosition"]["coordinates"][1],
"address": info["address"]["fullAddress"],
@ -39,6 +39,7 @@
}
if "typologies" in info:
res["extra_kwargs"]["typologies"] = [x["value"] for x in info["typologies"]]
if "securityInfo" in info or "borderInfo" in info:
for ev in Event.objects.filter(
title=info["name"],
@ -46,12 +47,7 @@
lat=res["lat"],
lon=res["lon"],
):
ddd = [
info["securityInfo"] if "securityInfo" in info else "",
info["borderInfo"] if "borderInfo" in info else "",
]
ddd = [x for x in ddd if x]
ev.description = " ".join(ddd)
ev.extra_kwargs = res["extra_kwargs"]
ev.save()
ret.append(res)

View File

@ -3,3 +3,6 @@
class EventsConfig(AppConfig):
name = "passfinder.events"
def ready(self):
import passfinder.events.signals # noqa

View File

@ -0,0 +1,66 @@
# Generated by Django 4.2.1 on 2023-05-24 09:40
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
("events", "0018_userroute_baseuserroutepoint_userroutetransaction_and_more"),
]
operations = [
migrations.CreateModel(
name="Category",
fields=[
(
"oid",
models.CharField(
max_length=24, primary_key=True, serialize=False, unique=True
),
),
("name", models.CharField(max_length=500, unique=True)),
("description", models.TextField()),
],
options={
"abstract": False,
},
),
migrations.CreateModel(
name="PointCategory",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
(
"category",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
related_name="points",
to="events.category",
),
),
(
"point",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
related_name="categories",
to="events.basepoint",
),
),
],
options={
"unique_together": {("point", "category")},
},
),
migrations.DeleteModel(
name="Tag",
),
]

View File

@ -73,13 +73,26 @@ def location(self):
return [self.lat, self.lon]
class Tag(OIDModel):
name = models.CharField(max_length=250)
class Category(OIDModel):
name = models.CharField(max_length=500, unique=True)
description = models.TextField()
def __str__(self):
return self.name
class PointCategory(models.Model):
point = models.ForeignKey(
"BasePoint", related_name="categories", on_delete=models.CASCADE
)
category = models.ForeignKey(
"Category", related_name="points", on_delete=models.CASCADE
)
class Meta:
unique_together = ("point", "category")
class BasePoint(OIDModel, PolymorphicModel):
title = models.CharField(max_length=500)
parser_source = models.CharField(max_length=250, null=True, blank=True)

View File

@ -0,0 +1,15 @@
from django.db.models.signals import pre_save
from django.dispatch import receiver
from passfinder.events.models import Category
from passfinder.utils.generators import generate_charset
@receiver(pre_save, sender=Category)
def create_model_link(sender, instance, created, **kwargs):
if instance.id is None:
oid = generate_charset(24)
while Category.objects.filter(oid=oid).exists():
oid = generate_charset(24)
instance.oid = oid