22import string
33from enum import IntFlag
44import requests
5+ import os
56from django .utils import timezone
67from django .db .models .signals import post_save
78from django .dispatch import receiver
1112from django .contrib .sites .models import Site
1213from django .utils .html import format_html
1314from django .conf import settings
15+ from django .core .files .base import ContentFile
16+ from django .core .files .storage import FileSystemStorage
1417from sortedm2m .fields import SortedManyToManyField
15- from sorl .thumbnail import ImageField
18+ from sorl .thumbnail import ImageField , get_thumbnail
1619from gsuser .models import User
1720
1821
22+ class OverwriteStorage (FileSystemStorage ):
23+ def get_available_name (self , name , max_length = None ):
24+ # If the filename already exists, remove it as if it was a true file system
25+ if self .exists (name ):
26+ os .remove (os .path .join (settings .MEDIA_ROOT , name ))
27+ return name
28+
29+
1930class Municipality (models .Model ):
2031 class Meta :
2132 verbose_name_plural = 'municipalities'
@@ -146,8 +157,9 @@ def thumbnail(self):
146157
147158 def get_absolute_link (self ):
148159 domain = Site .objects .get_current ().domain
160+ proto = 'https' if settings .USE_HTTPS else 'http'
149161 return format_html (
150- f'<a href="//{ domain } { self .get_absolute_url ()} " target="_blank">'
162+ f'<a href="{ proto } : //{ domain } { self .get_absolute_url ()} " target="_blank">'
151163 f'{ domain } { self .get_absolute_url ()} </a>'
152164 )
153165 get_absolute_link .short_description = "Snapshot Url"
@@ -183,6 +195,18 @@ def test_exists(pk):
183195
184196 super ().save (* args , ** kwargs )
185197
198+ def image_twitter (self ):
199+ return get_thumbnail (
200+ self .screenshot , '1200x630' ,
201+ crop = 'bottom' , format = 'PNG'
202+ )
203+
204+ def image_facebook (self ):
205+ return get_thumbnail (
206+ self .screenshot , '1200x630' ,
207+ crop = 'bottom' , format = 'PNG'
208+ )
209+
186210 def __str__ (self ):
187211 if self .municipality :
188212 return f'{ self .municipality .fullname } , { self .title } , ' \
@@ -193,9 +217,10 @@ def __str__(self):
193217
194218@receiver (post_save , sender = Snapshot )
195219def save_screenshot_handler (sender , ** kwargs ):
220+ instance = kwargs .get ('instance' )
221+
196222 def save_screenshot ():
197223 post_save .disconnect (save_screenshot_handler , sender = Snapshot )
198- instance = kwargs .get ('instance' )
199224 # only create snapshot if data changed
200225 if instance .data_changed ([
201226 'data' , 'screenshot_generated' , 'thumbnail_generated'
@@ -213,9 +238,27 @@ def save_screenshot():
213238 finally :
214239 # always reconnect signal
215240 post_save .connect (save_screenshot_handler , sender = Snapshot )
241+
242+ def save_meta (storage ):
243+ domain = Site .objects .get_current ().domain
244+ proto = 'https' if settings .USE_HTTPS else 'http'
245+ meta = f'''name="og:title" content="{ instance .title } "
246+ name="og:url" content="{ instance .get_absolute_link ()} "
247+ name="og:image" content="{ proto } ://{ domain } /{ instance .image_facebook ()} "
248+ name="twitter:image" content="{ proto } ://{ domain } /{ instance .image_twitter ()} "
249+ '''
250+ storage .save (f'snapshot-meta/{ instance .id } .html' , ContentFile (meta ))
251+
216252 if hasattr (settings , 'SAVE_SCREENSHOT_ENABLED' ) and settings .SAVE_SCREENSHOT_ENABLED is True :
217253 save_screenshot ()
218254
255+ if instance .screenshot :
256+ storage = OverwriteStorage ()
257+ if instance .permission is int (SnapshotPermission .PUBLIC ):
258+ save_meta (storage )
259+ else :
260+ storage .delete (f'snapshot-meta/{ instance .id } .html' )
261+
219262
220263class Workspace (models .Model ):
221264 class Meta :
@@ -234,9 +277,10 @@ class Meta:
234277 snapshots = SortedManyToManyField (Snapshot )
235278
236279 def get_absolute_link (self ):
280+ proto = 'https' if settings .USE_HTTPS else 'http'
237281 domain = Site .objects .get_current ().domain
238282 return format_html (
239- f'<a href="//{ domain } { self .get_absolute_url ()} " target="_blank">'
283+ f'<a href="{ proto } : //{ domain } { self .get_absolute_url ()} " target="_blank">'
240284 f'{ domain } { self .get_absolute_url ()} </a>'
241285 )
242286 get_absolute_link .short_description = "Workspace Url"
@@ -245,7 +289,6 @@ def get_absolute_url(self):
245289 first_id = self .snapshots .all ().first ().id
246290 return f'/{ self .id } /{ first_id } /'
247291
248-
249292 def save (self , * args , ** kwargs ):
250293 def test_exists (pk ):
251294 if self .__class__ .objects .filter (pk = pk ):
0 commit comments