Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add hash field, function to calculate hash, and a migration #571

Merged
merged 1 commit into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions sde_collections/migrations/0040_candidateurl_hash.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Generated by Django 5.0.1 on 2024-01-31 20:01

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("sde_collections", "0039_includepattern"),
]

operations = [
migrations.AddField(
model_name="candidateurl",
name="hash",
field=models.CharField(
blank=True, default="", max_length=32, verbose_name="Hash"
),
),
]
12 changes: 12 additions & 0 deletions sde_collections/models/candidate_url.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import hashlib
from urllib.parse import urlparse

from django.db import models
Expand Down Expand Up @@ -30,6 +31,7 @@ class CandidateURL(models.Model):
Collection, on_delete=models.CASCADE, related_name="candidate_urls"
)
url = models.CharField("URL")
hash = models.CharField("Hash", max_length=32, blank=True, default="")
scraped_title = models.CharField(
"Scraped Title",
default="",
Expand Down Expand Up @@ -109,3 +111,13 @@ def path(self) -> str:

def __str__(self) -> str:
return self.url

def save(self, *args, **kwargs):
# Generate the hash based on the model values
hash_string = f"{self.url}{self.generated_title}{self.document_type}"
hash_value = hashlib.md5(hash_string.encode()).hexdigest()

# Set the hash value
self.hash = hash_value

super().save(*args, **kwargs)