#!/usr/bin/env python # -*- coding: UTF-8 -*- """ Title: Resume access management models Author: Will Hardy Project: TheProfessional (resume module) Date: September 2007 Test Suite: ../manage.py test resume $Revision: 207 $ Copyright: Will Hardy 2007, released under GPL version 3 (see licence.txt) Description: """ import datetime from django.db import models from django.contrib.sites.models import Site from django.utils.translation import gettext_lazy as _ from theprofessional import tools __all__ = ('Visitor', 'ResumeView') class Visitor(models.Model): """ An visitor is just a tag that can refer to a specific visitor or a group of visitor. A passphrase is defined to limit access to areas of the site, a weak security precaution. This is used to confirm that a request is from an visitor. """ # Attributes name = models.CharField(_("visitor name"), max_length=100) passphrase = models.CharField(_("passphrase"), max_length=20, blank=True, unique=True) active = models.BooleanField(_("active"), default=True) expiry_date = models.DateField(_("expiry date"), default=None, blank=True, null=True) # None is 'never'. date_created = models.DateField(_("date created"), auto_now=True) def save(self): " Overriding save method automate some fields " # PASSPHRASE # Initialise the passphrase to something random, if it's empty if not self.passphrase: self.passphrase = tools.randomPassphrase(length=6) # EXPIRY DATE # Automatically sets the expiry date, 1 month into the future #self.expiry_date = datetime.date.today() + datetime.timedelta(weeks=4) super(Visitor, self).save() def url(self): " Returns the special link that this passphrase creates, for copy+paste convenience. " domain = Site.objects.get_current().domain return "http://%s/cv/%s/" % (domain, self.passphrase) url.short_description = 'special url' url.allow_tags = True def link(self): " Returns an HTML Link for the CVs URL " link = self.url() return u'%s' % (link, link) link.short_description = 'special link' link.allow_tags = True def isActive(self): " Combines expiry date and active flag to allow convenient checking. " if not self.active: return False elif self.expiry_date and self.expiry_date <= datetime.date.today(): return False else: return True isActive.short_description = 'is currently active' isActive.boolean = True #is_active = property(isActive) def __unicode__(self): return self.name class Admin: list_display = ('name', 'passphrase', 'link', 'isActive', 'expiry_date') search_fields = ('name', 'passphrase') class Meta: verbose_name = _("visitor") verbose_name_plural = _("visitors") class ResumeView(models.Model): " Record who has read a resume and when. " visitor = models.ForeignKey(Visitor, verbose_name=_("visitor")) timestamp = models.DateTimeField(_("visitor"), auto_now_add=True) ip_address = models.IPAddressField(_("IP address"), null=True) def getTime(self): " Returns a linguistic description of the time. " return tools.fuzzyTime(self.timestamp) when = property(getTime) def __unicode__(self): return "%s: %s" % (self.when, self.visitor) class Admin: list_display = ('when', 'visitor', 'timestamp', 'ip_address') list_filter = ('visitor', ) ordering = ('-timestamp', ) class Meta: verbose_name = _('resume view') verbose_name_plural = _('resume views')