Source

HackersEdge / he_one / models.py

from django.db import models
from django.contrib.auth.models import User
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes import generic
from django.core.cache import cache
import hashlib
from django.db.models.signals import post_save
import datetime

class Objective(models.Model):
    ACTION_TYPES = (
        (0, 'None'),
        (1, 'Delete'),
        (2, 'Steal'),
        (3, 'Email'),
        (4, 'Connect'),
    )
    content_type = models.ForeignKey(ContentType)
    object_id = models.PositiveIntegerField()
    target = generic.GenericForeignKey()
    action = models.PositiveSmallIntegerField(choices=ACTION_TYPES)
    is_object = models.BooleanField()
    exp = models.PositiveIntegerField('Experience')
    def __unicode__(self):
        return u"%s %s" % (self.get_action_display(), self.target)

class Mission(models.Model):
    title = models.CharField(max_length=80)
    description = models.TextField()
    level = models.PositiveSmallIntegerField('Difficulty Level')
    cash = models.PositiveIntegerField('Credits')
    owner = models.ForeignKey(User)
    objectives = models.ManyToManyField(Objective, through='MissionObjectives')
    def __unicode__(self):
        return u"%s" % self.title

class MissionObjectives(models.Model):
    objective = models.ForeignKey(Objective)
    mission = models.ForeignKey(Mission)
    order = models.PositiveSmallIntegerField()
    def __unicode__(self):
        return u"%s: %s" % (self.mission, self.objective)
    class Meta:
        verbose_name = 'mission objective'
        verbose_name_plural = 'mission objectives'

class Log(models.Model):
    by_who = models.ForeignKey(User)
    message = models.CharField(max_length=80)
    logged_at = models.DateTimeField()
    deleted = models.BooleanField()
    def __unicode__(self):
        return u"%s" % self.message

class PCFile(models.Model):
    filename = models.CharField(max_length=8)
    file_type = models.CharField(max_length=3)
    content = models.TextField()
    is_booted = False
    def __unicode__(self):
        return u"%s.%s" % (self.filename, self.file_type)
    class Meta:
        verbose_name = 'file'
        verbose_name_plural = 'files'
    def get_filesize(self):
        return len(self.content)

class Message(models.Model):
    from_user = models.ForeignKey(User, related_name='sent_set', verbose_name='From')
    to_user = models.ForeignKey(User, verbose_name='To')
    subject = models.CharField(max_length=80)
    body = models.TextField()
    attachments = models.ManyToManyField(PCFile, blank=True, null=True)
    def __unicode__(self):
        return u"%s" % self.subject
    @models.permalink
    def get_absolute_url(self):
        return ('message-detail', [self.pk])

class Mailbox(models.Model):
    owner = models.ForeignKey(User)
    messages = models.ManyToManyField(Message, blank=True, null=True)
    def __unicode__(self):
        return u"%s's Mailbox" % self.owner
    @models.permalink
    def get_absolute_url(self):
        return ('mailbox-detail', [str(self.pk)])
    class Meta:
        verbose_name_plural = 'mailboxes'

class Host(models.Model):
    ip = models.IPAddressField('IP Address', unique=True, db_index=True)
    gateway = models.ForeignKey('self', null=True, blank=True)
    owner = models.ForeignKey(User, related_name='hosts')
    users = models.ManyToManyField(User, through='UserPermission')
    is_router = models.BooleanField()
    is_booted = models.BooleanField()
    files = models.ManyToManyField(PCFile, through='FilePermission')
    supports_mail = models.BooleanField()
    mailboxes = models.ManyToManyField(Mailbox, blank=True, null=True)
    logs = models.ManyToManyField(Log, blank=True, null=True)
    supports_web = models.BooleanField()
    supports_store = models.BooleanField()
    supports_download = models.BooleanField()
    supports_search = models.BooleanField()
    provides_missions = models.BooleanField()
    mission_list = models.ManyToManyField(Mission, blank=True, null=True)
    def __unicode__(self):
        return u"%s" % self.ip
    @models.permalink
    def get_absolute_url(self):
        return ('host-detail', [self.ip])

class UserPermission(models.Model):
    user = models.ForeignKey(User)
    host = models.ForeignKey(Host)
    is_staff = models.BooleanField()
    is_superuser = models.BooleanField()
    def __unicode__(self):
        return u"%s's permission on %s" % (self.user, self.host)

class FilePermission(models.Model):
    pcfile = models.ForeignKey(PCFile)
    host = models.ForeignKey(Host)
    creator = models.ForeignKey(User)
    create_date = models.DateTimeField()
    is_hidden = models.BooleanField()
    deleted = models.BooleanField()
    patch = models.IntegerField()
    is_public = models.BooleanField()
    cost = models.PositiveIntegerField(blank=True, null=True)
    def __unicode__(self):
        return "%s on %s" % (self.pcfile, self.host)

class NameSystem(models.Model):
    hostname = models.CharField(max_length=20)
    domain = models.CharField(max_length=30)
    slug = models.SlugField(max_length=51)
    host = models.ForeignKey(Host)
    class Meta:
        unique_together = ('hostname', 'domain',)
        verbose_name = 'DNS entry'
        verbose_name_plural = 'DNS entries'
    def __unicode__(self):
        return u"%s.%s" % (self.hostname, self.domain)
    def get_ipaddr(self):
        return u"%s" % self.host.ip

class UserProfile(models.Model):
    user = models.OneToOneField(User)
    tutorial = models.PositiveSmallIntegerField()
    exp = models.PositiveIntegerField('Experience')
    cash = models.PositiveIntegerField('Credits')
    home_host = models.ForeignKey(Host, blank=True, null=True)
    def __unicode__(self):
        return u"%s's Profile" % self.user

class Task(models.Model):
    owner = models.ForeignKey(User)
    host = models.ForeignKey(Host)
    end_time = models.DateTimeField()
    target_host = models.ForeignKey(Host, null=True, blank=True, related_name='remote_tasks')
    content_type = models.ForeignKey(ContentType)
    object_id = models.PositiveIntegerField()
    target = generic.GenericForeignKey()
    is_download = models.BooleanField()
    is_delete = models.BooleanField()
    def __unicode__(self):
        return u"Task %s" % self.pk
    def is_complete(self):
        return self.end_time > datetime.datetime.now()

def get_cache_key(fragment, arg):
    return "template.cache.%s.%s" % (fragment, hashlib.md5(u'%s' % arg).hexdigest())

def clear_emails_cache(sender, instance, **kwargs):
    cache.delete(get_cache_key("emails", instance.pk))
    cache.delete(get_cache_key("mailboxes", instance.owner.pk))

def clear_hosts_cache(sender, instance, **kwargs):
    cache.delete(get_cache_key("hosts", instance.owner.pk))

def clear_logs_cache(sender, instance, **kwargs):
    cache.delete(get_cache_key("logs", instance.by_who.pk))

def clear_profile_cache(sender, instance, **kwargs):
    cache.delete('profile-%s' % instance.user.pk)

def clear_tools_cache(sender, instance, **kwargs):
    cache.delete('usertools-%s' % instance.host.owner.pk)

post_save.connect(clear_emails_cache, sender=Mailbox)
post_save.connect(clear_hosts_cache, sender=Host)
post_save.connect(clear_logs_cache, sender=Log)
post_save.connect(clear_profile_cache, sender=UserProfile)
post_save.connect(clear_tools_cache, sender=FilePermission)