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

#422-is-published-property #423

Merged
merged 5 commits into from
May 17, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
32 changes: 32 additions & 0 deletions core/migrations/0033_auto_20230515_1353.py
Copy link
Collaborator

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

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

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
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

9% of developers fix this issue

F401: 'core.models.utils' imported but unused


ℹ️ Expand to see all @sonatype-lift commands

You can reply with the following commands. For example, reply with @sonatype-lift ignoreall to leave out all findings.

Command Usage
@sonatype-lift ignore Leave out the above finding from this PR
@sonatype-lift ignoreall Leave out all the existing findings from this PR
@sonatype-lift exclude <file|issue|path|tool> Exclude specified file|issue|path|tool from Lift findings by updating your config.toml file

Note: When talking to LiftBot, you need to refresh the page to see its response.
Click here to add LiftBot to another repo.

from django.db import migrations
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

9% of developers fix this issue

E0401: Unable to import 'django.db'


ℹ️ Expand to see all @sonatype-lift commands

You can reply with the following commands. For example, reply with @sonatype-lift ignoreall to leave out all findings.

Command Usage
@sonatype-lift ignore Leave out the above finding from this PR
@sonatype-lift ignoreall Leave out all the existing findings from this PR
@sonatype-lift exclude <file|issue|path|tool> Exclude specified file|issue|path|tool from Lift findings by updating your config.toml file

Note: When talking to LiftBot, you need to refresh the page to see its response.
Click here to add LiftBot to another repo.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

10% of developers fix this issue

reportMissingImports: Import "django.db" could not be resolved


ℹ️ Expand to see all @sonatype-lift commands

You can reply with the following commands. For example, reply with @sonatype-lift ignoreall to leave out all findings.

Command Usage
@sonatype-lift ignore Leave out the above finding from this PR
@sonatype-lift ignoreall Leave out all the existing findings from this PR
@sonatype-lift exclude <file|issue|path|tool> Exclude specified file|issue|path|tool from Lift findings by updating your config.toml file

Note: When talking to LiftBot, you need to refresh the page to see its response.
Click here to add LiftBot to another repo.



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',
),
]
4 changes: 2 additions & 2 deletions core/models/cohort.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from django.db import models
from django.conf import settings

from .utils import CoreTrackedModel, TextFieldWithInputWidget
from .utils import CoreTrackedDBModel, TextFieldWithInputWidget


class Cohort(CoreTrackedModel):
class Cohort(CoreTrackedDBModel):
class Meta:
app_label = 'core'
get_latest_by = "added"
Expand Down
5 changes: 5 additions & 0 deletions core/models/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ class AppMeta:
verbose_name='Sensitivity class',
help_text='Sensitivity denotes the security classification of this dataset.')

@property
def is_published(self):
exposures_list = self.exposures.all()
return len(exposures_list) > 0

@property
def data_types(self):
all_data_types = set()
Expand Down
4 changes: 2 additions & 2 deletions core/models/partner.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from django_countries.fields import CountryField

from model_utils import Choices
from .utils import CoreTrackedModel, TextFieldWithInputWidget
from .utils import CoreTrackedDBModel, TextFieldWithInputWidget

from elixir_daisy import settings

Expand All @@ -21,7 +21,7 @@
)


class Partner(CoreTrackedModel):
class Partner(CoreTrackedDBModel):
"""
Represents a partner.
{
Expand Down
4 changes: 3 additions & 1 deletion core/models/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,9 @@ def __str__(self):
return self.acronym or self.title or "undefined"



@property
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

23% of developers fix this issue

E303: too many blank lines (2)


ℹ️ Expand to see all @sonatype-lift commands

You can reply with the following commands. For example, reply with @sonatype-lift ignoreall to leave out all findings.

Command Usage
@sonatype-lift ignore Leave out the above finding from this PR
@sonatype-lift ignoreall Leave out all the existing findings from this PR
@sonatype-lift exclude <file|issue|path|tool> Exclude specified file|issue|path|tool from Lift findings by updating your config.toml file

Note: When talking to LiftBot, you need to refresh the page to see its response.
Click here to add LiftBot to another repo.

def is_published(self):
return any(dataset.is_published for dataset in self.datasets.all())

def to_dict(self):
contact_dicts = []
Expand Down
37 changes: 29 additions & 8 deletions core/models/utils.py
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

Expand All @@ -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!')
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

32% of developers fix this issue

F541: f-string is missing placeholders


ℹ️ Expand to see all @sonatype-lift commands

You can reply with the following commands. For example, reply with @sonatype-lift ignoreall to leave out all findings.

Command Usage
@sonatype-lift ignore Leave out the above finding from this PR
@sonatype-lift ignoreall Leave out all the existing findings from this PR
@sonatype-lift exclude <file|issue|path|tool> Exclude specified file|issue|path|tool from Lift findings by updating your config.toml file

Note: When talking to LiftBot, you need to refresh the page to see its response.
Click here to add LiftBot to another repo.

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)()
Expand All @@ -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='{}',
Expand All @@ -56,9 +54,14 @@ class CoreTrackedModel(CoreModel):
verbose_name='Additional scientific metadata (in JSON format)',
validators=[validate_json] # This will work in ModelForm only
)

class Meta:
abstract = True

@abstractmethod
def is_published(self):
Copy link

Choose a reason for hiding this comment

The 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 commands

You can reply with the following commands. For example, reply with @sonatype-lift ignoreall to leave out all findings.

Command Usage
@sonatype-lift ignore Leave out the above finding from this PR
@sonatype-lift ignoreall Leave out all the existing findings from this PR
@sonatype-lift exclude <file|issue|path|tool> Exclude specified file|issue|path|tool from Lift findings by updating your config.toml file

Note: When talking to LiftBot, you need to refresh the page to see its response.
Click here to add LiftBot to another repo.

pass

def publish(self, save=True):
generate_id_function_path = getattr(settings, 'IDSERVICE_FUNCTION')
generate_id_function = import_string(generate_id_function_path)
Expand Down Expand Up @@ -87,6 +90,25 @@ 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
@property
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

13% of developers fix this issue

E301: expected 1 blank line, found 0


ℹ️ Expand to see all @sonatype-lift commands

You can reply with the following commands. For example, reply with @sonatype-lift ignoreall to leave out all findings.

Command Usage
@sonatype-lift ignore Leave out the above finding from this PR
@sonatype-lift ignoreall Leave out all the existing findings from this PR
@sonatype-lift exclude <file|issue|path|tool> Exclude specified file|issue|path|tool from Lift findings by updating your config.toml file

Note: When talking to LiftBot, you need to refresh the page to see its response.
Click here to add LiftBot to another repo.

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):
Expand All @@ -105,11 +127,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)

1 change: 1 addition & 0 deletions elixir_daisy/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@
'consent_status',
'data_types',
'deidentification_method',
'is_published',
),
'contract': (
'contacts',
Expand Down
1 change: 1 addition & 0 deletions web/templates/search/_items/datasets.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ <h2 class="card-title">
<a href="{% url 'dataset' pk=dataset.pk %}">{{ dataset.title }}</a>
</h2>
<ul class="card-text">
<li><strong>Published:</strong> {{ dataset.is_published | yesno }}</li>
<li><strong>Datatypes:</strong>
{% for datatype in dataset.data_types %}
<span class="badge badge-pill badge-secondary">{{ datatype }}</span>
Expand Down