Skip to content

Instantly share code, notes, and snippets.

View johnsardine's full-sized avatar

João Sardinha johnsardine

View GitHub Profile
@johnsardine
johnsardine / main-application.js
Created November 16, 2017 11:39
2 - Send data from main to sidebar
const data = {
version: 2,
};
jQuery(document).trigger('updateSidebarData', [data]);
@johnsardine
johnsardine / header.html
Created November 16, 2017 11:21
0 - Including Vue.js
<script src="https://unpkg.com/vue"></script>
@johnsardine
johnsardine / sidebar.html
Last active November 16, 2017 11:22
1 - Connection Example
<div id="sidebar">
Welcome to {{ name }}
</div>
@johnsardine
johnsardine / __init__.py
Created August 21, 2017 16:50
Simple cache bust technique for Flask projects. `/static/main.css` becomes `/static/main.123456789.css`
import re
from flask import Flask
from flask.helpers import send_from_directory
# Subclass Flask and override send_static_file method
class CustomStaticFlask(Flask):
def send_static_file(self, filename):
"""Overrides to filter out cache buster from file path
Input images/file.{hash}.css
@johnsardine
johnsardine / force_external_url_for.py
Created August 3, 2017 09:01
Force Flask url_for to use domain name (absolute url)
# In manage.py add
# Make sure you import: from flask import url_for
@app.context_processor
def override_url_for():
return dict(url_for=external_url_for)
def external_url_for(endpoint, **values):
# Force absolute url in assets
if app.config.get('FORCE_EXTERNAL_URL'):
@johnsardine
johnsardine / middleware.py
Created March 17, 2017 11:42 — forked from thomasyip/middleware.py
Accept Tastypie API Key authentication for regular django app.
class TastypieApiKeyUserMiddleware(object):
"""
Middleware for per-request authentication with tastypie
"""
# Name of request header to grab username from. This will be the key as
# used in the request.META dictionary, i.e. the normalization of headers to
# all uppercase and the addition of "HTTP_" prefix apply.
header = 'HTTP_AUTHORIZATION'
method = 'apikey'
apikey_auth = ApiKeyAuthentication()
@johnsardine
johnsardine / .editorconfig
Last active November 3, 2016 12:20
.editorconfig
# EditorConfig is awesome: http://EditorConfig.org
# top-most EditorConfig file
root = true
# Unix-style newlines with a newline ending every file
[*]
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
import requests, re
problem_name = 'even-or-odd'
problem_url = 'https://www.codewars.com/kata/' + problem_name
users = [ 'fabiopedrosa', 'johnsardine', 'disbeat', 'pgcmarques', 'nnunobm', 'alexp100' ]
problem_id = re.findall('<div class="info-row code-challenge" data-id="(.+?)"', requests.get(problem_url).text)[0]
users = dict( [ (u, []) for u in users ] )
for user in users:
$(document).ready(function(){
// $sections incleudes all of the section divs that relate to menu items.
var $sections = $('.section');
// The user scrolls
$(window).scroll(function(){
// currentScroll is the number of pixels the window has been scrolled
var currentScroll = $(this).scrollTop();
@johnsardine
johnsardine / pattern
Created July 17, 2016 16:34
Regex match Twitter/Instagram mention, hashtag and link
// In RegExr http://regexr.com/3dqqv
/([@][A-z]+)|([#][A-z]+)|((?:(?:https?|ftp):\/\/|www\.)[^\s/$.?#].[^\s]*)/g