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

Identify & Remedy Duplicate Sponsors #347

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
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
106 changes: 106 additions & 0 deletions js/admin/duplicate-sponsors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import React from 'react';
import Cookies from "js-cookie";

class DuplicateSponsors extends React.Component {
constructor(props) {
super(props);
this.state = {
submitSuccess: false,
state: this.props.state,
sponsors: ['sponsorGroup0'],
matchRequests: []
}
this.handleInputChange = this.handleInputChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
this.pullFormData = this.pullFormData.bind(this);
this.addNewSponsorGroup = this.addNewSponsorGroup.bind(this);
}

handleSubmit(event) {
const csrftoken = Cookies.get("csrftoken");
const url = "/admin/people/update/duplicate_sponsors/";
const sponsorData = this.pullFormData();

fetch(url, {
method: "POST",
credentials: "same-origin",
headers: {
"Accept": "application/json",
"X-Requested-With": "XMLHttpRequest",
"X-CSRFToken": csrftoken,
},
body: JSON.stringify(sponsorData),
}).then(response => {
this.setState({submitSuccess: true});
return response.json();
}).catch(error => {
console.log(error);
})
event.preventDefault();
}

addNewSponsorGroup(event) {
event.preventDefault();
let sponsorDivs = [...this.state.sponsors];
sponsorDivs.push(`sponsorGroup${sponsorDivs.length}`);
this.setState({sponsors: sponsorDivs });
}

pullFormData() {
const requested = [];
const sponsors = [...this.state.sponsors];
sponsors.forEach((sponsor) => {
const parent = document.getElementById(sponsor);
const request = {};
parent.childNodes.forEach(child => {
const select = child.childNodes[1];
const { name, value } = select;
const sponsorName = value.split(': ')[0];
const sponsorId = value.split(': ')[1];
request[`${name}Name`] = sponsorName;
request[`${name}Id`] = sponsorId;
});
requested.push({ ...request });
});
return requested;
}

render() {
const { submitSuccess } = this.state;
const options = this.props.state_sponsors.map((sponsor) => {
const value = `${sponsor.name }: ${sponsor.id}`;
return <option value={value}>{value}</option>});
const sponsorGroups = this.state.sponsors.map((sponsor) => {
return <div className="sponsorGroup" id={sponsor}>
<label>First Sponsor:
<select className="firstSponsor" name="first">{options}</select>
</label>
<label>Second Sponsor:
<select className="secondSponsor" name="second">{options}</select>
</label>
<hr/>
</div>
});

return(
<div>
{submitSuccess ? (
<div>
<p>Sponsor edits submitted successfully.</p>
<a className="button button--primary" href="/admin/people">Back to Jurisdictions</a>
</div>
) : (
<form onSubmit={this.handleSubmit}>
{sponsorGroups}
<input type="submit" className="button--primary button" value="Submit" />
<input type="submit" className="button" value="Add Another Match" onClick={this.addNewSponsorGroup}/>
<a className="button" style={{float: 'right'}} href="/admin/people">Cancel</a>
</form>
)}

</div>
)
}
}

export default DuplicateSponsors;
2 changes: 2 additions & 0 deletions js/admin/people.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import React from "react";
import {addDataHookListener} from "../utils";
import PeopleList from "./people-list";
import NewPersonForm from "./add-person";
import DuplicateSponsors from "./duplicate-sponsors";

addDataHookListener("people-list", "context", PeopleList);
addDataHookListener("new-person", "context", NewPersonForm);
addDataHookListener("duplicate-sponsors", "context", DuplicateSponsors);
12 changes: 12 additions & 0 deletions people_admin/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
new_legislator,
apply_new_legislator,
apply_bulk_edits,
duplicate_sponsors,
apply_duplicate_sponsors,
create_delta_sets,
create_pr,
)
Expand Down Expand Up @@ -59,6 +61,16 @@
apply_bulk_edits,
name="apply_bulk_edits",
),
re_path(
r"^(?P<state>{})/duplicate_sponsors/$".format(state_abbr_pattern),
duplicate_sponsors,
name="duplicate_sponsors",
),
re_path(
r"^update/duplicate_sponsors/",
apply_duplicate_sponsors,
name="apply_duplicate_sponsors",
),
re_path(
r"^(?P<state>{})/deltas/$".format(state_abbr_pattern),
create_delta_sets,
Expand Down
46 changes: 46 additions & 0 deletions people_admin/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from django.shortcuts import render, get_object_or_404
from django.db.models import Count
from openstates.data.models import LegislativeSession, Person

from utils.common import abbr_to_jid, sessions_with_bills, states
from django.views.decorators.http import require_http_methods
from django.views.decorators.cache import never_cache
Expand Down Expand Up @@ -248,6 +249,51 @@ def apply_bulk_edits(request):
return JsonResponse({"status": "success"})


@user_passes_test(lambda u: u.has_perm(EDIT_PERM))
def duplicate_sponsors(request, state):
jid = abbr_to_jid(state)
state_sponsors = [
person_data(p)
for p in Person.objects.filter(
current_jurisdiction_id=jid, current_role__isnull=False
).order_by("family_name", "name")
]

context = {"state": state, "state_sponsors": state_sponsors}

return render(request, "people_admin/duplicate_sponsors.html", {"context": context})


@user_passes_test(lambda u: u.has_perm(EDIT_PERM))
def apply_duplicate_sponsors(request):
form_data = json.load(request)

delta = DeltaSet.objects.get_or_create(
name=f"duplicates by {request.user}",
created_by=request.user,
)

for match in form_data:
additional_id = [
"append",
"other_identifiers",
{"scheme": "openstates", "identifier": match["secondId"]},
]
additional_name = ["append", "other_names", {"name": match["secondName"]}]
PersonDelta.objects.create(
person_id=match["firstId"],
delta_set=delta,
data_changes=[additional_id, additional_name],
)

ds = DeltaSet.objects.get(id=delta, pr_status="N")
print(f"creating {ds.id} | {ds.name} | {ds.created_by}")
ds.pr_url = delta_set_to_pr(ds)
ds.pr_status = "C"
ds.save()
return JsonResponse({"status": "success"})


@never_cache
@user_passes_test(lambda u: u.has_perm(EDIT_PERM))
def create_delta_sets(request, state):
Expand Down
12 changes: 12 additions & 0 deletions templates/people_admin/duplicate_sponsors.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{% extends "public/components/base.html" %}
{% load static %}

{% block scripts %}
{{ context|json_script:"context" }}
<meta charset="utf-8">
<script src="{% static "bundles/people_admin.js" %}"></script>
{% endblock %}

{% block content %}
<div data-hook="duplicate-sponsors"></div>
{% endblock %}
2 changes: 2 additions & 0 deletions templates/people_admin/people_matcher.html
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ <h1 class="heading--large">People Matcher</h1>
<h2>{{ session|default:"All Sessions" }}</h2>
<h4>Unmatched Total: {{ unmatched_total }}</h4>
<button type="button" class="button"><a href="{% url 'create_delta_sets' state %}">Create DeltaSet</a></button>
<p>Think you see multiple sponsors available for a match?</p>
<button type="button" class="button"><a href="{% url 'duplicate_sponsors' state %}">Duplicate Sponsors Available</a></button>
</div>
<div class="cell medium-5">
</br>
Expand Down