Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View percyperez's full-sized avatar

Percy Perez percyperez

  • ORCAS
  • Eugene, Oregon
View GitHub Profile
@percyperez
percyperez / gist:4288072
Created December 14, 2012 19:46
Regular expression for validating time hh:mm am|pm
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 July 31, 2012 17:22
Get user avatar url from Twitter, FB via django-social-auth
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 / create_api_key.py
Created June 4, 2013 04:15
Signal function to auto-create ApiKey objects when using tastypie ApiKeyAuthentication.
# 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 April 18, 2013 16:22
Custom User Model with Django 1.5 due to "relation auth_user doesn't exist..." error using AbstractUser class
# 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:1918232
Created February 26, 2012 18:45
Convert view for lazysignup with template_name
@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, {
@percyperez
percyperez / startwork.sh
Created August 12, 2014 23:10
This script starts the virtual machine, ssh into the VM and then runs django's run server
#!/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 21:18
Get populated data from subdocument excluding specific fields
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 April 14, 2014 01:37
Promise chain using q and Mongoose models. Passing result from previous promise to another
// 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)