44from django .db import models
55from django .utils import timezone
66
7+ from apps .common .constants import NL
8+ from apps .common .geocoding import get_location_coordinates
79from apps .common .models import BulkSaveModel , TimestampedModel
8- from apps .common .utils import slugify
10+ from apps .common .open_ai import OpenAi
11+ from apps .common .utils import join_values , slugify
912from apps .github .utils import normalize_url
1013
1114
@@ -30,13 +33,18 @@ class Category(models.TextChoices):
3033 choices = Category .choices ,
3134 default = Category .OTHER ,
3235 )
33-
34- end_date = models .DateField (verbose_name = "End Date" , null = True , blank = True )
35- key = models .CharField (verbose_name = "Key" , max_length = 100 , unique = True )
3636 name = models .CharField (verbose_name = "Name" , max_length = 100 )
37- description = models .TextField (verbose_name = "Description" , default = "" , blank = True )
3837 start_date = models .DateField (verbose_name = "Start Date" )
38+ end_date = models .DateField (verbose_name = "End Date" , null = True , blank = True )
39+ description = models .TextField (verbose_name = "Description" , default = "" , blank = True )
40+ key = models .CharField (verbose_name = "Key" , max_length = 100 , unique = True )
41+ summary = models .TextField (verbose_name = "Summary" , blank = True , default = "" )
42+ suggested_location = models .CharField (
43+ verbose_name = "Suggested Location" , max_length = 255 , blank = True , default = ""
44+ )
3945 url = models .URLField (verbose_name = "URL" , default = "" , blank = True )
46+ latitude = models .FloatField (verbose_name = "Latitude" , null = True , blank = True )
47+ longitude = models .FloatField (verbose_name = "Longitude" , null = True , blank = True )
4048
4149 def __str__ (self ):
4250 """Event human readable representation."""
@@ -130,3 +138,53 @@ def from_dict(self, category, data):
130138
131139 for key , value in fields .items ():
132140 setattr (self , key , value )
141+
142+ def generate_geo_location (self ):
143+ """Add latitude and longitude data."""
144+ location = None
145+ if self .suggested_location and self .suggested_location != "None" :
146+ location = get_location_coordinates (self .suggested_location )
147+ if location is None :
148+ location = get_location_coordinates (self .get_context ())
149+ if location :
150+ self .latitude = location .latitude
151+ self .longitude = location .longitude
152+
153+ def generate_suggested_location (self , prompt ):
154+ """Generate a suggested location for the event."""
155+ open_ai = OpenAi ()
156+ open_ai .set_input (self .get_context ())
157+ open_ai .set_max_tokens (100 ).set_prompt (prompt )
158+ try :
159+ suggested_location = open_ai .complete ()
160+ self .suggested_location = (
161+ suggested_location if suggested_location and suggested_location != "None" else ""
162+ )
163+ except (ValueError , TypeError ):
164+ self .suggested_location = ""
165+
166+ def generate_summary (self , prompt ):
167+ """Generate a summary for the event."""
168+ open_ai = OpenAi ()
169+ open_ai .set_input (self .get_context (include_dates = True ))
170+ open_ai .set_max_tokens (100 ).set_prompt (prompt )
171+ try :
172+ summary = open_ai .complete ()
173+ self .summary = summary if summary and summary != "None" else ""
174+ except (ValueError , TypeError ):
175+ self .summary = ""
176+
177+ def get_context (self , include_dates = False ):
178+ """Return geo string."""
179+ context = [
180+ f"Name: { self .name } " ,
181+ f"Description: { self .description } " ,
182+ f"Summary: { self .summary } " ,
183+ ]
184+ if include_dates :
185+ context .append (f"Dates: { self .start_date } - { self .end_date } " )
186+
187+ return join_values (
188+ context ,
189+ delimiter = NL ,
190+ )
0 commit comments