Skip to content
Snippets Groups Projects
models.py 44.2 KiB
Newer Older
    t_end_time           = models.IntegerField(null=True)
    t_end_time_ns        = models.IntegerField(null=True)
    v_end_time           = models.IntegerField(null=True)
    v_end_time_ns        = models.IntegerField(null=True)
    eff_dist_g           = models.FloatField(null=True)
    eff_dist_h           = models.FloatField(null=True)
    eff_dist_l           = models.FloatField(null=True)
    eff_dist_t           = models.FloatField(null=True)
    eff_dist_v           = models.FloatField(null=True)
    # Additional desired attributes that are not in the SimInspiral table
    source_channel       = models.CharField(max_length=50, blank=True, default="")
    destination_channel  = models.CharField(max_length=50, blank=True, default="")

    @classmethod
    def field_names(cls):
        try:
            return cls._field_names
        except AttributeError: pass
        # We only care about the model field names in this particular case.
        cls._field_names = [ x.name for x in cls._meta.fields ]
        return cls._field_names
## Tags (user-defined log message attributes)
class Tag(models.Model):
    # XXX Does the tag need to have a submitter column?
    # No, because creating a tag will generate a log message.
    # For the same reason, a timstamp is not necessary.
    eventlogs   = models.ManyToManyField(EventLog)
    name        = models.CharField(max_length=100)
    displayName = models.CharField(max_length=200,null=True)
    def __unicode__(self):
        if self.displayName:
            return self.displayName
        else:
            return self.name

#     def getEvents(self):
#         # XXX Any way of doing this with filters?
#         # We would need to filter for a non-null intersection of the 
#         # set of log messages in the event with the set of log 
#         # messages in the tag.
#         eventlist = [log.event for log in self.eventlogs.all()]
class VOEvent(models.Model):
    class Meta:
        ordering = ['-created','-N']
        unique_together = ("event","N")
    # Now N will be the serial number.
    event = models.ForeignKey(Event, null=False)
    created = models.DateTimeField(auto_now_add=True)
    issuer = models.ForeignKey(DjangoUser)
    ivorn = models.CharField(max_length=200, default="")
    filename = models.CharField(max_length=100, default="")
    file_version = models.IntegerField(null=True)
    N = models.IntegerField(null=False)
    VOEVENT_TYPE_CHOICES = (('PR','preliminary'), ('IN','initial'), ('UP','update'), ('RE', 'retraction'),)
    voevent_type = models.CharField(max_length=2, choices=VOEVENT_TYPE_CHOICES)

    def fileurl(self):
        if self.filename:
            actual_filename = self.filename
            if self.file_version >= 0:
                actual_filename += ',%d' % self.file_version
            return reverse('file', args=[self.event.graceid(), actual_filename])
        else:
            return None

    def save(self, *args, **kwargs):
        success = False
        # XXX filename must not be 'None' because null=False for the filename
        # field above.
        self.filename = self.filename or ""
        attempts = 0
        while (not success and attempts < 5):
            attempts = attempts + 1
            if not self.N:
                if self.event.voevent_set.count():
                    self.N = int(self.event.voevent_set.aggregate(models.Max('N'))['N__max']) + 1
                else:
                    self.N = 1
            try:
                super(VOEvent, self).save(*args, **kwargs)
                success = True
            except IntegrityError:
                # IntegrityError means an attempt to insert a duplicate
                # key or to violate a foreignkey constraint.
                # We are under race conditions.  Let's try again.
                pass

        if not success:
            # XXX Should this be a custom exception?  That way we could catch it
            # in the views that use it and give an informative error message.
            raise Exception("Too many attempts to save log message. Something is wrong.")