Modified: Archive sha1 to md5 hashsum field

This commit is contained in:
Stepan Zhukovsky 2023-08-08 19:05:11 +09:00
parent 3e222fb305
commit 51d1c09540
3 changed files with 29 additions and 8 deletions

View File

@ -0,0 +1,22 @@
# Generated by Django 4.2 on 2023-08-08 09:17
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('collector', '0003_alter_archive_ticket'),
]
operations = [
migrations.RenameField(
model_name='archive',
old_name='sha1',
new_name='md5',
),
migrations.RemoveField(
model_name='archive',
name='size',
),
]

View File

@ -28,8 +28,7 @@ class Archive(models.Model):
blank=True,
null=True
)
size = models.CharField(max_length=50, blank=True, editable=False)
sha1 = models.CharField(max_length=1024, editable=False)
md5 = models.CharField(max_length=1024, editable=False)
time_create = models.DateTimeField(auto_now_add=True)
time_update = models.DateTimeField(auto_now=True)
ticket = models.ForeignKey(
@ -41,12 +40,12 @@ class Archive(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
def save(self, *args, **kwargs):
# calculate sha 1 hash sum and write sha1 field to db
# calculate sha 1 hash sum and write md5 field to db
with self.file.open('rb') as f:
sha1 = hashlib.sha1()
md5 = hashlib.md5()
for byte_block in iter(lambda: f.read(4096), b""):
sha1.update(byte_block)
self.sha1 = sha1.hexdigest()
md5.update(byte_block)
self.md5 = md5.hexdigest()
# Call the "real" save() method
super().save(*args, **kwargs)

View File

@ -8,8 +8,8 @@
</small>
<small>
<br>
<b>SHA1:</b>
<span style="word-wrap: break-word">{{ archive.sha1 }}</span>
<b>MD5:</b>
<span style="word-wrap: break-word">{{ archive.md5 }}</span>
</small>
<small>
<br>