-
Notifications
You must be signed in to change notification settings - Fork 10
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
#422-is-published-property #423
Changes from 4 commits
4d293a5
aec1bf8
b89e528
2fcfa37
dc2a851
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,32 @@ | ||||||||||||||||||
# Generated by Django 3.2.19 on 2023-05-15 11:53 | ||||||||||||||||||
|
||||||||||||||||||
import core.models.utils | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. F401: 'core.models.utils' imported but unused ℹ️ Expand to see all @sonatype-lift commandsYou can reply with the following commands. For example, reply with @sonatype-lift ignoreall to leave out all findings.
Note: When talking to LiftBot, you need to refresh the page to see its response. |
||||||||||||||||||
from django.db import migrations | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. E0401: Unable to import 'django.db' ℹ️ Expand to see all @sonatype-lift commandsYou can reply with the following commands. For example, reply with @sonatype-lift ignoreall to leave out all findings.
Note: When talking to LiftBot, you need to refresh the page to see its response. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. reportMissingImports: Import "django.db" could not be resolved ℹ️ Expand to see all @sonatype-lift commandsYou can reply with the following commands. For example, reply with @sonatype-lift ignoreall to leave out all findings.
Note: When talking to LiftBot, you need to refresh the page to see its response. |
||||||||||||||||||
|
||||||||||||||||||
|
||||||||||||||||||
class Migration(migrations.Migration): | ||||||||||||||||||
|
||||||||||||||||||
dependencies = [ | ||||||||||||||||||
('core', '0032_auto_20230508_1200'), | ||||||||||||||||||
] | ||||||||||||||||||
|
||||||||||||||||||
operations = [ | ||||||||||||||||||
migrations.RenameField( | ||||||||||||||||||
model_name='cohort', | ||||||||||||||||||
old_name='is_published', | ||||||||||||||||||
new_name='_is_published', | ||||||||||||||||||
), | ||||||||||||||||||
migrations.RenameField( | ||||||||||||||||||
model_name='partner', | ||||||||||||||||||
old_name='is_published', | ||||||||||||||||||
new_name='_is_published', | ||||||||||||||||||
), | ||||||||||||||||||
migrations.RemoveField( | ||||||||||||||||||
model_name='dataset', | ||||||||||||||||||
name='is_published', | ||||||||||||||||||
), | ||||||||||||||||||
migrations.RemoveField( | ||||||||||||||||||
model_name='project', | ||||||||||||||||||
name='is_published', | ||||||||||||||||||
), | ||||||||||||||||||
] |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -3,7 +3,7 @@ | |||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
from django.core.exceptions import ValidationError | ||||||||||||||||||||||||||||||
from django.db import models | ||||||||||||||||||||||||||||||
from django.db.models import Q, ObjectDoesNotExist | ||||||||||||||||||||||||||||||
from django.db.models import Q, ObjectDoesNotExist, Count | ||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. E0401: Unable to import 'django.db.models' ❗❗ 2 similar findings have been found in this PR 🔎 Expand here to view all instances of this finding
Visit the Lift Web Console to find more details in your report. ℹ️ Expand to see all @sonatype-lift commandsYou can reply with the following commands. For example, reply with @sonatype-lift ignoreall to leave out all findings.
Note: When talking to LiftBot, you need to refresh the page to see its response. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. reportMissingImports: Import "django.db.models" could not be resolved ❗❗ 2 similar findings have been found in this PR 🔎 Expand here to view all instances of this finding
Visit the Lift Web Console to find more details in your report. ℹ️ Expand to see all @sonatype-lift commandsYou can reply with the following commands. For example, reply with @sonatype-lift ignoreall to leave out all findings.
Note: When talking to LiftBot, you need to refresh the page to see its response. |
||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
from enumchoicefield import EnumChoiceField, ChoiceEnum | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
|
@@ -13,7 +13,6 @@ | |||||||||||||||||||||||||||||
from auditlog.models import AuditlogHistoryField | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
class StatusChoices(ChoiceEnum): | ||||||||||||||||||||||||||||||
precreated = "Pre-created" | ||||||||||||||||||||||||||||||
active = "Active" | ||||||||||||||||||||||||||||||
|
@@ -145,7 +144,7 @@ def __str__(self): | |||||||||||||||||||||||||||||
else: | ||||||||||||||||||||||||||||||
return f"Access ({self.status}) to dataset {self.dataset.title} given to a user: {self.user}/{self.access_notes}" | ||||||||||||||||||||||||||||||
except ObjectDoesNotExist as e: | ||||||||||||||||||||||||||||||
return f"Access ({self.status}) to dataset {self.dataset.title} given to a deleted contact or user: {self.access_notes}" | ||||||||||||||||||||||||||||||
return f"Access ({self.status}) to dataset {self.dataset.title} given to a deleted contact or user: {self.access_notes}" | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
def delete(self, force: bool = False): | ||||||||||||||||||||||||||||||
self.status = StatusChoices.terminated | ||||||||||||||||||||||||||||||
|
@@ -159,12 +158,12 @@ def display_locations(self): | |||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
@classmethod | ||||||||||||||||||||||||||||||
def find_for_user(cls, user) -> List[str]: | ||||||||||||||||||||||||||||||
accesses = cls.objects.filter(user=user, dataset__is_published=True) | ||||||||||||||||||||||||||||||
accesses = cls.objects.annotate(c=Count("dataset__exposures")).filter(user=user, c__gt=0) | ||||||||||||||||||||||||||||||
return cls._filter_expired(accesses) | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
@classmethod | ||||||||||||||||||||||||||||||
def find_for_contact(cls, contact) -> List[str]: | ||||||||||||||||||||||||||||||
accesses = cls.objects.filter(contact=contact, dataset__is_published=True) | ||||||||||||||||||||||||||||||
accesses = cls.objects.annotate(c=Count("dataset__exposures")).filter(contact=contact, c__gt=0) | ||||||||||||||||||||||||||||||
return cls._filter_expired(accesses) | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
@staticmethod | ||||||||||||||||||||||||||||||
|
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -151,7 +151,9 @@ def __str__(self): | |||||||||
return self.acronym or self.title or "undefined" | ||||||||||
|
||||||||||
|
||||||||||
|
||||||||||
@property | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. E303: too many blank lines (2) ℹ️ Expand to see all @sonatype-lift commandsYou can reply with the following commands. For example, reply with @sonatype-lift ignoreall to leave out all findings.
Note: When talking to LiftBot, you need to refresh the page to see its response. |
||||||||||
def is_published(self): | ||||||||||
return any(dataset.is_published for dataset in self.datasets.all()) | ||||||||||
|
||||||||||
def to_dict(self): | ||||||||||
contact_dicts = [] | ||||||||||
|
@@ -217,6 +219,9 @@ def serialize_to_export(self): | |||||||||
d['publications'] = ','.join(publications) | ||||||||||
return d | ||||||||||
|
||||||||||
def publish(self): | ||||||||||
pass | ||||||||||
|
||||||||||
# faster lookup for permissions | ||||||||||
# https://django-guardian.readthedocs.io/en/stable/userguide/performance.html#direct-foreign-keys | ||||||||||
class ProjectUserObjectPermission(UserObjectPermissionBase): | ||||||||||
|
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
@@ -1,3 +1,4 @@ | ||||||||||
from abc import abstractmethod | ||||||||||
from json import loads | ||||||||||
from json.decoder import JSONDecodeError | ||||||||||
|
||||||||||
|
@@ -11,19 +12,21 @@ | |||||||||
|
||||||||||
COMPANY = getattr(settings, "COMPANY", 'Company') | ||||||||||
|
||||||||||
|
||||||||||
def validate_json(value): | ||||||||||
if len(value) == 0: | ||||||||||
return value | ||||||||||
|
||||||||||
try: | ||||||||||
loads(value) | ||||||||||
if '{' not in value: # Very inaccurate, but should do the trick when the user tries to save e.g. '123' | ||||||||||
raise ValidationError(f'`scientific_metadata` field must be a valid JSON containing a dictionary!') | ||||||||||
raise ValidationError(f'`scientific_metadata` field must be a valid JSON containing a dictionary!') | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. F541: f-string is missing placeholders ℹ️ Expand to see all @sonatype-lift commandsYou can reply with the following commands. For example, reply with @sonatype-lift ignoreall to leave out all findings.
Note: When talking to LiftBot, you need to refresh the page to see its response. |
||||||||||
return value | ||||||||||
except JSONDecodeError as ex: | ||||||||||
msg = str(ex) | ||||||||||
raise ValidationError(f'`scientific_metadata` field must contain a valid JSON! ({msg})') | ||||||||||
|
||||||||||
|
||||||||||
class classproperty(property): | ||||||||||
def __get__(self, cls, owner): | ||||||||||
return self.fget.__get__(None, owner)() | ||||||||||
|
@@ -43,11 +46,6 @@ class CoreTrackedModel(CoreModel): | |||||||||
blank=True, | ||||||||||
null=True, | ||||||||||
max_length=20) | ||||||||||
|
||||||||||
is_published = models.BooleanField( | ||||||||||
default=False, | ||||||||||
blank=False, | ||||||||||
verbose_name='Is published?') | ||||||||||
|
||||||||||
scientific_metadata = models.TextField( | ||||||||||
default='{}', | ||||||||||
|
@@ -56,26 +54,17 @@ class CoreTrackedModel(CoreModel): | |||||||||
verbose_name='Additional scientific metadata (in JSON format)', | ||||||||||
validators=[validate_json] # This will work in ModelForm only | ||||||||||
) | ||||||||||
|
||||||||||
class Meta: | ||||||||||
abstract = True | ||||||||||
|
||||||||||
def publish(self, save=True): | ||||||||||
generate_id_function_path = getattr(settings, 'IDSERVICE_FUNCTION') | ||||||||||
generate_id_function = import_string(generate_id_function_path) | ||||||||||
if not self.is_published: | ||||||||||
self.is_published = True | ||||||||||
if not self.elu_accession: | ||||||||||
self.elu_accession = generate_id_function(self) | ||||||||||
if save: | ||||||||||
self.save(update_fields=['is_published', 'elu_accession']) | ||||||||||
@abstractmethod | ||||||||||
def is_published(self): | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. E0202: An attribute defined in core.models.utils line 69 hides this method ℹ️ Expand to see all @sonatype-lift commandsYou can reply with the following commands. For example, reply with @sonatype-lift ignoreall to leave out all findings.
Note: When talking to LiftBot, you need to refresh the page to see its response. |
||||||||||
pass | ||||||||||
|
||||||||||
def generate_elu_accession(self, save=True): | ||||||||||
generate_id_function_path = getattr(settings, 'IDSERVICE_FUNCTION') | ||||||||||
generate_id_function = import_string(generate_id_function_path) | ||||||||||
if not self.elu_accession: | ||||||||||
self.elu_accession = generate_id_function(self) | ||||||||||
if save: | ||||||||||
self.save(update_fields=['elu_accession']) | ||||||||||
@abstractmethod | ||||||||||
def publish(self): | ||||||||||
pass | ||||||||||
|
||||||||||
def clean(self): | ||||||||||
cleaned_data = super().clean() | ||||||||||
|
@@ -87,6 +76,36 @@ def save(self, *args, **kwargs): | |||||||||
super().save(*args, **kwargs) | ||||||||||
|
||||||||||
|
||||||||||
class CoreTrackedDBModel(CoreTrackedModel): | ||||||||||
_is_published = models.BooleanField( | ||||||||||
default=False, | ||||||||||
blank=False, | ||||||||||
verbose_name='Is published?') | ||||||||||
|
||||||||||
class Meta: | ||||||||||
abstract = True | ||||||||||
|
||||||||||
def publish(self, save=True): | ||||||||||
generate_id_function_path = getattr(settings, 'IDSERVICE_FUNCTION') | ||||||||||
generate_id_function = import_string(generate_id_function_path) | ||||||||||
if not self.is_published: | ||||||||||
self.is_published = True | ||||||||||
if not self.elu_accession: | ||||||||||
self.elu_accession = generate_id_function(self) | ||||||||||
if save: | ||||||||||
self.save(update_fields=['_is_published', 'elu_accession']) | ||||||||||
|
||||||||||
@property | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. E301: expected 1 blank line, found 0 ℹ️ Expand to see all @sonatype-lift commandsYou can reply with the following commands. For example, reply with @sonatype-lift ignoreall to leave out all findings.
Note: When talking to LiftBot, you need to refresh the page to see its response. |
||||||||||
def is_published(self): | ||||||||||
# Getter method | ||||||||||
return self._is_published | ||||||||||
|
||||||||||
@is_published.setter | ||||||||||
def is_published(self, value): | ||||||||||
# Setter method | ||||||||||
self._is_published = value | ||||||||||
|
||||||||||
|
||||||||||
class TextFieldWithInputWidget(TextField): | ||||||||||
|
||||||||||
def formfield(self, **kwargs): | ||||||||||
|
@@ -105,11 +124,10 @@ class HashedField(models.CharField): | |||||||||
A custom field that will store a hash of the provided value. | ||||||||||
""" | ||||||||||
description = "Keeps the hash of the string in the DB" | ||||||||||
|
||||||||||
def pre_save(self, model_instance, add): | ||||||||||
""" | ||||||||||
This function is called when the value is about to be saved to the DB. We hash the value and return it. | ||||||||||
""" | ||||||||||
value = getattr(model_instance, self.attname) | ||||||||||
return make_password(value, salt=settings.SECRET_KEY) | ||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Careful to change migration name and dependency once #420 is merged in develop
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done