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

Associate hub with subfields #1637

Merged
merged 1 commit into from
Jun 19, 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
26 changes: 26 additions & 0 deletions src/hub/migrations/0023_hub_subfield.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Generated by Django 4.2.13 on 2024-06-17 22:06

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
("topic", "0006_alter_topic_keywords"),
("hub", "0022_alter_hub_concept"),
]

operations = [
migrations.AddField(
model_name="hub",
name="subfield",
field=models.OneToOneField(
blank=True,
null=True,
on_delete=django.db.models.deletion.SET_NULL,
related_name="hub",
to="topic.subfield",
),
),
]
18 changes: 18 additions & 0 deletions src/hub/migrations/0024_hub_is_used_for_rep.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.2.13 on 2024-06-17 22:07

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("hub", "0023_hub_subfield"),
]

operations = [
migrations.AddField(
model_name="hub",
name="is_used_for_rep",
field=models.BooleanField(default=False),
),
]
10 changes: 10 additions & 0 deletions src/hub/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,16 @@ class Hub(models.Model):
blank=True,
)

subfield = models.OneToOneField(
"topic.subfield",
related_name="hub",
on_delete=models.SET_NULL,
null=True,
blank=True,
)

is_used_for_rep = models.BooleanField(default=False)

def __str__(self):
return "{}, locked: {}".format(self.name, self.is_locked)

Expand Down
38 changes: 38 additions & 0 deletions src/topic/management/commands/associate_subfields_to_hubs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
"""
The purpose of this script is to associate hubs with subfields.
This is a prerequisite for calculating reputation since only specific
hubs that are tagged with subfields are used for reputation calculation.
"""

from django.core.management.base import BaseCommand

from hub.models import Hub
from topic.models import Subfield


class Command(BaseCommand):
help = "Associate hubs with subfields (Used for rep)"

def handle(self, *args, **kwargs):
subfields = Subfield.objects.all()

for subfield in subfields:
try:
hub = Hub.objects.all()
continue
except Hub.DoesNotExist:
hub, created = Hub.objects.get_or_create(
name=subfield.display_name.lower(),
defaults={"subfield": subfield, "is_used_for_rep": True},
)
if created:
print(
f"Created new hub {hub.name} and associated with subfield {subfield.display_name}."
)
else:
hub.subfield = subfield
hub.is_used_for_rep = True
hub.save()
print(
f"Updated hub {hub.name} with subfield {subfield.display_name}"
)