# -*- coding: utf-8 -*-
from django.http import Http404
from google.appengine.ext import db
from ragendja.pyutils import getattr_by_path
from random import choice
from string import ascii_letters, digits

def get_filters(*filters):
    """Helper method for get_filtered."""
    if len(filters) % 2 == 1:
        raise ValueError('You must supply an even number of arguments!')
    return zip(filters[::2], filters[1::2])

def get_filtered(data, *filters):
    """Helper method for get_xxx_or_404."""
    for filter in get_filters(*filters):
        data.filter(*filter)
    return data

def get_object_or_404(model, *filters_or_key, **kwargs):
    if kwargs.get('key_name'):
        item = model.get_by_key_name(kwargs.get('key_name'))
    elif kwargs.get('id'):
        item = model.get_by_id(kwargs.get('id'))
    elif len(filters_or_key) > 1:
        item = get_filtered(model.all(), *filters_or_key).get()
    else:
        item = model.get(filters_or_key[0])