class RemovalReason(models.Model): """ This is a class describing messages left behind when a comment is removed, and policy for notifications. Use it when removing comments, where you want a side effect like a notification to the comment author or """ name = models.CharField(max_length=255, unique=True) organizations = models.ManyToManyField( "profiles.Organization", help_text="To which organizations will this removal reason be visible?" ) default_web_message = models.TextField( help_text="Default message to display in place of the removed comment." " If blank, no web message will be left.", blank=True) default_comment_author_message = models.TextField( help_text="Default message to send to commenters, if any." " If blank, commenters won't be notified.", blank=True) default_post_author_message = models.TextField( help_text="Default message to send to post authors, if any." " If blank, post authors won't be notified.", blank=True) objects = OrgManager() class QuerySet(OrgQuerySet): orgs = ["organizations"] def __unicode__(self): return self.name
class ReplyCode(models.Model): # Avoiding ambiguous characters like s5z2l1o0... CHARACTERS = "abcdefghijkmnpqrtuvwxy2346789" code = models.CharField(max_length=16, db_index=True, blank=True, unique=True) objects = OrgManager() class QuerySet(OrgQuerySet): orgs = [ "document__author__organization", "campaign__organizations", ] def save(self, *args, **kwargs): if not self.code: while True: self.code = self._random_string() if not ReplyCode.objects.filter(code=self.code).exists(): break super(ReplyCode, self).save(*args, **kwargs) def _random_string(self, length=4): return ''.join(random.choice(self.CHARACTERS) for i in range(length)) def to_dict(self): return { 'id': self.id, 'code': self.code, } def doc_dict(self): document = None campaign = None try: document = self.document except ObjectDoesNotExist: try: campaign = self.campaign except ObjectDoesNotExist: pass finally: if document: dd = { 'id': document.pk, 'title': unicode(document.get_title()), 'author': document.author.profile.to_dict(), 'type': document.type, 'date_written': document.date_written.isoformat(), 'url': document.get_absolute_url(), 'edit_url': document.get_edit_url(), 'comment_count': document.comments.count(), } else: dd = None if campaign: cc = { 'id': campaign.pk, 'title': unicode(campaign.title), 'public': campaign.public, 'created': campaign.created.isoformat(), 'ended': campaign.ended.isoformat() if campaign.ended else None, 'url': campaign.get_absolute_url(), } else: cc = None return { 'id': self.pk, 'code': self.code, 'document': dd, 'campaign': cc, } def __unicode__(self): return self.code
class Scan(models.Model): """ This is the raw scan with envelope and all. """ pdf = models.FileField(upload_to=TMP_UPLOAD, blank=True) author = models.ForeignKey(settings.AUTH_USER_MODEL, related_name="scans_authored", blank=True, null=True) uploader = models.ForeignKey(settings.AUTH_USER_MODEL, related_name="scans_uploaded") org = models.ForeignKey('profiles.Organization', null=True) processing_complete = models.BooleanField(default=False) under_construction = models.BooleanField(default=False) created = models.DateTimeField(default=datetime.datetime.now) modified = models.DateTimeField(default=datetime.datetime.now) # A unique ID identifying this scan by an external source (e.g. mail # scanner) which we can use to track whether a document has been loaded or # not. source_id = models.CharField(blank=True, max_length=100) objects = OrgManager() class QuerySet(OrgQuerySet): orgs = ["author__organization", "org"] def save(self, *args, **kwargs): self.modified = datetime.datetime.now() super(Scan, self).save(*args, **kwargs) def to_dict(self): try: lock = self.editlock_set.all()[0].to_dict() except IndexError: lock = None try: code = self.pendingscan.code except PendingScan.DoesNotExist: code = None return { 'id': self.pk, 'pdf': self.pdf.url, 'author': self.author.profile.to_dict() if self.author else None, 'uploader': self.uploader.profile.to_dict(), 'processing_complete': self.processing_complete, 'under_construction': self.under_construction, 'created': self.created.isoformat() if self.created else None, 'modified': self.modified.isoformat() if self.modified else None, 'pages': [p.to_dict() for p in self.scanpage_set.all()], 'url': self.get_absolute_url(), 'edit_url': self.get_edit_url(), 'lock': lock, 'pendingscan_code': code, } def get_absolute_url(self): return reverse("moderation.home") + "#/process/scan/{0}".format( self.pk) def get_edit_url(self): return self.get_absolute_url() def full_delete(self, filesonly=False): for doc in self.document_set.all(): doc.full_delete(filesonly) for page in self.scanpage_set.all(): page.full_delete(filesonly) if self.pdf: self.pdf.delete() if not filesonly: self.delete() class Meta: ordering = ['created'] permissions = (('view_raw_scans', 'View raw scans'), ) def __unicode__(self): return "(%s) %s" % (self.pk or "", self.pdf.name)