added attractions

This commit is contained in:
Alexander Karpov 2023-05-23 18:14:06 +03:00
parent eb319e01e9
commit c3cdcf0509
6 changed files with 124 additions and 7 deletions

File diff suppressed because one or more lines are too long

45
parsers/attractions.py Normal file
View File

@ -0,0 +1,45 @@
import json
from pprint import pprint
from passfinder.events.models import Region
with open("data/pos-attr.json") as f:
data = json.load(f)
reggg = {
"г. Санкт-Петербург": "Санкт-Петербург",
"г. Москва": "Москва",
"г. Севастополь": "Севастополь",
"Республика Адыгея (Адыгея)": "Республика Адыгея",
"Чувашская Республика - Чувашия": "Чувашская Республика",
"Республика Татарстан (Татарстан)": "Республика Татарстан",
"Республика Северная Осетия - Алания": "Республика Северная Осетия Алания",
"Ханты-Мансийский автономный округ - Югра": "Ханты-Мансийский автономный округ — Югра",
}
ret = []
for infff in data:
info = infff["general"]
if info["address"] and "mapPosition" in info["address"]:
r_name = (
reggg[info["region"]["value"]]
if info["region"]["value"] in reggg
else info["region"]["value"]
)
res = {
"title": info["name"],
"parser_source": "mkrf.ru",
"region": Region.objects.get(title=r_name),
"lat": info["address"]["mapPosition"]["coordinates"][0],
"lon": info["address"]["mapPosition"]["coordinates"][1],
"address": info["address"]["fullAddress"],
"type": "attraction",
"extra_kwargs": {"objectType": info["objectType"]["value"]},
}
if "typologies" in info:
res["extra_kwargs"]["typologies"] = [x["value"] for x in info["typologies"]]
ret.append(res)
def get_att():
return ret

View File

@ -1,4 +1,5 @@
from rest_framework import serializers
from rest_framework.generics import get_object_or_404
from passfinder.events.models import Hotel, HotelPhone, City, Event, BasePoint, Region
@ -49,6 +50,14 @@ class RouteSerializer(serializers.Serializer):
points = serializers.ListSerializer(child=PointSerializer())
class RouteInputSerializer(serializers.Serializer):
date_from = serializers.DateField(required=False, allow_null=True)
date_to = serializers.DateField(required=False, allow_null=True)
region = serializers.CharField(
min_length=24, max_length=24, required=False, allow_blank=True
)
class RegionSerializer(serializers.ModelSerializer):
class Meta:
model = Region

View File

@ -1,21 +1,20 @@
from django_filters import DateFilter
from django_filters.rest_framework import DjangoFilterBackend
from rest_framework.generics import GenericAPIView, ListAPIView
from rest_framework.generics import GenericAPIView, ListAPIView, get_object_or_404
from rest_framework.response import Response
from drf_spectacular.utils import extend_schema
from passfinder.events.api.serializers import (
PointSerializer,
RouteSerializer,
RegionSerializer,
RouteInputSerializer,
)
from passfinder.events.models import BasePoint, Region
class BuildRouteApiView(GenericAPIView):
filter_backends = (DjangoFilterBackend,)
filterset_class = DateFilter
serializer_class = RouteSerializer
@extend_schema(responses={200: RouteSerializer(many=True)})
def get(self, request):
routes = []
for _ in range(10):
@ -30,6 +29,39 @@ def get(self, request):
)
return Response(data=routes)
@extend_schema(
request=RouteInputSerializer, responses={200: RouteSerializer(many=True)}
)
def post(self, request):
serializer = RouteInputSerializer(data=request.data)
serializer.is_valid(raise_exception=True)
region = serializer.data["region"]
routes = []
if region:
region = get_object_or_404(Region, oid=region)
for _ in range(2):
routes.append(
{
"name": "bebra",
"description": "bebra bebra bebra",
"points": PointSerializer(many=True).to_representation(
region.points.all().order_by("?")[:10]
),
}
)
else:
for _ in range(10):
routes.append(
{
"name": "bebra",
"description": "bebra bebra bebra",
"points": PointSerializer(many=True).to_representation(
BasePoint.objects.order_by("?")[:10]
),
}
)
return Response(data=routes)
class ListRegionApiView(ListAPIView):
serializer_class = RegionSerializer

View File

@ -0,0 +1,28 @@
# Generated by Django 4.2.1 on 2023-05-23 14:57
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
("events", "0016_remove_basepoint_location_remove_city_location_and_more"),
]
operations = [
migrations.AddField(
model_name="event",
name="address",
field=models.CharField(max_length=250, null=True),
),
migrations.AddField(
model_name="event",
name="extra_kwargs",
field=models.JSONField(null=True),
),
migrations.AlterField(
model_name="basepoint",
name="title",
field=models.CharField(max_length=500),
),
]

View File

@ -81,7 +81,7 @@ def __str__(self):
class BasePoint(OIDModel, PolymorphicModel):
title = models.CharField(max_length=250)
title = models.CharField(max_length=500)
parser_source = models.CharField(max_length=250, null=True, blank=True)
description = models.TextField()
city = models.ForeignKey(
@ -136,6 +136,7 @@ class EventType(models.TextChoices):
concert = "concert", "концерт"
spektatli = "plays", "спектакли"
address = models.CharField(max_length=250, null=True)
payment_method = models.CharField(max_length=250, null=True, blank=True)
type = models.CharField(
db_index=True,
@ -151,6 +152,8 @@ class EventType(models.TextChoices):
ticket_price = models.IntegerField(blank=True, null=True)
schedule = models.JSONField(null=True)
extra_kwargs = models.JSONField(null=True)
class Hotel(BasePoint):
address = models.CharField(max_length=250)