Skip to content

Instantly share code, notes, and snippets.

@zetas
zetas / gist:8707088
Created January 30, 2014 12:01
Secure UUID Generation in PHP. Change 16 to get a larger or smaller uuid.
$bytes = openssl_random_pseudo_bytes(16);
$uuid = bin2hex($bytes);
@zetas
zetas / views.py
Created February 12, 2014 20:34
User verify ajax method
class LicenseVerifyView(View):
def render_to_json_response(self, context, **response_kwargs):
data = json.dumps(context)
response_kwargs['content_type'] = 'application/json'
return HttpResponse(data, **response_kwargs)
def get(self, request, *args, **kwargs):
if request.is_ajax():
data = {
@zetas
zetas / views.py
Created February 16, 2014 02:20
Too much logic in a post method? You're probably right.
def post(self, request, *args, **kwargs):
form = CheckoutForm(request.POST)
if form.is_valid():
user = request.user
stripe.api_key = settings.STRIPE_SECRET
token = form.cleaned_data['stripe_token']
quantity = form.cleaned_data.get('quantity', 1)
customer = stripe.Customer.retrieve(user.stripe_customer_id)
customer.card = token
customer.save()
@zetas
zetas / views.py
Created February 16, 2014 15:43
Biggest view ever.
class UpgradeView(View):
form_class = CheckoutForm
template = 'account/checkout.html'
def create_sub(self, user, customer, plan, quantity):
subscription = customer.subscriptions.create(plan=plan, quantity=quantity)
user.stripe_subscription_id = subscription.id
user.save()
def get(self, request, *args, **kwargs):
/* --- LOADING [css.bootstrap.min] from assets/components/core/lib/bootstrap/css/bootstrap.min.css */
@import '../../../assets/components/core/lib/bootstrap/css/bootstrap.min.css';
/* --- LOADING [css.font-awesome.min] from assets/components/ui/icons/fontawesome/assets/css/font-awesome.min.css */
@import '../../../assets/components/ui/icons/fontawesome/assets/css/font-awesome.min.css';
/* --- LOADING [css.glyphicons_regular] from assets/components/ui/icons/glyphicons/assets/css/glyphicons_regular.css */
@import '../../../assets/components/ui/icons/glyphicons/assets/css/glyphicons_regular.css';
<link rel="stylesheet/less" href="../assets/less/pages/module.front.page.index.less" />
<script src="../assets/components/core/lib/jquery/jquery.min.js"></script>
<script src="../assets/components/core/lib/jquery/jquery-migrate.min.js"></script>
<script src="../assets/components/core/lib/plugins/less-js/less.min.js"></script>
@zetas
zetas / views.py
Created March 12, 2014 18:20
CheckoutView for an e-commerce site with licenses and recurring payments.
class CheckoutView(View):
form_class = CheckoutForm
template = 'account/checkout.html'
email_template_name = 'account.checkout_success'
def create_sub(self, user, customer, plan, quantity):
subscription = customer.subscriptions.create(plan=plan, quantity=quantity)
user.stripe_subscription_id = subscription.id
user.save()
@zetas
zetas / models.py
Created March 12, 2014 18:37
Complex data model for new "Classroom" addition to the app. Requires using a intermediary relationship table to achieve the necessary abstract-ness.
class Classroom(models.Model):
name = models.CharField(max_length=100)
code = models.CharField(max_length=10, unique=True, default=_create_small_code)
students = models.ManyToManyField(WUser, through='Attendance')
creation = models.DateTimeField(auto_now_add=True)
def __unicode__(self):
return self.name
def class_code(self):
@zetas
zetas / tests.py
Created March 12, 2014 18:43
Tests for the intermediary model
import random
from django.test import TestCase
from django.utils.text import slugify
from account.models import WUser
from classes.models import Classroom, Attendance
# Create your tests here.
@zetas
zetas / Gruntfile.js
Created May 5, 2014 17:41
Yes, I finally made the plunge into grunt.
module.exports = function(grunt) {
// Project configuration.
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
uglify: {
options: {
banner: '/*! <%= pkg.name %> <%= grunt.template.today("yyyy-mm-dd") %> */\n'
},
build: {