Skip to content

Instantly share code, notes, and snippets.

View armonge's full-sized avatar
🏠

Andrés Reyes Monge armonge

🏠
View GitHub Profile
@armonge
armonge / fields.py
Created July 19, 2011 17:32
Django fields to validate file content type
class ContentTypeRestrictedFileField(models.FileField):
"""
Tomado de http://nemesisdesign.net/blog/coding/django-filefield-content-type-size-validation/
Same as FileField, but you can specify:
* content_types - list containing allowed content_types. Example: ['application/pdf', 'image/jpeg']
* max_upload_size - a number indicating the maximum file size allowed for upload.
2.5MB - 2621440
5MB - 5242880
10MB - 10485760
20MB - 20971520
@armonge
armonge / gist:2830057
Created May 29, 2012 19:04
django youtube field
import urlparse
import re
from django.db import models
from django import forms
def validate_youtube_url(value):
'''El patron lo saque de http://stackoverflow.com/questions/2964678/jquery-youtube-url-validation-with-regex'''
pattern = r'^http:\/\/(?:www\.)?youtube.com\/watch\?(?=.*v=\w+)(?:\S+)?$'
@armonge
armonge / mixins.py
Created August 22, 2011 16:35
A model Mixin to use in translatable applications, to use inherit your models from this
class TranslatableModelMixin(models.Model):
'''
Adapted from http://snippets.dzone.com/posts/show/2979
'''
language = models.CharField(max_length=2, choices=settings.LANGUAGES, db_index=True)
translation_of = models.ForeignKey(
to='self',
related_name='translation_set',
limit_choices_to={'language' : settings.DEFAULT_LANGUAGE},
blank=True,
@armonge
armonge / fields.py
Created July 18, 2011 14:41
A Django custom modelfield, formfield and formwidget to select and save a set of geographic coordinates using Google Maps
class GoogleMapMarker(object):
def __init__(self, latitude, longitude):
self.latitude = latitude
self.longitude = longitude
def __unicode__(self):
return '%f,%f'%(self.latitude, self.longitude)
def __len__(self):
return len(self.__unicode__())
#!/usr/bin/env bash
set -euo pipefail
set -o xtrace
if [ ! -f "dialogflow_key.json" ] && [ "${DIALOGFLOW_KEY:-}" ]; then
echo "$DIALOGFLOW_KEY" >dialogflow_key.json
fi
if [ -f "dialogflow_key.json" ]; then
gcloud auth activate-service-account --key-file=dialogflow_key.json
@armonge
armonge / deploy-alexa.sh
Last active April 4, 2019 07:34
voxa-cli
#!/usr/bin/env bash
set -euo pipefail
# set -o xtrace
if [ ! -d "$HOME/.ask" ]; then
# Create the .ask directory
mkdir ~/.ask
# Create ASK config
sed -e s/ASK_ACCESS_TOKEN/"${ASK_ACCESS_TOKEN}"/g -e \
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@armonge
armonge / Dialogflow Translation strings.ipynb
Last active December 19, 2018 00:10
Count the words in all different phrases of a Dialogflow agent
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@armonge
armonge / admin.py
Created January 13, 2012 04:13
Show an image in django admin list_display, using sorl-thumbnail
class ProductAdmin(admin.ModelAdmin):
list_display = ('name','thumb')
def thumb(self, obj):
return render_to_string('thumb.html',{
'image': obj.image
})
thumb.allow_tags = True
@armonge
armonge / futures_pi.py
Created September 11, 2014 05:55
Calculating PI with Python
import functools
import random
import math
from concurrent import futures
def map_d(c):
return math.hypot(random.random(), random.random())