Source

django-live / live / models.py

Full commit
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright 2010 Nicolás Echániz
# All rights reserved.
#
# This file is part of django-live.
#
# Django-Live is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Django-Live is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

from django.db import models
from django.utils.translation import ugettext_lazy as _
from django.db.utils import DatabaseError
from django.contrib.auth.models import User
from django.contrib.sessions.models import Session

from autoslug.fields import AutoSlugField

class Participant(models.Model):
    """Represents each participant in a chat channel."""
    name = models.CharField(_('name'), max_length=250,
                             db_index=True, blank=False, unique=True)
    slug = AutoSlugField(populate_from='name', unique=True,
                         db_index=True, always_update=True)
    user = models.ForeignKey(User, null=True, blank=True)
    session = models.ForeignKey(Session, editable=False) #, unique=True)
    
    def __unicode__(self):
        return self.name


class Channel(models.Model):
    """A Chat Channel"""
    name = models.CharField(_('name'), max_length=250,
                             db_index=True, blank=False, unique=True)
    slug = AutoSlugField(populate_from='name', unique=True,
                         db_index=True, always_update=True)
    # for password protected Channel (not yet implemented)
    password = models.CharField(_('password'), max_length=100, blank=True)
    # for members only Channel (not yet implemented)
    members = models.ManyToManyField(User, db_index=True, null=True, blank=True)
    participants = models.ManyToManyField(Participant, db_index=True,
                                          related_name='channels', editable=False,
                                          null=True, blank=True)
    allow_guests = models.BooleanField(_('allow guests'), default=True)
    # for conversation logging (not implemented yet)
    save_log = models.BooleanField(_('save conversation log'), default=False)
    persistant = models.BooleanField(_('pertistant'), default=False)

    def __unicode__(self):
        return self.name


## when the process starts we delete all participants data that may be around from a previous run
try:
    Participant.objects.all().delete()
# this will fail when the database has not been created yet (before sync_db)
except DatabaseError:
    pass

## when the process starts we delete all channels that are not persistant
try:
    Channel.objects.filter(persistant=False).delete()
# this will fail when the database has not been created yet (before sync_db)
except DatabaseError:
    pass