~bnrubin/bantrackertwo/devel

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
from django.db import models
from django.contrib.auth.models import User
from django.db.models import signals
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes import generic
from pytz import common_timezones
from django import forms
from django.forms import ModelForm
import re

# Make a dict of all timezones using pytz instead of maintaining a static dict here
TZ_CHOICES = []
for zone in common_timezones:
    TZ_CHOICES.append((zone,zone))


class Issue(models.Model):
    create_date = models.DateTimeField(auto_now_add = True,editable = False)
    modify_date = models.DateTimeField(auto_now = True, editable = False)
    closed_date = models.DateTimeField(null = True)
    author = models.ForeignKey(User,null = True)
    description = models.TextField()
    log = models.ForeignKey('Log', null = True)
    comments = generic.GenericRelation('Comment')

    def is_closed(self):
        return self.closed_date != None

class IssueForm(ModelForm):
    class Meta:
        model = Issue
        fields = ('description',)
    


class Event(models.Model):
    "Defines the Event base class. Every IRC event class should inherit from this."
    create_date = models.DateTimeField(auto_now_add = False)
    modify_date = models.DateTimeField(auto_now = True) 
    channel = models.CharField(max_length=255)
    log = models.ForeignKey('Log')
    operator = models.ForeignKey('Operator')
    hostmask = models.CharField(max_length=255)
    comments = generic.GenericRelation('Comment')
    
    def __unicode__(self):
        return self.hostmask
   
    def nick(self):
        return self.hostmask.split('!')[0]
    
    
class Ban(Event):
    mask = models.CharField(max_length=255)
    removal_date = models.DateTimeField(null = True)
    removed_by = models.ForeignKey('Operator', null = True, related_name = 'removed_by')
    kick = models.OneToOneField('Kick', null = True)
    
    def is_removed(self):
        return self.removal_date != None

    def is_mute(self):
        return self.mask[0] == '%'

    def __unicode__(self):
        return self.mask

    def ban_pattern(self):
        return re.compile(self.mask.replace('?','.').replace('*','.*'))
   
class Kick(Event):
    reason = models.CharField(max_length=255,null = True) 
    
    def __unicode__(self):
        return self.hostmask
    
class Mark(Event):
    reason = models.CharField(max_length=255)
    
    def __unicode__(self):
        return self.reason

class Log(models.Model):
    create_date = models.DateTimeField(auto_now_add = True)
    channel = models.CharField(max_length=255,null = True)
    text = models.TextField()

    def is_private_message(self):
        return self.channel == None

    def __unicode__(self):
        return self.text

class UserProfile(models.Model):
    user = models.ForeignKey(User, unique = True, editable=False)
    hostmask = models.CharField(max_length=255) # Will need to figure out how to lock this down, so that people cannot assign themselves *!*@*
    timezone = models.CharField(max_length=100,choices=TZ_CHOICES)
    
    def hostmask_pattern(self):
        return re.compile(self.hostmask.replace('?','.').replace('*','.*'))  
    
    def __unicode__(self):
        return self.user.username
    
class Operator(models.Model):
    user = models.ForeignKey(User, null = True)
    host = models.CharField(max_length=255, null = True)
    def __unicode__(self):
        if self.host:
            return self.host
        else:
            return ''
            
def user_post_save(sender, instance, **kwargs):
    # This automatically adds our UserProfile to newly created or saved Users
    profile, new = UserProfile.objects.get_or_create(user=instance)
models.signals.post_save.connect(user_post_save, sender=User)
    
class Comment(models.Model):
    create_date = models.DateTimeField(null=True)
    modify_date = models.DateTimeField(auto_now = True)
    author = models.ForeignKey('Operator')
    text = models.TextField()
    
    content_type = models.ForeignKey(ContentType)
    object_id = models.PositiveIntegerField()
    content_object = generic.GenericForeignKey('content_type', 'object_id')
    
    def __unicode__(self):
        return self.text