Skip to content

Instantly share code, notes, and snippets.

@ncole458
ncole458 / APIView
Created September 24, 2015 06:21
Django REST Framework POST JSON & upload file based on request.data
# FULL WORKING POST FOR JSON & FILE
def post(self, request, format=None):
up_file = request.data.get('file')
serializer = ridesSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
if up_file is not None:
if socket.getfqdn() == 'STG' or socket.getfqdn() == 'PRD':
destination = open(PRD_UPLOAD + up_file.name, 'wb+')
else:
"""
Django REST Framework filtering by comma (used with Ember.js).
Change from the standard ?var=1&var=2 etc.
"""
# GET querystring i.e. ?var=1,2,4,6
get_my_var = self.request.query_params.get('var', None)
if get_my_var:
my_var = get_my_var.split(',')
else:
import csv
reader=csv.reader(open('UserstoImport.csv', 'r'), delimiter=',')
writer=csv.writer(open('UserstoImport_withoutduplicates.csv', 'w'), delimiter=',')
entries = set()
for row in reader:
key = (row[1], row[2]) # CSV rows by num
@ncole458
ncole458 / _native.scss
Last active January 4, 2016 05:17
Native UI tweaks for Cordova/PhoneGap apps
/*********************************************************************************
Author: Nicholas Cole
Author URI: http://oceanbluecreative.com.au
License: MIT
Usage: Sass Partial for native UI tweaks in Cordova/PhoneGap apps
Gist: https://gist.github.com/ncole458/49bb6b0309572dc429ea
**********************************************************************************/
/** Preload Images (plays nicely with Cordova/PhoneGap i.e. stops images 'popping' in!) **/
body {
color: $fontColor;
background: url('../images/bg.jpg') no-repeat -9999px -9999px,
url('../images/bg1.png') no-repeat -9999px -9999px,
url('../images/bg2.png') no-repeat -9999px -9999px,
url('../images/bg3.png') no-repeat -9999px -9999px,
url('../images/introOne@3.png') no-repeat -9999px -9999px,
url('../images/introTwo@3.png') no-repeat -9999px -9999px,
@ncole458
ncole458 / views.html
Last active January 23, 2016 06:02
Kendo UI data-title change (using onclick over data-click)
<div data-role="view" data-title="View One" id="view-one">
<header data-role="header">
<div data-role="navbar">
<span data-role="view-title" id="one-title"></span>
</div>
</header>
<a data-role="button" data-mytitle="username 1" onclick="viewtwo(this)">go to view username 1</a>
<a data-role="button" data-mytitle="username 2" onclick="viewtwo(this)">go to view username 2</a>
</div>
@ncole458
ncole458 / settings.py
Last active January 28, 2016 10:09
Mailgun for Django
"""
Mailgun is a nice option over common Gmail & can be used with a custom domain
""
# 1. setup a Mailgun account @ https://mailgun.com
# 2. add your domain & DNS records as provided on Mailgun (note: add "" around TXT records)
# 3. create new 'Route' with rules = match_recipient("you@yourdomain.com") & forward("anyone@domain.com")
EMAIL_BACKEND = "django.core.mail.backends.smtp.EmailBackend"
EMAIL_HOST = "smtp.mailgun.org"
@ncole458
ncole458 / haversine.py
Last active November 8, 2023 00:07
Django Haversine Formula Queryset
"""
Find user/database entries within a km radius based on long/lat co-ords.
i.e. return all objects where longlat lies within 10km of my current long/lat.
Using with Django REST Framework but approach is same for any similar req.
"""
import math
def get_queryset(self):
user = self.request.user
lat = self.request.query_params.get('lat', None)
@ncole458
ncole458 / distance.py
Last active March 8, 2023 10:11
Django queryset for objects within X long/lat radius
# Simple version of Django queryset for objects within X long/lat radius
import math
def get_queryset(self):
user = self.request.user
lat = self.request.query_params.get('lat', None)
lon = self.request.query_params.get('long', None)
if lat and lon:
lat = float(lat)
@ncole458
ncole458 / uuid-guid.js
Created February 11, 2016 13:12
UUID / GUID in JavaScript
// https://en.wikipedia.org/wiki/Universally_unique_identifier
// https://en.wikipedia.org/wiki/Globally_unique_identifier
// No hyphens (-) in below but can be add in the 'xxxxxxxxxxxx4xxxyxxxxxxxxxxxxxxx'
var uuid = 'xxxxxxxxxxxx4xxxyxxxxxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
return v.toString(16);
});
console.log(uuid);