Skip to content

Instantly share code, notes, and snippets.

View santiagobasulto's full-sized avatar

Santiago Basulto santiagobasulto

View GitHub Profile
class ModelResource(Resource):
"""
A subclass of ``Resource`` designed to work with Django's ``Models``.
This class will introspect a given ``Model`` and build a field list based
on the fields found on the model (excluding relational fields).
Given that it is aware of Django's ORM, it also handles the CRUD data
operations of the resource.
"""
+----+-------------+-------+--------+--------------------------+--------------------------+---------+-------------------+------+----------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+--------+--------------------------+--------------------------+---------+-------------------+------+----------------------------------------------+
| 1 | SIMPLE | w | index | stats_workout_fbfc09f1 | stats_workout_fbfc09f1 | 4 | NULL | 32 | Using index; Using temporary; Using filesort |
| 1 | SIMPLE | pr | eq_ref | user_id | user_id | 4 | athlete.w.user_id | 1 | Using index |
| 1 | SIMPLE | p | ref | workout_id | workout_id | 5 | athlete.w.id | 1 | Using index
+----+-------------+-------+--------+------------------------+------------------------+---------+-------------------+------+----------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+--------+------------------------+------------------------+---------+-------------------+------+----------------------------------------------+
| 1 | SIMPLE | w | index | stats_workout_fbfc09f1 | stats_workout_fbfc09f1 | 4 | NULL | 32 | Using index; Using temporary; Using filesort |
| 1 | SIMPLE | pr | eq_ref | user_id | user_id | 4 | athlete.w.user_id | 1 | Using index |
| 1 | SIMPLE | p | ref | workout_id | workout_id | 5 | athlete.w.id | 1 | Using index
@santiagobasulto
santiagobasulto / gist:2835978
Created May 30, 2012 12:32
Most important queries
{
"query" : {
"bool":{
"must" :[
{"query_string" : {"query" : "Sam*"}},
{"term" : { "subcategoria" : "monitores" }},
{"term" : { "resolucion_pantalla" : "1024x768" }}
]
}
},
<?xml version="1.0" encoding="UTF-8"?>
<gpx version="1.1" creator="Exported from Strava"
xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd http://www.garmin.com/xmlschemas/GpxExtensions/v3 http://www.garmin.com/xmlschemas/GpxExtensionsv3.xsd http://www.garmin.com/xmlschemas/TrackPointExtension/v1 http://www.garmin.com/xmlschemas/TrackPointExtensionv1.xsd"
xmlns="http://www.topografix.com/GPX/1/1"
xmlns:gpxtpx="http://www.garmin.com/xmlschemas/TrackPointExtension/v1"
xmlns:gpxx="http://www.garmin.com/xmlschemas/GpxExtensions/v3" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<metadata>
<link href="http://cosmocatalano.com/strava-gpx-export">
<text>Cosmo's Strava GPX Exporter</text>
</link>
@santiagobasulto
santiagobasulto / gist:2845549
Created May 31, 2012 19:13
Workout format for Athlete.com API
{
"run_date": "1970-01-01T00:00:00Z",
"title": "Run Title!",
"run_type": "Endurance",
"duration_in_seconds": 3600,
"distance_in_meters": 1000,
"post_body": "This is the body, in order to provide a full description of your run",
"points": [
{
"lng":"-111.5373066",
@santiagobasulto
santiagobasulto / gist:2845849
Created May 31, 2012 20:00
Complete Athlete Workout example
{
"run_date": "1970-01-01T00:00:00Z",
"run_type": "Endurance",
"title": "Run Title!",
"distance_in_meters": 1000,
"duration_in_seconds": 3600,
"post_body": "This is the body, in order to provide a full description of your run",
"points": [
{
@santiagobasulto
santiagobasulto / gist:2851944
Created June 1, 2012 12:55
Stored format for route points in Athlete.com
[
{
"lng": "-111.5373066",
"lat": "40.7231711",
"time": "1970-01-01T00:00:04Z",
"elev": "1942.1789265256325"
},
{
"lng": "-111.5372056",
"lat": "40.7228762",
@santiagobasulto
santiagobasulto / gist:2897326
Created June 8, 2012 18:06
Quick hack for turning full=True on related fields on Tastypie
""" This is a quick fix to provide full dehydration by demand
on Django Tastypie.
In order to use it you can query for:
/api/v1/user/1/full=True
"""
class RelatedResource(ModelResource):
@santiagobasulto
santiagobasulto / gist:3053583
Created July 5, 2012 13:12
Ranking queries
-- Query 1
SELECT id, distance_in_meters from stats_workout s1
WHERE distance_in_meters > (SELECT distance_in_meters FROM stats_workout WHERE id=3)
ORDER BY distance_in_meters DESC;
-- Query 2
SELECT DISTINCT s1.id, s1.distance_in_meters from stats_workout s1
JOIN stats_workout s2 ON (s1.distance_in_meters > s2.distance_in_meters)