web/ldt_utils/ldt/models.py
changeset 4 7c994c98d1df
parent 3 651f67b66c51
equal deleted inserted replaced
3:651f67b66c51 4:7c994c98d1df
       
     1 from django.db import models
       
     2 from django.conf import settings
       
     3 from ldt.core.models import Document, Owner
       
     4 from django.utils.translation import ugettext_lazy as _
       
     5 from utils import create_ldt, copy_ldt
       
     6 import uuid
       
     7 
       
     8 class Author(models.Model):
       
     9 
       
    10     handle = models.CharField(max_length=512, unique=True, blank=True, null=True)
       
    11     email = models.EmailField(unique=False, blank=True, null=True)
       
    12     firstname = models.CharField(max_length=512, blank=True, null=True)
       
    13     lastname = models.CharField(max_length=512, blank=True, null=True)
       
    14     
       
    15     def __unicode__(self):
       
    16         return unicode(self.id) + " - " + self.handle + ", " + self.email + ", " + self.firstname + " " + self.lastname
       
    17 
       
    18 
       
    19 class Content(models.Model):
       
    20     iri_id = models.CharField(max_length=1024, unique=True)
       
    21     iriurl = models.URLField()
       
    22     videopath = models.URLField(null=True, blank=True)
       
    23     creation_date = models.DateTimeField(auto_now_add=True)
       
    24     update_date = models.DateTimeField(auto_now=True)
       
    25     title = models.CharField(max_length=1024, null=True, blank=True)
       
    26     description = models.TextField(null=True, blank=True)
       
    27     external_id = models.CharField(max_length=1024, null=True, blank=True)
       
    28     authors = models.ManyToManyField(Author)
       
    29     duration = models.IntegerField(null=True, blank=True)
       
    30 
       
    31     def get_duration(self):
       
    32         if self.duration is None:
       
    33             doc = xml.dom.minidom.parse(self.iri_file_path())
       
    34             doc = Ft.Xml.Domlette.ConvertDocument(doc)        
       
    35             con = xml.xpath.Context.Context(doc, 1, 1, None)
       
    36             res = xml.xpath.Evaluate("/iri/body/medias/media[@id='video']/video", context=con)
       
    37             self.duration = int(res[0].getAttributeNS(None, 'dur'))
       
    38             self.save()
       
    39         return self.duration
       
    40     
       
    41     def delete(self):
       
    42         super(Content, self).delete()
       
    43         writer = lucene.IndexWriter(STORE, ANALYZER, True, lucene.IndexWriter.MaxFieldLength.UNLIMITED)
       
    44         writer.deleteDocuments(lucene.Term("iri_id", self.iri_id))
       
    45         writer.commit() 
       
    46     
       
    47     def __unicode__(self):
       
    48         return str(self.id) + ": " + self.iri_id
       
    49         
       
    50     def iri_url(self, web_url=settings.WEB_URL):
       
    51         if 'http' in self.iriurl or 'https' in self.iriurl:
       
    52             return self.iriurl
       
    53         else:
       
    54             return unicode(web_url) + unicode(settings.MEDIA_URL)+u"media/ldt/"+unicode(self.iriurl)
       
    55         
       
    56         
       
    57 class Project(Document):  
       
    58     STATE_CHOICES=(
       
    59     (1, 'edition'),
       
    60     (2, 'published'),
       
    61     (3, 'moderated'),
       
    62     (4, 'rejected'),
       
    63     (5, 'deleted')
       
    64     )
       
    65     ldt_id = models.CharField(max_length=1024, unique=True)
       
    66     ldt = models.TextField(null=True)
       
    67     title = models.CharField(max_length=1024)
       
    68     contents = models.ManyToManyField(Content)
       
    69     creation_date = models.DateTimeField(auto_now_add=True)
       
    70     modification_date = models.DateTimeField(auto_now=True)
       
    71     created_by = models.CharField(_("created by"), max_length=70)
       
    72     changed_by = models.CharField(_("changed by"), max_length=70)
       
    73     state = models.IntegerField(choices=STATE_CHOICES, default=1)
       
    74     
       
    75     def __unicode__(self):
       
    76         return unicode(self.id) + u": " + unicode(self.ldt_id)
       
    77 
       
    78     @staticmethod
       
    79     def create_project(user, title, contents):
       
    80         owner = Owner.objects.get(user=user)
       
    81         project = Project(title=title, owner=owner)
       
    82         project.ldt_id = str(uuid.uuid1())
       
    83         project.created_by=user.username
       
    84         project.changed_by=user.username
       
    85         project.state = 1
       
    86         project.save()
       
    87         for content in contents:
       
    88             project.contents.add(content)
       
    89         project.save()
       
    90         return create_ldt(project, user)
       
    91 
       
    92     def copy_project(self, user, title):
       
    93         owner = Owner.objects.get(user=user)
       
    94         project = Project(title=title, owner=owner)
       
    95         project = copy_ldt(self, project, user)
       
    96         project.save()
       
    97         for content in self.contents.all():
       
    98             project.contents.add(content)
       
    99         project.save()
       
   100         return project
       
   101 
       
   102 class Segment(models.Model):
       
   103     
       
   104     project_obj = models.ForeignKey(Project, null=True)
       
   105     content = models.ForeignKey(Content)
       
   106     project_id = models.CharField(max_length=1024, unique=False, blank=True, null=True)
       
   107     iri_id = models.CharField(max_length=1024, unique=False)
       
   108     ensemble_id = models.CharField(max_length=1024, unique=False)
       
   109     cutting_id = models.CharField(max_length=1024, unique=False)    
       
   110     element_id = models.CharField(max_length=1024, unique=False)
       
   111     tags = models.CharField(max_length=2048, unique=False, null=True, blank=True)  
       
   112     title = models.CharField(max_length=2048, unique=False, null=True, blank=True)
       
   113     duration = models.IntegerField(null=True)
       
   114     start_ts = models.IntegerField(null=True)
       
   115     author = models.CharField(max_length=1024, unique=False, null=True, blank=True)
       
   116     date = models.CharField(max_length=128, unique=False, null=True, blank=True)
       
   117     abstract = models.TextField(null=True, blank=True)
       
   118     
       
   119     def __unicode__(self):
       
   120         return "/".join((unicode(self.project_id), unicode(self.iri_id), unicode(self.ensemble_id), unicode(self.cutting_id), unicode(self.element_id)))
       
   121     
       
   122     class Meta:
       
   123         unique_together = (('project_id', 'iri_id', 'ensemble_id', 'cutting_id', 'element_id'),)
       
   124