Skip to content

Commit e820282

Browse files
committed
Merge branch 'opengraphmeta' into stage
2 parents 2e3d7e3 + 2dab8e0 commit e820282

File tree

5 files changed

+105
-17
lines changed

5 files changed

+105
-17
lines changed

django/gsmap/schema.py

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from graphene_django.types import DjangoObjectType
99
from graphene_django.filter import DjangoFilterConnectionField
1010
from graphene_django.converter import convert_django_field
11-
# from sorl.thumbnail import get_thumbnail
11+
from sorl.thumbnail import get_thumbnail
1212
from gsmap.models import Municipality, Snapshot, SnapshotPermission, Workspace
1313

1414

@@ -45,15 +45,20 @@ class SnapshotNode(DjangoObjectType):
4545
class Meta:
4646
model = Snapshot
4747
fields = [
48-
'is_showcase', 'title', 'topic', 'data', 'municipality', 'predecessor'
48+
'is_showcase', 'title', 'topic', 'data', 'municipality',
49+
'predecessor'
50+
]
51+
filter_fields = [
52+
'municipality__id', 'municipality__canton', 'is_showcase'
4953
]
50-
filter_fields = ['municipality__id', 'municipality__canton', 'is_showcase']
5154
interfaces = [graphene.relay.Node]
5255

5356
data = generic.GenericScalar(source='data')
5457
pk = graphene.String(source='id')
5558
thumbnail = graphene.String()
5659
screenshot = graphene.String()
60+
screenshot_facebook = graphene.String()
61+
screenshot_twitter = graphene.String()
5762

5863
@classmethod
5964
def get_queryset(cls, queryset, info):
@@ -65,6 +70,20 @@ def resolve_screenshot(self, info):
6570
def resolve_thumbnail(self, info):
6671
return self.thumbnail
6772

73+
def resolve_screenshot_facebook(self, info):
74+
screenshot = get_thumbnail(self.screenshot,
75+
'1200x630',
76+
crop='bottom',
77+
format='PNG')
78+
return screenshot
79+
80+
def resolve_screenshot_twitter(self, info):
81+
screenshot = get_thumbnail(self.screenshot,
82+
'1200x600',
83+
crop='bottom',
84+
format='PNG')
85+
return screenshot
86+
6887

6988
class MunicipalityNode(DjangoObjectType):
7089
class Meta:
@@ -83,7 +102,8 @@ class Meta:
83102
perimeter_bounds = graphene.List(graphene.Float)
84103

85104
def resolve_snapshots(self, info):
86-
return Snapshot.objects.filter(Q_SNAPSHOT_ONLY_PUBLIC & Q(municipality__id=self.pk))
105+
return Snapshot.objects.filter(Q_SNAPSHOT_ONLY_PUBLIC
106+
& Q(municipality__id=self.pk))
87107

88108
def resolve_perimeter_centroid(self, info):
89109
return self.perimeter.centroid
@@ -95,9 +115,7 @@ def resolve_perimeter_bounds(self, info):
95115
class WorkspaceNode(DjangoObjectType):
96116
class Meta:
97117
model = Workspace
98-
fields = [
99-
'title', 'description'
100-
]
118+
fields = ['title', 'description']
101119
interfaces = [graphene.relay.Node]
102120

103121
pk = graphene.String(source='id')
@@ -113,7 +131,6 @@ class Query(object):
113131

114132
snapshot = graphene.relay.Node.Field(SnapshotNode)
115133
snapshots = DjangoFilterConnectionField(
116-
SnapshotNode, filterset_class=SnapshotOnlyPublicFilter
117-
)
134+
SnapshotNode, filterset_class=SnapshotOnlyPublicFilter)
118135

119136
workspace = graphene.relay.Node.Field(WorkspaceNode)

django/main/settings.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,16 +90,20 @@
9090

9191
AUTH_PASSWORD_VALIDATORS = [
9292
{
93-
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
93+
'NAME':
94+
'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
9495
},
9596
{
96-
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
97+
'NAME':
98+
'django.contrib.auth.password_validation.MinimumLengthValidator',
9799
},
98100
{
99-
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
101+
'NAME':
102+
'django.contrib.auth.password_validation.CommonPasswordValidator',
100103
},
101104
{
102-
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
105+
'NAME':
106+
'django.contrib.auth.password_validation.NumericPasswordValidator',
103107
},
104108
]
105109

@@ -111,7 +115,8 @@
111115

112116
FORMAT_MODULE_PATH = 'main.formats'
113117

114-
STATIC_ROOT = os.environ.get('DJANGO_STATIC_DIR', '/var/services/django/static')
118+
STATIC_ROOT = os.environ.get('DJANGO_STATIC_DIR',
119+
'/var/services/django/static')
115120
STATIC_URL = '/static/'
116121

117122
MEDIA_ROOT = os.environ.get('DJANGO_MEDIA_DIR', '/var/services/django/media')
@@ -122,6 +127,14 @@
122127

