Skip to content

Commit

Permalink
Merge pull request #536 from UW-GAC/feature/audit-drupal-data
Browse files Browse the repository at this point in the history
Feature/audit drupal data
  • Loading branch information
jmcarson authored Apr 12, 2024
2 parents cb62072 + d74b99b commit 7ee58e4
Show file tree
Hide file tree
Showing 16 changed files with 1,370 additions and 29 deletions.
5 changes: 5 additions & 0 deletions .env.dist
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,8 @@ DJANGO_EMAIL_PORT=
DJANGO_EMAIL_HOST_USER=
DJANGO_EMAIL_HOST_PASSWORD=
DJANGO_EMAIL_USE_TLS=

# drupal api
DRUPAL_API_CLIENT_ID=
DRUPAL_API_CLIENT_SECRET=
DRUPAL_API_REL_PATH=
10 changes: 10 additions & 0 deletions config/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,3 +403,13 @@
# Specify the subject for AnVIL account verification emails.
ANVIL_ACCOUNT_LINK_EMAIL_SUBJECT = "Verify your AnVIL account email"
ANVIL_ACCOUNT_VERIFY_NOTIFICATION_EMAIL = "[email protected]"

DRUPAL_API_CLIENT_ID = env("DRUPAL_API_CLIENT_ID", default="")
DRUPAL_API_CLIENT_SECRET = env("DRUPAL_API_CLIENT_SECRET", default="")
DRUPAL_API_REL_PATH = env("DRUPAL_API_REL_PATH", default="mockapi")
DRUPAL_DATA_AUDIT_DEACTIVATE_USERS = env(
"DRUPAL_DATA_AUDIT_DEACTIVATE_USERS", default=False
)
DRUPAL_DATA_AUDIT_REMOVE_USER_SITES = env(
"DRUPAL_DATA_AUDIT_REMOVE_USER_SITES", default=False
)
15 changes: 3 additions & 12 deletions primed/primed_anvil/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,9 @@ class StudyAdmin(SimpleHistoryAdmin):
class StudySiteAdmin(admin.ModelAdmin):
"""Admin class for the `Study` model."""

list_display = (
"short_name",
"full_name",
)
search_fields = (
"short_name",
"full_name",
)
sortable_by = (
"short_name",
"full_name",
)
list_display = ("short_name", "full_name", "drupal_node_id")
search_fields = ("short_name", "full_name", "drupal_node_id")
sortable_by = ("short_name", "full_name", "drupal_node_id")


@admin.register(models.AvailableData)
Expand Down
18 changes: 18 additions & 0 deletions primed/primed_anvil/migrations/0006_studysite_drupal_node_id.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 3.2.19 on 2023-12-06 16:21

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('primed_anvil', '0005_availabledata'),
]

operations = [
migrations.AddField(
model_name='studysite',
name='drupal_node_id',
field=models.IntegerField(blank=True, null=True),
),
]
10 changes: 7 additions & 3 deletions primed/primed_anvil/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class Study(TimeStampedModel, models.Model):
full_name = models.CharField(
max_length=255, help_text="The full name for this Study."
)

history = HistoricalRecords()

class Meta:
Expand All @@ -32,13 +33,16 @@ def get_absolute_url(self):


class StudySite(TimeStampedModel, models.Model):
"""A model to track Research Centers."""
"""A model to track Study Sites."""

short_name = models.CharField(max_length=15, unique=True)
"""The short name of the Research Center."""
"""The short name of the Study Sites."""

full_name = models.CharField(max_length=255)
"""The full name of the Research Center."""
"""The full name of the Study Sites."""

drupal_node_id = models.IntegerField(blank=True, null=True)
"""Reference node ID for entity in drupal"""

def __str__(self):
"""String method.
Expand Down
47 changes: 47 additions & 0 deletions primed/templates/users/drupal_data_audit_email.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@

{% load static i18n %}<!DOCTYPE html>
{% load render_table from django_tables2 %}

<html>
<head>
<title>Drupal Data Audit Report</title>
</head>

<div class="container">

{% block content %}

<h1>Drupal Data Audit - [applying_changes={{ apply_changes }}]</h1>
<h2>User Audit</h2>

<h3>Verified Users - {{ user_audit.verified|length }} record(s)</h3>

<h3>Needs action - {{user_audit.needs_action|length }} record(s)</h3>
{% if user_audit.needs_action %}
{% render_table user_audit.get_needs_action_table %}
{% endif %}

<h3>Errors - {{user_audit.errors|length }} record(s)</h3>
{% if user_audit.errors %}
{% render_table user_audit.get_errors_table %}
{% endif %}

<h2>Site Audit</h2>

<h3>Verified sites - {{ site_audit.verified|length }} record(s)</h3>

<h3>Sites that need action - {{site_audit.needs_action|length }} record(s)</h3>
{% if site_audit.needs_action %}
{% render_table site_audit.get_needs_action_table %}
{% endif %}

<h3>Sites with errors - {{site_audit.errors|length }} record(s)</h3>
{% if site_audit.errors %}
{% render_table site_audit.get_errors_table %}
{% endif %}


{% endblock content %}

</div>
</html>
33 changes: 20 additions & 13 deletions primed/users/adapters.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class SocialAccountAdapter(DefaultSocialAccountAdapter):
def is_open_for_signup(self, request: HttpRequest, sociallogin: Any):
return getattr(settings, "ACCOUNT_ALLOW_REGISTRATION", True)

def update_user_info(self, user, extra_data: Dict):
def update_user_info(self, user, extra_data: Dict, apply_update=True):
drupal_username = extra_data.get("preferred_username")
drupal_email = extra_data.get("email")
first_name = extra_data.get("first_name")
Expand Down Expand Up @@ -52,13 +52,15 @@ def update_user_info(self, user, extra_data: Dict):
user.email = drupal_email
user_changed = True

if user_changed is True:
if user_changed is True and apply_update is True:
user.save()
return user_changed

def update_user_study_sites(self, user, extra_data: Dict):
def update_user_study_sites(self, user, extra_data: Dict, apply_update=True):
# Get list of research centers in domain table

research_center_or_site = extra_data.get("study_site_or_center")
user_sites_updated = False
if research_center_or_site:
if not isinstance(research_center_or_site, list):
raise ImproperlyConfigured(
Expand All @@ -79,19 +81,24 @@ def update_user_study_sites(self, user, extra_data: Dict):
continue
else:
if not user.study_sites.filter(pk=rc.pk):
user.study_sites.add(rc)
logger.info(
f"[SocialAccountAdatpter:update_user_study_sites] adding user "
f"study_sites user: {user} rc: {rc}"
)
user_sites_updated = True
if apply_update is True:
user.study_sites.add(rc)
logger.info(
f"[SocialAccountAdatpter:update_user_study_sites] adding user "
f"study_sites user: {user} rc: {rc}"
)

for existing_rc in user.study_sites.all():
if existing_rc.short_name not in research_center_or_site:
user.study_sites.remove(existing_rc)
logger.info(
"[SocialAccountAdatpter:update_user_study_sites] "
f"removing study_site {existing_rc} for user {user}"
)
user_sites_updated = True
if apply_update:
user.study_sites.remove(existing_rc)
logger.info(
"[SocialAccountAdatpter:update_user_study_sites] "
f"removing study_site {existing_rc} for user {user}"
)
return user_sites_updated

def update_user_groups(self, user, extra_data: Dict):
managed_scope_status = extra_data.get("managed_scope_status")
Expand Down
Loading

0 comments on commit 7ee58e4

Please sign in to comment.