2017-12-26 17:46:27 +01:00
from django . db import models
from django . utils . translation import ugettext_lazy as _
2018-04-02 04:22:30 +02:00
from django . utils import timezone
2017-12-26 17:46:27 +01:00
from rest_framework import serializers
2018-04-02 04:22:30 +02:00
from . . import location
import pytz
2017-12-26 17:46:27 +01:00
import datetime
# Provides a searchable index of events that may belong to this site or a federated site
class Searchable ( models . Model ) :
2018-03-11 08:13:57 +01:00
event_uri = models . CharField ( primary_key = True , max_length = 256 , null = False , blank = False )
event_url = models . URLField ( null = False , blank = False )
2017-12-26 17:46:27 +01:00
event_title = models . CharField ( max_length = 256 , null = False , blank = False )
2018-02-27 05:03:56 +01:00
img_url = models . URLField ( null = False , blank = False )
2017-12-26 17:46:27 +01:00
location_name = models . CharField ( max_length = 256 , null = False , blank = False )
group_name = models . CharField ( max_length = 256 , null = False , blank = False )
2018-01-27 03:07:07 +01:00
venue_name = models . CharField ( max_length = 256 , null = False , blank = True )
2017-12-26 17:46:27 +01:00
longitude = models . DecimalField ( max_digits = 12 , decimal_places = 8 , null = True , blank = True )
latitude = models . DecimalField ( max_digits = 12 , decimal_places = 8 , null = True , blank = True )
start_time = models . DateTimeField ( )
end_time = models . DateTimeField ( )
2018-04-02 04:22:30 +02:00
tz = models . CharField ( max_length = 32 , verbose_name = _ ( ' Default Timezone ' ) , default = ' UTC ' , choices = location . TimezoneChoices ( ) , blank = False , null = False , help_text = _ ( ' The most commonly used timezone for this Team. ' ) )
2017-12-26 17:46:27 +01:00
cost = models . PositiveSmallIntegerField ( default = 0 , blank = True )
tags = models . CharField ( blank = True , null = True , max_length = 128 )
origin_node = models . URLField ( null = False , blank = False )
federation_node = models . URLField ( null = False , blank = False )
federation_time = models . DateTimeField ( default = datetime . datetime . now )
def __str__ ( self ) :
return u ' %s ' % ( self . event_url )
2018-04-02 04:22:30 +02:00
@property
def local_start_time ( self , val = None ) :
if val is not None :
2018-06-13 23:37:41 +02:00
self . start_time = val . astimezone ( pytz . utc )
2018-04-02 04:22:30 +02:00
else :
if self . start_time is None :
return None
event_tz = pytz . timezone ( self . tz )
return timezone . make_naive ( self . start_time . astimezone ( event_tz ) , event_tz )
@property
def local_end_time ( self , val = None ) :
if val is not None :
2018-06-13 23:37:41 +02:00
self . end_time = val . astimezone ( pytz . utc )
2018-04-02 04:22:30 +02:00
else :
if self . end_time is None :
return None
event_tz = pytz . timezone ( self . tz )
return timezone . make_naive ( self . end_time . astimezone ( event_tz ) , event_tz )
2017-12-26 17:46:27 +01:00
class SearchableSerializer ( serializers . ModelSerializer ) :
class Meta :
model = Searchable
fields = (
2018-03-18 18:08:23 +01:00
' event_uri ' ,
2017-12-26 17:46:27 +01:00
' event_url ' ,
' event_title ' ,
2018-03-18 18:13:50 +01:00
' img_url ' ,
2017-12-26 17:46:27 +01:00
' location_name ' ,
' group_name ' ,
' venue_name ' ,
' longitude ' ,
' latitude ' ,
' start_time ' ,
' end_time ' ,
' cost ' ,
' tags ' ,
' origin_node '
)