Last active
January 8, 2016 19:26
-
-
Save AphonicChaos/2d8ead710b1fa30b910b to your computer and use it in GitHub Desktop.
Dynamic Message Contexts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from universal.helpers import message_context | |
from myjobs.models import User | |
from seo.models import Company | |
u = User.objects.get(email="[email protected]") | |
company = Company.objects.get(name="Test Company") | |
invitation = u.create_invitation(company, "[email protected]") | |
# for a default invitation email | |
invitation.send() | |
# for a custom invitation email | |
invitation.send("This is a test") | |
# for a saved search invitation email | |
saved_search = SavedSearch.objects.first() | |
invitation.send(saved_search) | |
# for a role invitation | |
role = Role.objects.filter(company=company, name="Admin") | |
invitation.send(role) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# in universal/helpers.py | |
def message_context(obj): | |
if isinstance(obj, basestring): | |
return {"message": obj} | |
elif obj is None: | |
return {"message": ""} | |
if hasattr(obj, "__message_context__"): | |
return obj.__message_context__() | |
else: | |
raise TypeError("object of type '%s' has no message_context()" % type(obj)) | |
# in mysearches/models.py | |
class SavedSearch(Model): | |
def __message_context___(self): | |
return {"message": "to receive saved searches", "saved_search": self} | |
# in myjobs/models.py | |
class Role(Model): | |
def __message_context__(self): | |
return {"message": "as a(n) %s for %s" % (self.name, self.company), "role": self} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from pkgutil import simplegeneric | |
from mysearches.models import SavedSearch | |
# in universal/helpers.py | |
@simplegeneric | |
def message_context(obj): | |
raise TypeError("object of type '%s' has no message_context()" % type(obj) | |
message_context.register(None)(lambda obj: {"message": ""} | |
message_context.register(str)(lambda obj: {"message": obj} | |
# in mysearches/models.py | |
message_context.register(SavedSearch)(lambda obj: {"message": "to receive saved searches", "saved_search": obj} | |
# in myjobs/models.py | |
message_context.register(Role)(lambda obj: {"message": "as a(n) %s for %s" % (obj.self, obj.company), "role": obj}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment