Skip to content

Commit

Permalink
Merge branches '77-council-ror-download', '188-update-council-import-…
Browse files Browse the repository at this point in the history
…scripts' and '159-prev-years-ror-answer' into staging
  • Loading branch information
struan committed Oct 7, 2024
4 parents e02a8d5 + 03de62d + 9691bae + edc86e0 commit 8b54b05
Show file tree
Hide file tree
Showing 16 changed files with 850 additions and 49 deletions.
5 changes: 5 additions & 0 deletions ceuk-marking/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@
rightofreply.AuthorityRORSectionQuestions.as_view(),
name="authority_ror",
),
path(
"authorities/<name>/ror/download/",
rightofreply.AuthorityRORCSVView.as_view(),
name="authority_ror_download",
),
path(
"authority_ror_authorities/",
rightofreply.AuthorityRORList.as_view(),
Expand Down
48 changes: 44 additions & 4 deletions crowdsourcer/fixtures/ror_responses.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"option": 181,
"response_type": 2,
"public_notes": "",
"page_number": "0",
"page_number": "",
"evidence": "",
"private_notes": "",
"agree_with_response": true,
Expand All @@ -29,9 +29,9 @@
"user": 3,
"option": 191,
"response_type": 2,
"public_notes": "",
"page_number": "0",
"evidence": "",
"public_notes": "http://example.org/",
"page_number": "20",
"evidence": "We do not agree for reasons",
"private_notes": "a council objection",
"agree_with_response": false,
"revision_type": null,
Expand All @@ -40,5 +40,45 @@
"last_update": "2023-03-15T17:22:10+0000",
"multi_option": []
}
},
{
"model": "crowdsourcer.response",
"pk": 101,
"fields": {
"authority": 2,
"question": 272,
"user": 2,
"option": 181,
"response_type": 1,
"public_notes": "a public note",
"page_number": "0",
"evidence": "",
"private_notes": "a private note",
"revision_type": null,
"revision_notes": null,
"created": "2023-03-15T17:22:10+0000",
"last_update": "2023-03-15T17:22:10+0000",
"multi_option": []
}
},
{
"model": "crowdsourcer.response",
"pk": 102,
"fields": {
"authority": 2,
"question": 273,
"user": 2,
"option": 6,
"response_type": 1,
"public_notes": "a public note",
"page_number": "0",
"evidence": "",
"private_notes": "a private note",
"revision_type": null,
"revision_notes": null,
"created": "2023-03-15T17:22:10+0000",
"last_update": "2023-03-15T17:22:10+0000",
"multi_option": []
}
}
]
1 change: 1 addition & 0 deletions crowdsourcer/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ def __init__(self, *args, **kwargs):

self.authority_obj = self.initial.get("authority", None)
self.question_obj = self.initial.get("question", None)
self.previous_response = self.initial.get("previous_response", None)
self.orig = self.initial.get("original_response", None)

def clean(self):
Expand Down
63 changes: 57 additions & 6 deletions crowdsourcer/management/commands/import_councils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@

import pandas as pd

from crowdsourcer.models import Assigned, Marker, PublicAuthority, ResponseType
from crowdsourcer.models import (
Assigned,
Marker,
MarkingSession,
PublicAuthority,
ResponseType,
)

YELLOW = "\033[33m"
RED = "\033[31m"
Expand All @@ -22,13 +28,20 @@ def add_arguments(self, parser):
"-q", "--quiet", action="store_true", help="Silence progress bars."
)

parser.add_argument(
"--session",
action="store",
required=True,
help="Marking session to use assignments with",
)

parser.add_argument(
"--add_users", action="store_true", help="add users to database"
)

parser.add_argument("--council_list", help="file to import data from")

def handle(self, quiet: bool = False, *args, **options):
def handle(self, quiet: bool = False, session: str = None, *args, **options):
if options.get("council_list") is not None:
self.council_file = options["council_list"]

Expand All @@ -43,6 +56,7 @@ def handle(self, quiet: bool = False, *args, **options):
],
)

