Skip to content

Instantly share code, notes, and snippets.

@jmahmood
Created January 26, 2012 03:44
Show Gist options
  • Save jmahmood/1680845 to your computer and use it in GitHub Desktop.
Save jmahmood/1680845 to your computer and use it in GitHub Desktop.
An example of a reusable Django View
# This is not tested and not complete, but give an interesting example of how you can turn a Django view into something reusable.
# It is based on the code here: http://code.google.com/p/django-jqchat/
# It might make sense to turn this into an abstract base class.
# The purpose of this is:
# 1. Improved code reuse for outputting data.
# 2. Easier modification to relevant areas
# 3. Because it is new and interesting to me (Kiss my future jobs goodbye for admitting that :/)
# 4. Can stash it in a different file, so the view is not as cluttered-up.
# For example, you could define a class JsonExchange(ChatExchange) and make small modifications to output as JSON,
# an XMLExchange(ChatExchange) to do the same for XML, etc... It seems far more reusable, although I could be wrong.
class ChatExchange(object):
def authorize(self, request):
# This is how we normally authorize the chat request.
return request.user.is_authenticated()
def invalid_login(self):
return HttpResponseBadRequest('You need to be logged in to access the chat system.')
def create_conversation(self, user1, user2):
c = Conversation()
c.name = "Conversation between %s and %s" % (str(user1), str(user2))
c.distributor.add(user1)
c.distributor.add(user2)
c.save()
return c
# This retrieves the conversation
def get_conversation(self):
user1 = self.request.user.id
user2 = user.objects.get(username=self.request.POST.get("to"))
# find a conversation that has user1 and user2
try:
conversation = Conversation.objects.filter(distributor__id=user1.id).filter(distributor__id=user2.id)
if conversation.open:
self.conversation = conversation
return
elif Conversation.can_start_conversation(user1, user2):
self.conversation = conversation
return
except:
if Conversation.can_start_conversation(user1, user2):
self.conversation = self.create_conversation(user1, user2)
return
self.conversation = False
# This adds messages to the conversation.
def add_messages(self):
if "new_message" not in self.request.POST:
return
if not self.conversation:
return
user1 = self.request.POST.get("userid")
m = Message()
m.user = user1
m.conversation = self.conversation
m.text = self.request.POST.get("message")
m.save()
self.message = m
self.statusCode = 2
# This retrieves the messages in the conversation
def get_messages(self):
if not self.conversation:
return
self.messages = Message.objects.filter(conversation=self.conversation)
self.statusCode = 1
# An empty function that you can define to filter messages or perform any transformations necessary before output is made.
def prehook(self):
pass
# An empty function that you can define to filter messages or perform any transformations necessary before output is made.
def posthook(self):
pass
# Outputs messages in the format defined. Must be overridden.
def output_messages(self):
raise NotImplementedError( "Should have implemented this" )
# Outputs a success message if you want to override something or other.
def output_success_message(self):
raise NotImplementedError( "Should have implemented this" )
# Outputs a list of errors in the program.
def output_errors(self):
raise NotImplementedError( "Should have implemented this" )
def __call__(self, request):
if not self.authorize(request):
return self.invalid_login(request)
self.statusCode = 0
self.request = request
self.prehook()
self.get_conversation()
self.add_messages()
self.get_messages()
self.posthook()
if self.statusCode == 1:
return self.output_messages()
if self.statusCode == 2:
return self.output_success_message()
return self.output_errors()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment