Skip to content

Instantly share code, notes, and snippets.

Avatar

Ed Menendez edmenendez

View GitHub Profile
@edmenendez
edmenendez / json_help.py
Last active Aug 29, 2015
Django JSON Helper
View json_help.py
"""
Version 0.3.8 - Ed Menendez - ed@digitalhaiku.com
Add to any Django Model definition and you will have a instance.as_json() function).
For example >> class Customer(models.Model, JSONHelper):
Latest at https://gist.github.com/edmenendez/06bcd4ff56a70d2de3fd
"""
import copy
import json
View freshdesk_api.py
"""
Interface to the FreshDesk API.
Author: Ed Menendez (ed@menendez.com)
Company: Digital Haiku
Created: September 1, 2014
Example settings:
FRESHDESK_URL = 'https://wpt.freshdesk.com'
@edmenendez
edmenendez / random_manager.py
Last active Dec 17, 2015
Django manager to pull a random row from a query
View random_manager.py
import datetime
import random
from django.db import models
from django.db.models import Count
class RandomManager(models.Manager):
def __init__(self, *args, **kwargs):
self.when_last_cached = datetime.datetime.now() - datetime.timedelta(minutes=10)
@edmenendez
edmenendez / misc.py
Created May 14, 2013
Misc Django Utils. Including mail a Django Group
View misc.py
from django.conf import settings
from django.contrib.auth.models import User, Permission
from django.core.mail import EmailMessage, mail_admins, send_mail, EmailMultiAlternatives
from django.db.models import Q
from django.utils.html import strip_tags
def mail_group(codename, subject, message=None, debug=False, html_message=None):
perm = Permission.objects.get(codename=codename) # 'get_billing_emails'
users = User.objects.filter(Q(groups__permissions=perm) | Q(user_permissions=perm)).distinct()