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

188 update council import scripts #193

Merged
merged 2 commits into from
Oct 8, 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
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
5 changes: 5 additions & 0 deletions crowdsourcer/tests/data/council_new_joint_users.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
,firstName,surname,council,councilInternalName,gssNumber,email,official-name
0,Armagh,Staff,"Armagh, Banbridge and Craigavon","Armagh City, Banbridge and Craigavon Borough Council",N09000002,armagh@example.org,"Armagh City, Banbridge and Craigavon Borough Council"
1,Aberdeen,Staff,Aberdeenshire,Aberdeenshire Council,S12000034,aberdeenshire@example.org,Aberdeenshire Council
2,Aberdeen,Staff,Aberdeen City,Aberdeen City Council,S12000033,aberdeenshire@example.org,Aberdeen City Council
3,Adur,Staff,Adur,Adur District Council,E07000223,adur@example.org,Adur District Council
5 changes: 5 additions & 0 deletions crowdsourcer/tests/data/council_new_users.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
,firstName,surname,council,councilInternalName,gssNumber,email,official-name
0,Armagh,Staff,"Armagh, Banbridge and Craigavon","Armagh City, Banbridge and Craigavon Borough Council",N09000002,armagh@example.org,"Armagh City, Banbridge and Craigavon Borough Council"
1,Aberdeen,Staff,Aberdeenshire,Aberdeenshire Council,S12000034,aberdeen@example.org,Aberdeenshire Council
2,Aberdeen,Staff,Aberdeen City,Aberdeen City Council,S12000033,aberdeen@example.org,Aberdeen City Council
3,Adur,Staff,Adur,Adur District Council,E07000223,new_adur@example.org,Adur District Council
5 changes: 5 additions & 0 deletions crowdsourcer/tests/data/council_split_users.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
,firstName,surname,council,councilInternalName,gssNumber,email,official-name
0,Armagh,Staff,"Armagh, Banbridge and Craigavon","Armagh City, Banbridge and Craigavon Borough Council",N09000002,armagh@example.org,"Armagh City, Banbridge and Craigavon Borough Council"
1,Aberdeen,Staff,Aberdeenshire,Aberdeenshire Council,S12000034,aberdeenshire@example.org,Aberdeenshire Council
2,Aberdeen,Staff,Aberdeen City,Aberdeen City Council,S12000033,aberdeen@example.org,Aberdeen City Council
3,Adur,Staff,Adur,Adur District Council,E07000223,adur@example.org,Adur District Council
Loading
Loading