1

I have an app I created that gets my latest github commits. Other than the model that was created to cache the info, I'm doing a lot of work inside of a simple_tag but I am unsure of the best way to avoid doing this. The reason I made this into a tag is that I want to display this data as part of the main layout of my overall project and so this code should be run for every view that is called that extends my main layout.

Basically, my simple_tag is supposed to provide the last commit information. It calls a function that checks the last timestamp in a db and if it's over 1 hour old, it uses urllib2 to hit the github commit API to get the last commit and in turn store that data in the db and then print the fresh commit info. So, it basically looks a little something like this, minus some boilerplate code to actually get the json from github and such:

@register.simple_tag
def latest_commit(project_name):
    commit = get_latest_commit(project_name)
    return 'Latest commit message: %s' % commit.message

def get_latest_commit(project_name):
    commit = models.Commit.objects.get(pk=project_name)
    # ... 
    if not commit or now - commit.updated_at >= one_hour
        # use urllib2 to get github json string
        # ...
        # use simple json to parse string
        # ...
        commit = Commit.objects.create(message=json['mesage'])
        commit.save()
    return commit

I feel like this logic should go into another place... it feels a little dirty to me and there is a potential for XSS, but I'm new to Django and I'm unsure of the best way to do this...

2
  • 1
    maybe you could make a method in Commit do this and just call that method from the template Sep 7, 2011 at 16:32
  • 1
    "Are there any downsides"? The answer to a question like this is always trivial: "Yes." You may want to ask a better question that provides you with the information you really want. For example "What are the downsides..." or something that has an answer outside "Yes."
    – S.Lott
    Sep 7, 2011 at 17:00

1 Answer 1

0

I'm doing a lot of work inside of a simple_tag but I am unsure of the best way to avoid doing this.

Two Words: View Functions.

A view function should provide the last commit information. The view function should call a function that checks the last timestamp in a db and if it's over 1 hour old, it uses urllib2 to hit the github commit API to get the last commit and in turn store that data in the db and then ...

provide all the data, including the fresh commit info, to the page template for rending.

That's what a view function should do. The template renders the information provided by the view function.

6
  • Just making sure I fully understand.. all of this will go into view functions in views.py and then within the template tag, just import those view functions and call them as any other normal function?
    – intargc
    Sep 7, 2011 at 17:03
  • @intargc: I see that you don't understand Django very well. The template should do almost nothing. Template tags should be rare. A template evaluates nothing. It merely renders content in HTML. The view function should do almost all the work, providing Python objects to the template for rendering. 80% of the time you do not need customized template tags. The only use for a customized template tag is to do customized HTML rendering of unique Python objects. Focus on the view functions.
    – S.Lott
    Sep 7, 2011 at 18:24
  • I think I didn't explain what I was trying to do very well... I need this to be a tag. I have no other way of embedding the output of the latest commit in my main project otherwise. The output of this tag will not really a standard view... it's more of a sidebar type of item and therefore using the full MVT stack doesn't seem possible here since I will not be rendering any HTML or specifying any URL's to get at a "page". Another page is going to be rendered normally within my project and then within that page, a small chunk of code needs to run to get the latest commit...
    – intargc
    Sep 7, 2011 at 23:25
  • @intargc: I think I didn't explain what you're doing wrong very well. It should not be a tag. There is lot of ways of getting this data in a standard view function. A "sidebar" item doesn't break or modify the essential idea behind view functions at all. Using the "full MVT" stack is absolutely and always part of every request; you cannot turn off view function processing. A small chunk of code is why you use Python for the view functions. You have a view function which calls a small chunk of code.
    – S.Lott
    Sep 8, 2011 at 1:01
  • So, typically I'd use a view function to render a template or send an HttpResponse, right? For instance, I have a view that renders the index page of my main app, a view that renders the contact page, etc... Each page that is rendered from a view extends my main.html template. It's within that template that I need to execute and display the data from the github commit app. It needs to be executed every time any template is rendered. This can be done with a tag like I'm doing now, it could possibly be done with an ajax call to a view function to render the data or possibly by some middleware
    – intargc
    Sep 8, 2011 at 1:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.