2017-12-26 17:46:27 +01:00
from django . db import models
from django . utils . translation import ugettext_lazy as _
2018-01-20 20:09:57 +01:00
from rest_framework import serializers
2018-01-04 05:44:27 +01:00
import pytz
2017-12-26 17:46:27 +01:00
class Language ( models . Model ) :
class Meta :
ordering = ( ' name ' , )
name = models . CharField ( _ ( " Language " ) , max_length = 150 , null = True )
code = models . CharField ( _ ( " Language Code " ) , max_length = 20 , null = True )
def __str__ ( self ) :
return u ' %s ' % ( self . name )
class Continent ( models . Model ) :
name = models . CharField ( _ ( " Name " ) , max_length = 50 )
class Meta :
ordering = ( ' name ' , )
def __str__ ( self ) :
return u ' %s ' % ( self . name )
class Country ( models . Model ) :
name = models . CharField ( _ ( " Name " ) , max_length = 100 )
code = models . CharField ( _ ( " Country Code " ) , max_length = 8 )
continents = models . ManyToManyField ( Continent )
class Meta :
ordering = ( ' name ' , )
2018-04-21 16:28:04 +02:00
verbose_name_plural = ' Countries '
2017-12-26 17:46:27 +01:00
def __str__ ( self ) :
return u ' %s ' % ( self . name )
@property
def slug ( self ) :
if self . name is not None :
return self . name . replace ( ' , ' , ' ' ) . replace ( ' ' , ' _ ' )
else :
return ' no_country '
2018-01-20 20:09:57 +01:00
class CountrySerializer ( serializers . ModelSerializer ) :
2018-03-25 20:40:29 +02:00
display = serializers . CharField ( source = ' __str__ ' , read_only = True )
2018-01-20 20:09:57 +01:00
class Meta :
model = Country
fields = (
' id ' ,
' name ' ,
2018-03-25 20:40:29 +02:00
' code ' ,
' display ' ,
2018-01-20 20:09:57 +01:00
)
2017-12-26 17:46:27 +01:00
class SPR ( models . Model ) :
name = models . CharField ( _ ( " Name " ) , max_length = 100 )
2018-01-04 05:44:27 +01:00
code = models . CharField ( _ ( " Admin Code " ) , max_length = 8 )
2017-12-26 17:46:27 +01:00
country = models . ForeignKey ( Country , on_delete = models . CASCADE )
class Meta :
ordering = ( ' name ' , )
def __str__ ( self ) :
return u ' %s , %s ' % ( self . name , self . country . name )
@property
def slug ( self ) :
if self . name is not None :
return self . name . replace ( ' , ' , ' ' ) . replace ( ' ' , ' _ ' )
else :
return ' no_spr '
2018-01-20 20:09:57 +01:00
class SPRSerializer ( serializers . ModelSerializer ) :
2018-01-21 20:01:00 +01:00
display = serializers . CharField ( source = ' __str__ ' , read_only = True )
2018-01-20 20:09:57 +01:00
class Meta :
model = SPR
fields = (
' id ' ,
' name ' ,
' code ' ,
' country ' ,
2018-01-21 20:01:00 +01:00
' slug ' ,
' display '
2018-01-20 20:09:57 +01:00
)
2017-12-26 17:46:27 +01:00
class City ( models . Model ) :
class Meta :
ordering = ( ' name ' , )
verbose_name_plural = _ ( " Cities " )
name = models . CharField ( _ ( " Name " ) , max_length = 100 )
spr = models . ForeignKey ( SPR , on_delete = models . CASCADE )
2018-01-04 05:44:27 +01:00
tz = models . CharField ( max_length = 32 , verbose_name = _ ( ' Default Timezone ' ) , default = ' UTC ' , choices = [ ( tz , tz ) for tz in pytz . all_timezones ] , blank = False , null = False , help_text = _ ( ' The most commonly used timezone for this Team. ' ) )
2018-01-25 22:41:12 +01:00
longitude = models . FloatField ( help_text = _ ( ' Longitude in Degrees East ' ) , null = True , blank = True )
latitude = models . FloatField ( help_text = _ ( ' Latitude in Degrees North ' ) , null = True , blank = True )
2018-09-27 23:42:23 +02:00
population = models . IntegerField ( help_text = _ ( ' Population ' ) , null = False , blank = False , default = 0 )
2017-12-26 17:46:27 +01:00
2018-03-25 20:40:29 +02:00
@property
def short_name ( self ) :
if self . spr . country . name == ' United States ' :
return u ' %s , %s ' % ( self . name , self . spr . name )
else :
return u ' %s , %s ' % ( self . name , self . spr . country . name )
2017-12-26 17:46:27 +01:00
def __str__ ( self ) :
return u ' %s , %s , %s ' % ( self . name , self . spr . name , self . spr . country . name )
@property
def slug ( self ) :
if self . name is not None :
return self . name . replace ( ' , ' , ' ' ) . replace ( ' ' , ' _ ' )
else :
return ' no_city '
2018-01-20 20:09:57 +01:00
class CitySerializer ( serializers . ModelSerializer ) :
2018-01-21 20:01:00 +01:00
display = serializers . CharField ( source = ' __str__ ' , read_only = True )
2018-01-20 20:09:57 +01:00
class Meta :
model = City
fields = (
' id ' ,
' name ' ,
2018-03-25 20:40:29 +02:00
' short_name ' ,
2018-01-20 20:09:57 +01:00
' spr ' ,
' tz ' ,
2018-09-27 23:42:23 +02:00
' latitude ' ,
' longitude ' ,
2018-01-21 20:01:00 +01:00
' slug ' ,
' display '
2018-01-20 20:09:57 +01:00
)
2017-12-26 17:46:27 +01:00