session = MarkingSession.objects.get(label=session)
rt = ResponseType.objects.get(type="Right of Reply")
for index, row in df.iterrows():
if pd.isna(row["email"]) or pd.isna(row["gssNumber"]):
Expand All @@ -63,23 +77,56 @@ def handle(self, quiet: bool = False, *args, **options):
continue

if Marker.objects.filter(authority=council).exists():
self.stdout.write(f"user already exists for council: {row['council']}")
continue
m = Marker.objects.get(authority=council)

if (
m.user.email == row["email"]
and m.marking_session.filter(pk=session.pk).exists()
):
self.stdout.write(
f"user already exists for council: {row['council']}"
)
continue

if User.objects.filter(username=row["email"]).exists():
u = User.objects.get(username=row["email"])
if u.marker.authority is not None and u.marker.authority != council:
if (
u.marker.authority == council
and not u.marker.marking_session.filter(pk=session.pk).exists()
):
u.marker.marking_session.set([session])
self.stdout.write(
f"updating marker to current session: {row['email']} ({council}, {u.marker.authority}"
)
elif (
u.marker.authority is None
and not Assigned.objects.filter(
user=u, authority=council, marking_session=session
).exists()
):
self.stdout.write(
f"updating marker to council: {row['email']} ({council}, {u.marker.authority}"
)
if options["add_users"]:
u.marker.authority = council
u.marker.save()
u.marker.marking_session.set([session])
elif u.marker.authority is not None and u.marker.authority != council:
self.stdout.write(
f"dual email for councils: {row['email']} ({council}, {u.marker.authority}"
)
if options["add_users"]:
for c in [council, u.marker.authority]:
if options["add_users"]:
a, _ = Assigned.objects.update_or_create(
user=u, authority=c
user=u,
authority=c,
marking_session=session,
)
u.marker.authority = None
u.marker.send_welcome_email = True
u.marker.save()
u.marker.marking_session.set([session])
continue
self.stdout.write(f"user already exists for email: {row['email']}")
continue
Expand All @@ -98,4 +145,8 @@ def handle(self, quiet: bool = False, *args, **options):
user=u,
authority=council,
response_type=rt,
defaults={
"send_welcome_email": True,
},
)
m.marking_session.set([session])
23 changes: 18 additions & 5 deletions crowdsourcer/management/commands/send_welcome_emails.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ def add_arguments(self, parser):
parser.add_argument("--send_emails", action="store_true", help="Send emails")

parser.add_argument(
"--stage", action="store", help="Only send emails to people in this stage"
"--stage",
required=True,
action="store",
help="Use template for this stage and only send emails to people in this stage",
)

parser.add_argument(
Expand All @@ -42,6 +45,16 @@ def get_config(self, session):

return None

def get_templates(self, config, user, stage="First Mark"):
if config.get(stage):
config = config[stage]

template = config["new_user_template"]
if user.password != "":
template = config["previous_user_template"]

return (template, config["subject_template"])

def handle(self, *args, **kwargs):
if not kwargs["send_emails"]:
self.stdout.write(
Expand Down Expand Up @@ -94,12 +107,12 @@ def handle(self, *args, **kwargs):
if user.email:
self.stdout.write(f"Sending email for to this email: {user.email}")
if kwargs["send_emails"]:
template = config["new_user_template"]
template, subject_template = self.get_templates(
config, user, kwargs["stage"]
)
if user.password == "":
user.set_password(get_random_string(length=20))
user.save()
else:
template = config["previous_user_template"]

form = PasswordResetForm({"email": user.email})
assert form.is_valid()
Expand All @@ -111,7 +124,7 @@ def handle(self, *args, **kwargs):
domain_override=config["server_name"],
use_https=True,
from_email=config["from_email"],
subject_template_name=config["subject_template"],
subject_template_name=subject_template,
email_template_name=template,
)
marker.send_welcome_email = False
Expand Down
Loading

0 comments on commit 8b54b05

Please sign in to comment.