Skip to content

Commit

Permalink
Add created_date field to HistoryMixin #143
Browse files Browse the repository at this point in the history
Signed-off-by: Jono Yang <jyang@nexb.com>
  • Loading branch information
JonoYang committed Jul 14, 2023
1 parent c639502 commit 097784b
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 9 deletions.
5 changes: 4 additions & 1 deletion minecode/model_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ def merge_packages(existing_package, new_package_data, replace=False):
# `existing_field` is a regular field on the Package model and can
# be updated normally.
setattr(existing_package, existing_field, new_value)
existing_package.last_modified_date = timezone.now()
existing_package.save()

if TRACE:
Expand Down Expand Up @@ -344,7 +345,9 @@ def merge_or_create_package(scanned_package, visit_level):
is_resolved=dependency.is_resolved,
)

created_package.last_modified_date = timezone.now()
time = timezone.now()
created_package.created_date = time
created_package.last_modified_date = time
created_package.save()
package = created_package
created = True
Expand Down
22 changes: 22 additions & 0 deletions packagedb/migrations/0072_package_created_date.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Generated by Django 4.1.2 on 2023-07-14 00:21

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("packagedb", "0071_remove_package_package_set"),
]

operations = [
migrations.AddField(
model_name="package",
name="created_date",
field=models.DateTimeField(
blank=True,
db_index=True,
help_text="Timestamp set when a Package is created",
null=True,
),
),
]
24 changes: 16 additions & 8 deletions packagedb/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def insert(self, download_url, **extra_fields):
package, created = self.get_or_create(download_url=download_url, defaults=extra_fields)
if created:
return package

def get_or_none(self, *args, **kwargs):
"""
Return the object matching the given lookup parameters, or None if no match exists.
Expand Down Expand Up @@ -87,6 +87,18 @@ class HistoryMixin(models.Model):
'History for this object a text where each line has the form: '
'"<timestamp><one space><message>". Append-only and not editable.'),
)
created_date = models.DateTimeField(
null=True,
blank=True,
db_index=True,
help_text=_('Timestamp set when a Package is created'),
)
last_modified_date = models.DateTimeField(
null=True,
blank=True,
db_index=True,
help_text=_('Timestamp set when a Package is created or modified'),
)

class Meta:
abstract = True
Expand All @@ -95,12 +107,14 @@ def append_to_history(self, message, save=False):
"""
Append the ``message`` string to the history of this object.
"""
timestamp = timezone.now().strftime("%Y-%m-%d-%H:%M:%S")
time = timezone.now()
timestamp = time.strftime("%Y-%m-%d-%H:%M:%S")
message = message.strip()
if any(lf in message for lf in ('\n' , '\r')):
raise ValueError('message cannot contain line returns (either CR or LF).')
entry = f"{timestamp} {message}\n"
self.history = self.history + entry
self.last_modified_date = time

if save:
self.save()
Expand Down Expand Up @@ -449,12 +463,6 @@ class Package(
subpath = LowerCaseField(
max_length=200,
)
last_modified_date = models.DateTimeField(
null=True,
blank=True,
db_index=True,
help_text=_('Timestamp set when a Package is created or modified'),
)
mining_level = models.PositiveIntegerField(
default=0,
help_text=_('A numeric indication of the highest depth and breadth '
Expand Down

0 comments on commit 097784b

Please sign in to comment.