backend/passfinder/recomendations/models.py

125 lines
4.2 KiB
Python
Raw Normal View History

2023-05-27 11:13:41 +03:00
from django.db import models
from passfinder.users.models import User
2023-05-28 16:52:03 +03:00
from passfinder.events.models import Event, Hotel, Restaurant, BasePoint
from passfinder.events.api.serializers import ObjectRouteSerializer
2023-05-27 22:32:52 +03:00
from django.contrib.postgres.fields import ArrayField
2023-05-27 11:13:41 +03:00
class UserPreferences(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
2023-05-28 00:11:44 +03:00
preffered_plays = models.ManyToManyField(Event, related_name="preffered_user_play")
unpreffered_plays = models.ManyToManyField(
Event, related_name="unpreffered_users_play"
)
preffered_movies = models.ManyToManyField(
Event, related_name="preffered_user_movie"
)
unpreffered_movies = models.ManyToManyField(
Event, related_name="unpreffered_user_movie"
)
preferred_concerts = models.ManyToManyField(
Event, related_name="preffered_users_concert"
)
unpreferred_concerts = models.ManyToManyField(
Event, related_name="unpreffered_users_concert"
)
prefferred_attractions = models.ManyToManyField(
Event, related_name="preffered_users_attractions"
)
unprefferred_attractions = models.ManyToManyField(
Event, related_name="unpreffered_users_attractions"
)
prefferred_museums = models.ManyToManyField(
Event, related_name="preffered_users_museums"
)
unprefferred_museums = models.ManyToManyField(
Event, related_name="unpreffered_users_museums"
)
preferred_categories = ArrayField(
base_field=models.CharField(max_length=100), null=True, blank=True
)
preferred_stars = ArrayField(
base_field=models.IntegerField(), null=True, blank=True
)
2023-05-27 11:13:41 +03:00
2023-05-28 16:52:03 +03:00
prefered_other = models.ManyToManyField(Event, related_name='other_users')
prefered_hotels = models.ManyToManyField(Hotel, related_name='hotels_user')
prefered_restaurants = models.ManyToManyField(Restaurant, related_name='restaurant_user')
def add_point(self, point: BasePoint):
print(point, type(point))
if isinstance(point, Event):
event = point
if event.type == 'play':
self.preferred_concerts.add(event)
elif event.type == 'movie':
self.preffered_movies.add(event)
elif event.type == 'attraction':
self.prefferred_attractions.add(event)
elif event.type == 'museum':
self.prefferred_museums.add(event)
else:
self.prefered_other.add(event)
self.save()
if isinstance(point, Hotel):
self.prefered_hotels.add(point)
self.save()
if isinstance(point, Restaurant):
self.prefered_restaurants.add(point)
self.save()
def get_points(self):
points = [
*list(self.preffered_plays.all()),
*list(self.prefered_hotels.all()),
*list(self.prefered_restaurants.all()),
*list(self.preferred_concerts.all()),
*list(self.preffered_movies.all()),
*list(self.prefferred_attractions.all()),
*list(self.prefferred_museums.all()),
*list(self.prefered_other.all()),
]
return list(
map(
lambda x: ObjectRouteSerializer(x).data,
points
)
)
2023-05-27 11:13:41 +03:00
class NearestEvent(models.Model):
2023-05-28 00:11:44 +03:00
event = models.ForeignKey(
Event, on_delete=models.CASCADE, related_name="nearest_model_rel"
)
nearest = models.ManyToManyField(Event, related_name="nearest_model_rev_rel")
2023-05-27 11:13:41 +03:00
class NearestHotel(models.Model):
2023-05-28 00:11:44 +03:00
hotel = models.ForeignKey(
Hotel, on_delete=models.CASCADE, related_name="nearest_hotel_rel"
)
nearest_events = models.ManyToManyField(Event, related_name="nearest_hotel_rev_rel")
2023-05-27 11:13:41 +03:00
class NearestRestaurantToEvent(models.Model):
event = models.ForeignKey(Event, on_delete=models.CASCADE)
restaurants = models.ManyToManyField(Restaurant)
class NearestRestaurantToHotel(models.Model):
hotel = models.ForeignKey(Hotel, on_delete=models.CASCADE)
restaurants = models.ManyToManyField(Restaurant)
class NearestEventToRestaurant(models.Model):
restaurant = models.ForeignKey(Restaurant, on_delete=models.CASCADE)
2023-05-28 00:11:44 +03:00
events = models.ManyToManyField(Event)