Skip to content

Instantly share code, notes, and snippets.

Avatar

Percy Perez percyperez

View GitHub Profile
@percyperez
percyperez / startwork.sh
Created Aug 12, 2014
This script starts the virtual machine, ssh into the VM and then runs django's run server
View startwork.sh
#!/bin/bash
cd ~/sandbox/git/moreau/devel/cms_dev/src/storyville
vagrant up
vagrant ssh -c 'cd ~/devel/cms_dev/src/storyville/medley/ && med rs'
@percyperez
percyperez / gist:034a2c2ca66603fcf67e
Created May 23, 2014
Get populated data from subdocument excluding specific fields
View gist:034a2c2ca66603fcf67e
var populate_data_from = 'trainers tags',
video_data = {
path: 'videos',
model: 'Video',
select: 'title description legacy_video videofiles.sources.10'
},
excludes = '-royalties';
/**
* @return {object} A single collection with videos, tags and trainers
@percyperez
percyperez / collection.js
Created Apr 14, 2014
Promise chain using q and Mongoose models. Passing result from previous promise to another
View collection.js
// The route
var Q = require('q');
module.exports = function(app) {
var promise = require('./path/to/promise'),
promise.init(app);
app.post('someurl/add', function(req, res, next) {
Q.fcall(promise.createModel1, req.body)
.then(promise.createModel2)
@percyperez
percyperez / create_api_key.py
Created Jun 4, 2013
Signal function to auto-create ApiKey objects when using tastypie ApiKeyAuthentication.
View create_api_key.py
# models.py
# Work around the ImportError: cannot import name create_api_key
# Issue https://github.com/toastdriven/django-tastypie/issues/937
class User(AbstractUser):
field1
...
@receiver(post_save, sender=User)
def create_user_api_key(sender, **kwargs):
@percyperez
percyperez / gist:5414079
Created Apr 18, 2013
Custom User Model with Django 1.5 due to "relation auth_user doesn't exist..." error using AbstractUser class
View gist:5414079
# forms.py
from django import forms
from django.contrib.auth.forms import UserChangeForm, UserCreationForm
from .models import User # Your custom user model
class CustomUserCreationForm(UserCreationForm):
class Meta:
model = User
@percyperez
percyperez / gist:4288072
Created Dec 14, 2012
Regular expression for validating time hh:mm am|pm
View gist:4288072
re = /(^(\d{1,2}):(\d{2})\s?(?:am|pm)?)$|^\d{1}(?:am|pm)$/i;
var reminder = (attributes.reminder || '').replace(/\./g, '').replace(/\s/g, '');
if (reminder !== '' && !reminder.match(re)) {
return "Enter valid time ie 3:30 pm or 5pm";
}
/* match result
12:00 pm
6pm
6 pm
@percyperez
percyperez / get_avatar_url
Created Jul 31, 2012
Get user avatar url from Twitter, FB via django-social-auth
View get_avatar_url
def get_avatar_url(request, backend, response, *args, **kwargs):
"""Pipeline to get user avatar from Twitter/FB via django-social-auth"""
avatar_url = ''
if isinstance(backend, FacebookBackend):
avatar_url = 'http://graph.facebook.com/%s/picture?type=large' \
% response['id']
elif isinstance(backend, TwitterBackend):
avatar_url = response.get('profile_image_url', '')
request.session['avatar_url'] = avatar_url
return
@percyperez
percyperez / gist:1918232
Created Feb 26, 2012
Convert view for lazysignup with template_name
View gist:1918232
@allow_lazy_user
def convert(request, form_class=UserCreationForm,
redirect_field_name='redirect_to',
anonymous_redirect=settings.LOGIN_URL,
template_name='lazysignup/convert.html'):
...
...
...
return direct_to_template(request, template_name, {
You can’t perform that action at this time.