import datetime
import os
import psutil
from django.template.defaultfilters import slugify
from django.conf import settings
from bigbrother.warnings import send_warning

def get_module_list():
    default_modules = (
        'bigbrother.core.UserCount',
        'bigbrother.core.NewUsersTodayCount',
        'bigbrother.core.FreeRamCount',
        'bigbrother.core.FreeDiskCount',
    )
    return getattr(settings, 'BIGBROTHER_MODULES', default_modules)

class BigBrotherModule():
    name = 'Unamed Module'
    write_to_db = True
    prepend_text = ''
    add_text = ''
    warning_low = None
    warning_high = None
    link_url = None
    
    def get_val(self):
        return None
        
    def get_text(self):
        return '%s%g%s' % (self.prepend_text, self.get_val(), self.add_text)
        
    def get_slug(self):
        return slugify(self.name)

    def check_warning(self):
        if self.get_val() >= self.warning_high and self.warning_high != None:
            self.warn(warningtype='high')
            return True
        if self.get_val() <= self.warning_low and self.warning_low != None:
            self.warn(warningtype='low')
            return True
        return False
    
    def warn(self, warningtype):
        send_warning(module=self.__class__, warningtype=warningtype)


class UserCount(BigBrotherModule):
    name = 'Total Users'
    
    def get_val(self):
        from django.contrib.auth.models import User
        users = User.objects.all()
        return users.count()

        
class NewUsersTodayCount(BigBrotherModule):
    name = 'New Users Today'
    
    def get_val(self):
        from django.contrib.auth.models import User
        curday = datetime.datetime.today()
        users = User.objects.filter(date_joined__year=curday.year, date_joined__month=curday.month, date_joined__day=curday.day)
        return users.count()


class FreeRamCount(BigBrotherModule):
    name = 'Free RAM'
    add_text = ' MB'
    warning_low = 16

    def get_val(self):
        return psutil.phymem_usage()[2] / (1024 * 1024)

class SwapUsage(BigBrotherModule):
    name = 'Swap Usage'
    add_text = ' MB'
    warning_high = 1

    def get_val(self):
        return psutil.virtmem_usage()[1] / (1024 * 1024)

class FreeDiskCount(BigBrotherModule):
    name = 'Free Disk Space'
    add_text = ' GB'
    
    def get_val(self):
        s = os.statvfs('/')
        return round((s.f_bavail * s.f_frsize) / ( 1024 * 1024 * 1024.0 ), 1)

@@ -1382,32 +1382,178 @@
  get_val(self):%0A
+        try:%0A            from django.contrib.auth import get_user_model%0A            USER_MODEL = get_user_model()%0A        except ImportError:%0A    
         from dja
@@ -1579,32 +1579,46 @@
 dels import User
+ as USER_MODEL
 %0A        users =
@@ -1611,35 +1611,41 @@
        users = U
-ser
+SER_MODEL
 .objects.all()%0A 
@@ -1775,32 +1775,178 @@
  get_val(self):%0A
+        try:%0A            from django.contrib.auth import get_user_model%0A            USER_MODEL = get_user_model()%0A        except ImportError:%0A    
         from dja
@@ -1980,16 +1980,30 @@
 ort User
+ as USER_MODEL
 %0A       
@@ -2055,19 +2055,25 @@
 sers = U
-ser
+SER_MODEL
 .objects

Update modules to support Django 1.5 custom user models.