Skip to content

Commit d348d9c

Browse files
NotFish232alanzhu0
authored andcommitted
feat(polls): add option to import from csv
1 parent 60e9d92 commit d348d9c

File tree

5 files changed

+104
-0
lines changed

5 files changed

+104
-0
lines changed

Ion.egg-info/SOURCES.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1154,6 +1154,7 @@ intranet/static/js/vendor/jquery-1.10.2.min.js
11541154
intranet/static/js/vendor/jquery-1.10.2.min.map
11551155
intranet/static/js/vendor/jquery.are-you-sure.js
11561156
intranet/static/js/vendor/jquery.cookie.js
1157+
intranet/static/js/vendor/jquery.csv.min.js
11571158
intranet/static/js/vendor/jquery.formset.js
11581159
intranet/static/js/vendor/jquery.overscroll.min.js
11591160
intranet/static/js/vendor/jquery.scrollto.min.js

intranet/static/css/polls.form.scss

+22
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,28 @@
88
padding-bottom: 10px;
99
}
1010

11+
#import_from_csv {
12+
width: 150px;
13+
}
14+
15+
#csv_wrapper {
16+
padding-top: 10px;
17+
padding-bottom: 10px;
18+
}
19+
20+
#csv_input {
21+
display: none;
22+
width: 0;
23+
height: 0;
24+
}
25+
26+
#csv_error {
27+
padding-left: 10px;
28+
width: 50px;
29+
color: red;
30+
text-decoration: none;
31+
}
32+
1133
#questions {
1234
.question {
1335
position: relative;

intranet/static/js/polls.js

+68
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,74 @@ $(function() {
5858
addInline(v);
5959
});
6060

61+
$("#import_from_csv").click(async function (e) {
62+
e.preventDefault();
63+
$("#csv_input").click();
64+
});
65+
66+
$("#csv_input").on("change", function (e) {
67+
e.preventDefault();
68+
let file = $("#csv_input").prop("files")[0];
69+
let file_reader = new FileReader();
70+
file_reader.onload = async function (e) {
71+
// csv should have the following headers
72+
// name, position, platform, slogan
73+
let csv_data = $.csv.toArrays(file_reader.result);
74+
if (csv_data.length === 0) {
75+
$("#csv_error").text("Empty CSV");
76+
return;
77+
}
78+
79+
let required_labels = ["position", "name", "platform", "slogan"]
80+
let labels = csv_data[0].map(l => l.toLowerCase());
81+
if (!required_labels.every(l => labels.includes(l))) {
82+
let missing_labels = required_labels.filter(l => !labels.includes(l))
83+
$("#csv_error").text(`Missing required label(s): ${missing_labels.join(", ")}`)
84+
return;
85+
}
86+
$("#csv_error").text("");
87+
88+
let content = csv_data.slice(1);
89+
let map = {}; // (position) -> (name, platform, slogan)
90+
for (let line of content) {
91+
let position = line[labels.indexOf("position")];
92+
let name = line[labels.indexOf("name")];
93+
let platform = line[labels.indexOf("platform")];
94+
let slogan = line[labels.indexOf("slogan")];
95+
96+
if (!(position in map)) {
97+
map[position] = [];
98+
}
99+
map[position].push([name, platform, slogan]);
100+
}
101+
102+
let sleep = async () => { await new Promise(resolve => setTimeout(resolve, 0)); };
103+
104+
for (let [position, choices] of Object.entries(map)) {
105+
$("#add_question").click();
106+
let question_element = $("#questions .question:last-child");
107+
await sleep();
108+
109+
// set position, type, and max choices of question
110+
question_element.find(".text").html(position);
111+
question_element.find(".type").data('selectize').setValue("RAN");
112+
question_element.find(".max").val(`${Math.min(choices.length, 3)}`);
113+
114+
for (let choice of choices) {
115+
let [name, platform, slogan] = choice;
116+
117+
question_element.find(".add_choice").click();
118+
await sleep();
119+
120+
let text_field = question_element.find(".choices .choice:last-child .info");
121+
let text = $(`<a href="${platform}">${name} | ${slogan}</a>`)
122+
text_field.html(text);
123+
}
124+
}
125+
};
126+
file_reader.readAsText(file);
127+
});
128+
61129
$("#poll-form").submit(function() {
62130
var out = [];
63131
$("#questions .question").each(function() {

intranet/static/js/vendor/jquery.csv.min.js

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

intranet/templates/polls/add_modify.html

+12
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
<script src="{% static 'vendor/datetimepicker-2.4.5/jquery.datetimepicker.js' %}"></script>
1414
<script src="{% static 'vendor/selectize.js-0.12.4/dist/js/standalone/selectize.min.js' %}"></script>
1515
<script src="{% static 'js/vendor/underscore-min.js' %}"></script>
16+
<script src="{% static 'js/vendor/jquery.csv.min.js' %}"></script>
1617
<script src="{% static 'js/polls.js' %}"></script>
1718
<script>
1819
var poll_questions = $.parseJSON("{{ poll_questions|escapejs }}");
@@ -112,6 +113,17 @@ <h2 style="padding-left:0">
112113
<button id="add_question"><i class="fas fa-plus"></i> Add Question</button>
113114
</td>
114115
</tr>
116+
<tr id="csv_wrapper">
117+
<td>From CSV:</td>
118+
<td>
119+
<button id="import_from_csv">
120+
<i class="fas fa-plus"></i>
121+
Load from CSV
122+
</button>
123+
<a id="csv_error"></a>
124+
</td>
125+
<input id="csv_input" type="file"/>
126+
</tr>
115127
<tr><td>&nbsp;</td><td>
116128
<input type="submit" style="width:200px">
117129
{% if poll.id %}

0 commit comments

Comments
 (0)