Created
April 21, 2020 13:30
-
-
Save paltman/934ec18eded8cf91c31dbedd07b4e791 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import axios from 'axios'; | |
const { RELEASE_VERSION } = window.AppConfig; | |
const versionHeaders = (config) => { | |
config.headers['X-App-Version'] = SOURCE_VERSION; | |
return config; | |
}; | |
const HTTP = axios.create({ | |
xsrfHeaderName: 'X-CSRFToken', | |
xsrfCookieName: 'csrftoken', | |
}); | |
HTTP.interceptors.request.use(versionHeaders, error => Promise.reject(error)); | |
export default { | |
getBookmarks: (cb) => HTTP.get('bookmarks/').then(response => cb(response.data)), | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
from django.http import JsonResponse | |
from django.views import View | |
from .models import Bookmark | |
class JsonResponseAuthError(JsonResponse): | |
status_code = 401 | |
class JsonResponseInvalidVersionError(JsonResponse): | |
status_code = 418 | |
class APIView(View): | |
def validate_version(self, request): | |
version = request.META.get("HTTP_X_APP_VERSION", "") | |
if version == "": | |
return True | |
return os.environ.get("HEROKU_SLUG_COMMIT", "") == version: | |
@property | |
def user(self): | |
return self.request.user | |
def dispatch(self, request, *args, **kwargs): | |
if self.validate_version(request) is False: | |
return JsonResponseInvalidVersionError(data={"error": "New version available."}) | |
if not request.user.is_authenticated: | |
return JsonResponseAuthError(data={"error": "Authentication required"}) | |
return super().dispatch(request, *args, **kwargs) | |
class Bookmarks(APIView): | |
def get(self, request, *args, **kwargs): | |
data = [b.data() for b in Bookmark.objects.all()] | |
return JSONResponse(data=dict(bookmarks=data)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<template> | |
<div class="version-update" v-if="versionUpdate"> | |
<p>There is a new version!</p> | |
<button @click="getNewVersion">Get Update</button> | |
</div> | |
<div class="bookmark-list" v-else> | |
<button @click="refresh">Refresh</button> | |
<div v-for="(bookmark, index) in bookmarks" :key="index"> | |
<a :href="bookmark.url">{{ bookmark.title }}</a> | |
</div> | |
</div> | |
</template> | |
<script> | |
import api from './api'; | |
export { | |
data() { | |
return { | |
bookmarks: [], | |
versionUpdate: false, | |
} | |
}, | |
methods: { | |
getNewVersion() { | |
window.location.reload(true); | |
}, | |
refresh() { | |
api.getBookmarks(data => { | |
this.bookmarks = data.bookmarks; | |
}).catch(error => { | |
if (error.response && error.response.status === 418) { | |
this.versionUpdate = true; | |
} else { | |
throw error; | |
} | |
}); | |
} | |
}, | |
created() { | |
this.refresh(); | |
} | |
} | |
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<!-- Put this in the head of your base template --> | |
<script> | |
window.AppConfig = { | |
SOURCE_VERSION: '{{ SOURCE_VERSION }}', | |
} | |
</script> | |
</head> | |
<body>...</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# If you are deployed on Heroku you can get the unique version | |
# of what's deployed from the environment | |
# | |
# https://devcenter.heroku.com/articles/dyno-metadata | |
def settings(request): | |
return dict( | |
SOURCE_VERSION=os.environ.get("HEROKU_SLUG_COMMIT", "") | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment