~widelands-dev/widelands-website/trunk

« back to all changes in this revision

Viewing changes to pybb/models.py

  • Committer: kaputtnik
  • Date: 2019-09-03 06:16:23 UTC
  • mfrom: (544.2.25 pybb_attachments)
  • Revision ID: kaputtnik-20190903061623-xu4kvqpabnzmuskw
Allow file uploads in the forum; added some basic file checks to validate files

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
from django.conf import settings
19
19
from notification.models import send
20
20
from check_input.models import SuspiciousInput
21
 
 
 
21
import magic
22
22
 
23
23
try:
24
24
    from notification import models as notification
345
345
    def delete(self, *args, **kwargs):
346
346
        self_id = self.id
347
347
        head_post_id = self.topic.posts.order_by('created')[0].id
 
348
 
 
349
        if self.attachments.all():
 
350
            for attach in self.attachments.all():
 
351
                attach.delete()
 
352
 
348
353
        super(Post, self).delete(*args, **kwargs)
349
354
 
350
355
        self.topic.save()
398
403
        super(Attachment, self).save(*args, **kwargs)
399
404
        if not self.hash:
400
405
            self.hash = hashlib.sha1(
401
 
                str(self.id) + settings.SECRET_KEY).hexdigest()
 
406
                bytes(self.id) + settings.SECRET_KEY.encode('utf-8')).hexdigest()
402
407
        super(Attachment, self).save(*args, **kwargs)
403
408
 
404
409
    def __str__(self):
407
412
    def get_absolute_url(self):
408
413
        return reverse('pybb_attachment', args=[self.hash])
409
414
 
410
 
    def size_display(self):
411
 
        size = self.size
412
 
        if size < 1024:
413
 
            return '%b' % size
414
 
        elif size < 1024 * 1024:
415
 
            return '%dKb' % int(size / 1024)
416
 
        else:
417
 
            return '%.2fMb' % (size / float(1024 * 1024))
418
 
 
419
415
    def get_absolute_path(self):
420
416
        return os.path.join(settings.MEDIA_ROOT, pybb_settings.ATTACHMENT_UPLOAD_TO,
421
417
                            self.path)
422
418
 
 
419
    def delete(self, *args, **kwargs):
 
420
        try:
 
421
            os.remove(self.get_absolute_path())
 
422
        except FileNotFoundError:
 
423
            pass
 
424
        super(Attachment, self).delete(*args, **kwargs)
 
425
 
423
426
 
424
427
if notification is not None:
425
428
    signals.post_save.connect(notification.handle_observations, sender=Post)