123128
CORS_ORIGIN_ALLOW_ALL = False
124129
CORS_ORIGIN_WHITELIST = [
125-
"http://localhost:8080", "http://localhost:8081",
130+
"http://localhost:8080",
131+
"http://localhost:8081",
126132
]
127133
CORS_ALLOW_CREDENTIALS = True
134+
CACHES = {
135+
'default': {
136+
'BACKEND': 'django.core.cache.backends.db.DatabaseCache',
137+
'LOCATION': 'django_cache',
138+
}
139+
}
140+
THUMBNAIL_BACKEND = 'main.utils.PermalinkThumbnailBackend'

django/main/utils.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
from sorl.thumbnail.base import ThumbnailBackend, EXTENSIONS
3+
from sorl.thumbnail.conf import settings
4+
from sorl.thumbnail.helpers import tokey, serialize
5+
import os.path
6+
7+
class PermalinkThumbnailBackend(ThumbnailBackend):
8+
def _get_thumbnail_filename(self, source, geometry_string, options):
9+
"""
10+
Computes the destination filename.
11+
"""
12+
key = tokey(source.key, geometry_string, serialize(options))
13+
filename, _ext = os.path.splitext(os.path.basename(source.name))
14+
path = '%s/%s' % (key, filename)
15+
return '%s%s.%s' % (settings.THUMBNAIL_PREFIX, path, EXTENSIONS[options['format']])

vue/src/components/SnapshotMap.vue

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,8 @@ export default {
121121
122122
props: {
123123
geojson: Object,
124+
screenshotFacebook: String,
125+
screenshotTwitter: String,
124126
geoboundsIn: Array,
125127
predecessor: Object
126128
},
@@ -175,6 +177,7 @@ export default {
175177
this.description = this.geojson.views[0].spec.description;
176178
this.legend = this.geojson.views[0].spec.legend;
177179
this.sources = this.geojson.sources;
180+
this.setMetaShare();
178181
},
179182
180183
setupMapbox() {
@@ -262,6 +265,40 @@ export default {
262265
this.layers = [];
263266
this.geobounds = [];
264267
this.map = null;
268+
this.removeMetaTags();
269+
},
270+
271+
addMetaTag(property, content) {
272+
const metaTag = document.createElement('meta');
273+
metaTag.setAttribute('property', property);
274+
metaTag.content = content;
275+
document.getElementsByTagName('head')[0].appendChild(metaTag);
276+
},
277+
278+
removeMetaTags() {
279+
const ogTitle = document.querySelector('[name=\'og:title\']');
280+
const ogDescription = document.querySelector('[name=\'og:description\']');
281+
const ogUrl = document.querySelector('[name=\'og:url\']');
282+
const ogImage = document.querySelector('[name=\'og:image\']');
283+
const twitterImage = document.querySelector('[name=\'twitter:image\']');
284+
if (ogTitle) ogTitle.remove();
285+
if (ogDescription) ogDescription.remove();
286+
if (ogUrl) ogUrl.remove();
287+
if (ogImage) ogImage.remove();
288+
if (twitterImage) twitterImage.remove();
289+
},
290+
291+
setMetaShare() {
292+
this.removeMetaTags();
293+
this.addMetaTag('og:title', this.title);
294+
this.addMetaTag('og:description', this.description);
295+
this.addMetaTag('og:url', `${document.location.origin}/${this.hash}`);
296+
if (this.screenshotFacebook) {
297+
this.addMetaTag('og:image', `${document.location.origin}/media/${this.screenshotFacebook}`);
298+
}
299+
if (this.screenshotTwitter) {
300+
this.addMetaTag('twitter:image', `${document.location.origin}/media/${this.screenshotTwitter}`);
301+
}
265302
}
266303
}
267304
};

vue/src/views/Snapshot.vue

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@
9494
:geojson="geojson"
9595
:geoboundsIn="geobounds"
9696
:predecessor="predecessor"
97+
:screenshotFacebook="screenshotFacebook"
98+
:screenshotTwitter="screenshotTwitter"
9799
/>
98100
</div>
99101
</template>
@@ -128,6 +130,8 @@ export default {
128130
return {
129131
hash: this.$route.params.hash,
130132
bfsNumber: this.$route.params.bfsNumber,
133+
screenshotFacebook: '',
134+
screenshotTwitter: '',
131135
geojson: null,
132136
geobounds: [],
133137
municipalityName: '',
@@ -196,6 +200,8 @@ export default {
196200
id
197201
pk
198202
}
203+
screenshotFacebook
204+
screenshotTwitter
199205
municipality {
200206
bfsNumber
201207
fullname
@@ -205,7 +211,6 @@ export default {
205211
title
206212
topic
207213
thumbnail
208-
screenshot
209214
}
210215
}
211216
}
@@ -236,6 +241,8 @@ export default {
236241
snapshot => !snapshotsIdExamplesExclude.includes(snapshot.id)
237242
);
238243
this.predecessor = (result.data.snapshot.predecessor);
244+
this.screenshotFacebook = result.data.snapshot.screenshotFacebook;
245+
this.screenshotTwitter = (result.data.snapshot.screenshotTwitter);
239246
this.$store.commit('setBfsnumber', result.data.snapshot.municipality.bfsNumber);
240247
this.$store.commit('setBfsname', result.data.snapshot.municipality.fullname);
241248
} else {
@@ -258,7 +265,6 @@ export default {
258265
title
259266
topic
260267
thumbnail
261-
screenshot
262268
}
263269
}
264270

0 commit comments

Comments
 (0)