Skip to content

Instantly share code, notes, and snippets.

@ratpik
ratpik / MainActivity.java
Last active July 9, 2018 05:56
PayTM Android Integration
package com.drc.paytm_example;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
@ratpik
ratpik / 0_reuse_code.js
Created August 19, 2014 12:45
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console
<!DOCTYPE html>
<html>
<head>
<meta name="description" content="Checkbox grid" />
<script src="//cdnjs.cloudflare.com/ajax/libs/handlebars.js/1.0.0/handlebars.js"></script>
<script src="//code.jquery.com/jquery-1.9.1.min.js"></script>
<meta charset="utf-8">
<title>JS Bin</title>
</head>
<body>
@ratpik
ratpik / dehydrate_metadata.py
Last active August 29, 2015 13:56
De-serialize json stored as unicode back to JSON for Tastypie
import json, ast
def dehydrate_metadata(self, bundle):
metadata_dict = ast.literal_eval(bundle.data['metadata'])
metadata_dict_str = dict([(str(k), str(v)) for k, v in metadata_dict.items()])
return json.dumps(metadata_dict_str)
@ratpik
ratpik / extra_data_resource.py
Created January 24, 2014 09:44
Add this to the tastypie model to extend the modelresource with other nested resources
def dehydrate(self, bundle):
x = bundle.obj.get_x()
ur = UserResource()
ur_bundle = ur.build_bundle(obj=x, request=bundle.request)
bundle.data['x'] = ur.full_dehydrate(ur_bundle)
return bundle
@ratpik
ratpik / basic_auth.py
Created December 30, 2013 13:33
Enable Basic Authetication in your django views. Useful when you have a ajax view accessed by a client that is csrf exempt on HTTP POST
import base64
from django.contrib.auth import authenticate
'''
In your views you can call has_basic_auth(request) to check if request headers have a valid Authorization header or not.
Returns False if header is absent or based on invalid username/password combination.
Handle the result in your view accordingly
@ratpik
ratpik / visualize_django_models
Created November 25, 2013 10:48
A way to visualize the schema of models in Django and take periodic snapshots to document schema changes over time. Uses the `django-extensions` app and `graphviz`
python manage.py graph_models <app > <app>.dot
dot <app>.dot -Tpng -o <app>+`date`.png
#Directory sizes sorted in reverse order
du -ch --exclude=.git --max-depth=1|sort -hr
@ratpik
ratpik / django_ajax_login.py
Created August 23, 2013 11:10
A simple way to log-in a user via an ajax request
from annoying.decorators import ajax_request
from django.contrib.auth import authenticate, login
from django.views.decorators.http import require_http_methods
@ajax_request
@require_http_methods(["POST"])
def ajax_login(request):
username = request.POST['username']
password = request.POST['password']
@ratpik
ratpik / patch_tastypie_imagefield.py
Last active December 21, 2015 12:49
A simple way to workaround using HTTP PATCH with Django tastypie on models that contain an Image/File Field
'''
I faced a problem when sending a HTTP PATCH to django models containing an ImageField.
The same problem was discussed on the django-tastypie mailing list - https://groups.google.com/forum/?fromgroups#!topic/django-tastypie/cxrI6Cl1z4s
When PATCHing a resource, tasypie dehydrates the original object, merges the changed fields, and then simulates a PUT. This presents a problem for FileField and ImageField as dehydrating these fields produces a URL to the file, rather than the path relative to MEDIA_ROOT stored in the database. Therefore, after a PATCH to a model with FileFields, those fields will contain full URLs rather than relatives paths.
For example, consider a set up in which media for a site is served from http://media.example.com/. When PATCHing a resource containing a file field, a reference to "/directory/file.jpg" will be dehydrated to "http://media.example.com/directory/file.jpg" which is the value that will be written back to the database. - Nikolas Stevenson-Molnar,