Skip to content

Instantly share code, notes, and snippets.

@radiosilence
Created May 23, 2012 16:06
Show Gist options
  • Save radiosilence/2776125 to your computer and use it in GitHub Desktop.
Save radiosilence/2776125 to your computer and use it in GitHub Desktop.
# Some sample "models"
class Model(object):
id = None
def save(self, *args, **kwargs):
# Here would be some code that would get a list of objects to call
# invalidate() on when it is saved, based on self.id
super(Model, self).save(*args, **kwargs)
class User(Model):
def __init__(self, id, first_name, last_name):
self.id = id
self.first_name = first_name
self.last_name = last_name
# This would be the library's code.
class Fragment(object):
"""This is a super baby-steps idea for a fragment system that doesn't suck.
Fragments would be stored in one place, perhaps as a template tag (this
class would then act as a decorator or something, perhaps?) or perhaps
just as templates but with a central repository of files.
I don't know where these would be initialised, perhaps in the config or
another file (fragments.py?).
Look at the function comments to see what each function would do.
"""
template = None
invalidated_by = []
def __init__(self, *args):
self._invalidated = True
self._parents = []
self._args = []
self._context = {}
# Register that for arg in args
self._args = args
for arg in self._args:
if isinstance(arg, Model):
# register that when model with id arg.id updates, this cache
# should be invalidated
print "Registering", arg, "on", arg.__class__.__name__, \
"to invalidate when ", arg.id, "updated"
pass
def add_parent(self, parent):
if parent not in self._parents:
self._parents.append(parent)
def add_child(self, child, name, is_list=False):
if is_list:
try:
self._context[name]
except KeyError:
self._context[name] = []
self._context[name].append(child)
else:
self._context[name] = child
child.add_parent(self)
def get(self, *args):
"""Return the HTML, and invalidate if for some reason the object is
not in the cache."""
#cached = cache.get(self.key.format(self._args))
#if not cached:
# self.invalidate(id)
pass
def invalidate(self, *args):
# 1. Regenerate own HTML fragment based on args defined in __init__
# 2. Trigger
print "INVALIDATING", self
self._invalidated = True
for parent in self._parents:
parent.invalidate(*self._args)
def regenerate(self):
if self.invalidated:
# regenerate
self._generate()
self.invalidated = False
#cache.set(self.key.format(self._args), self._generate())
def _generate(self):
"""Actual function to compile template"""
# The context wwould be got from self._context, and handed to django's
# template rendering code.
# There would be a special {% fragment %} template tag to render
# fragments that are in self._context
pass
# This would be inside your app, probably in a file called fragments.py or
# dolls.py
class UserInfoFragment(Fragment):
classes = [User, ]
template = 'userinfo.html'
class UserListFragment(Fragment):
template = 'userlist.html'
# This is just some sample code, it's rather a mess.
user = User(1, first_name='Steve', last_name='Parp')
user2 = User(2, first_name='Joe', last_name='Dong')
uf = UserInfoFragment(user)
u2f = UserInfoFragment(user2)
ulf = UserListFragment()
ulf.add_child(uf, 'users', True)
ulf.add_child(uf, 'users', True)
uf.invalidate()
print ulf._context
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment