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, blank=True,
null=True null=True
) )
size = models.CharField(max_length=50, blank=True, editable=False) md5 = models.CharField(max_length=1024, editable=False)
sha1 = models.CharField(max_length=1024, editable=False)
time_create = models.DateTimeField(auto_now_add=True) time_create = models.DateTimeField(auto_now_add=True)
time_update = models.DateTimeField(auto_now=True) time_update = models.DateTimeField(auto_now=True)
ticket = models.ForeignKey( ticket = models.ForeignKey(
@ -41,12 +40,12 @@ class Archive(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE) user = models.ForeignKey(User, on_delete=models.CASCADE)
def save(self, *args, **kwargs): 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: with self.file.open('rb') as f:
sha1 = hashlib.sha1() md5 = hashlib.md5()
for byte_block in iter(lambda: f.read(4096), b""): for byte_block in iter(lambda: f.read(4096), b""):
sha1.update(byte_block) md5.update(byte_block)
self.sha1 = sha1.hexdigest() self.md5 = md5.hexdigest()
# Call the "real" save() method # Call the "real" save() method
super().save(*args, **kwargs) super().save(*args, **kwargs)

View File

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