Django: Grouping by Dates and Servers

Posted by TheLizardKing on Stack Overflow See other posts from Stack Overflow or by TheLizardKing
Published on 2010-06-08T17:55:00Z Indexed on 2010/06/08 19:12 UTC
Read the original article Hit count: 242

Filed under:
|
|
|

So I am trying to emulate google app's status page: http://www.google.com/appsstatus#hl=en but for backups for our own servers. Instead of service names on the left it'll be server names but the dates and hopefully the pagination will be there too. My models look incredibly similar to this:

from django.db import models

STATUS_CHOICES = (
    ('UN', 'Unknown'),
    ('NI', 'No Issue'),
    ('IS', 'Issue'),
    ('NR', 'Not Running'),
)

class Server(models.Model):
    name = models.CharField(max_length=32)

    def __unicode__(self):
        return self.name

class Backup(models.Model):
    server = models.ForeignKey(Server)
    created = models.DateField(auto_now_add=True)
    modified = models.DateTimeField(auto_now=True)
    status = models.CharField(max_length=2, choices=STATUS_CHOICES, default='UN')
    issue = models.TextField(blank=True)

    def __unicode__(self):
        return u'%s: %s' % (self.server, self.get_status_display())

My issue is that I am having a hell of a time displaying the information I need. Everyday a little after midnight a cron job will run and add a row for each server for that day, defaulting on status unknown (UN).

My backups.html:

{% extends "base.html" %}
{% block content %}
<table>
    <tr>
        <th>Name</th>
{% for server in servers %}
        <th>{{ created }}</th>
    </tr>
    <tr>
        <td>{{ server.name }}</td>
        {% for backup in server.backup_set.all %}
        <td>{{ backup.get_status_display }}</td>
        {% endfor %}

    </tr>
{% endfor %}
</table>
{% endblock content %}

This actually works but I do not know how to get the dates to show. Obviously {{ created }} doesn't do anything but the servers don't have create dates. Backups do and because it's a cron job there should only be X number of rows with any particular date (depending on how many servers we are following for that day).

Summary

I want to create a table, X being server names, Y being dates starting at today while all the cells being the status of a backup. The above model and template should hopefully give you an idea what my thought process but I am willing to alter anything. Basically I am create a fancy excel spreadsheet.

© Stack Overflow or respective owner

Related posts about sql

Related posts about django