Skip to content
This repository was archived by the owner on Jun 1, 2022. It is now read-only.

Commit 8b9f12f

Browse files
committed
Fix topic summary citations (viewing and editing)
1 parent dfbf037 commit 8b9f12f

File tree

9 files changed

+168
-22
lines changed

9 files changed

+168
-22
lines changed

editorsnotes/admin_custom/forms/topics.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ class TopicForm(models.ModelForm):
1212
class Media:
1313
js = (
1414
"function/admin-bootstrap-topic.js",
15-
"function/admin-bootstrap-note-sections.js",
1615
)
1716
class Meta:
1817
model = Topic
@@ -28,7 +27,10 @@ class CitationForm(models.ModelForm):
2827
class Meta:
2928
model = Citation
3029
fields = ('document', 'notes', 'ordering',)
31-
widgets = {'document': forms.widgets.HiddenInput()}
30+
widgets = {
31+
'document': forms.widgets.HiddenInput(),
32+
'ordering': forms.widgets.HiddenInput()
33+
}
3234

3335
CitationFormset = generic_inlineformset_factory(
3436
Citation, form=CitationForm, extra=1)

editorsnotes/admin_custom/static/function/admin-bootstrap-base.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,14 +208,14 @@ $(document).ready(function () {
208208
.autocomplete({
209209
source: function (request, response) {
210210
$.ajax({
211-
url: '/api/topics/',
211+
url: '/api' + window.location.pathname.match(/^\/projects\/[^/]+/)[0] + '/topics/',
212212
dataType: 'json',
213213
data: {'q': request.term},
214214
beforeSend: function () {
215215
$('#related-topics-loading').css('display', 'inline-block');
216216
},
217217
success: function (data) {
218-
response($.map(data, function (item, index) {
218+
response($.map(data.results, function (item, index) {
219219
return { id: item.id, label: item.preferred_name, uri: item.uri };
220220
}));
221221
},
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,94 @@
1+
function bindCitation($citation) {
2+
var $input = $citation.find('.citation-document input')
3+
, prefix = 'id_' + $citation.prop('id')
4+
, autocompleteopts
5+
, editor = new wysihtml5.Editor(prefix + '-notes', {
6+
toolbar: $citation.prop('id') + '-toolbar',
7+
parserRules: wysihtml5ParserRules,
8+
useLineBreaks: false,
9+
stylesheets: ['/static/function/wysihtml5/stylesheet.css']
10+
});
11+
12+
$('#' + prefix + '-toolbar').show();
13+
14+
if ($input.length) {
15+
autocompleteopts= $.extend($('body').data('baseAutocompleteOptions'), {
16+
select: function (e, ui) {
17+
if (!ui.item) return;
18+
$citation.find('.citation-document').addClass('document-selected');
19+
$input.replaceWith(ui.item.value);
20+
$citation.find('#' + prefix + '-document').val(ui.item.id);
21+
}
22+
});
23+
24+
$input.autocomplete(autocompleteopts)
25+
.data('ui-autocomplete')._renderItem = function (ul, item) {
26+
return $('<li>')
27+
.data('ui-autocomplete-item', item)
28+
.append('<a>' + item.value + '</a>')
29+
.appendTo(ul)
30+
}
31+
}
32+
33+
34+
35+
}
36+
37+
function citationFormset($el) {
38+
var lastForm;
39+
40+
this.$el = $el;
41+
this.$items = $el.find('#citation-items');
42+
this.managementForm = $el.find('#id_citation-TOTAL_FORMS');
43+
this.lastForm = this.$items.children().last();
44+
this.blankForm = this.lastForm.clone();
45+
this.counter = parseInt(this.managementForm.val(), 10);
46+
47+
this.$items.children().each(function (idx, el) {
48+
bindCitation($(el));
49+
});
50+
51+
return this;
52+
}
53+
54+
citationFormset.prototype.addCitation = function () {
55+
var that = this
56+
, newForm
57+
, searchstr
58+
59+
if (this.lastForm.is(':hidden')) {
60+
this.lastForm.removeClass('hide');
61+
return
62+
}
63+
64+
newForm = this.blankForm.clone().removeClass('hide');
65+
searchstr = 'citation-' + (this.counter - 1) + '-'
66+
67+
newForm.prop('id', 'citation-' + that.counter);
68+
newForm.find('[name^="' + searchstr + '"], [id^="' + searchstr + '"]')
69+
.each(function (idx, el) {
70+
if (el.name) el.name = el.name.replace('-' + (that.counter - 1) + '-', '-' + that.counter + '-');
71+
if (el.id) el.id = el.id.replace('-' + (that.counter - 1) + '-', '-' + that.counter + '-');
72+
});
73+
74+
newForm.appendTo(that.$items);
75+
bindCitation(newForm);
76+
77+
this.counter += 1;
78+
this.managementForm.val(that.counter);
79+
}
80+
81+
182
$(document).ready(function() {
283
var editor = new wysihtml5.Editor('id_summary', {
384
toolbar: 'summary-toolbar',
485
parserRules: wysihtml5ParserRules,
586
useLineBreaks: false,
687
stylesheets: ['/static/function/wysihtml5/stylesheet.css']
788
});
89+
90+
var formset = new citationFormset($('#citations-formset'));
91+
92+
$('#add-citation').on('click', formset.addCitation.bind(formset));
93+
894
});

editorsnotes/admin_custom/static/style/bootstrap-admin.css

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,29 @@
7676
display: none;
7777
}
7878

79+
/* Hide delete field for citations */
80+
.citation input[id$="-DELETE"] {
81+
display: none;
82+
}
83+
84+
/* Topic citation spacing (DELETE ME GOD DELETE ME WHEN YOU CAN) */
85+
.citation-document i {
86+
font-size: 24px;
87+
position: relative;
88+
top: 4px;
89+
margin-right: 4px;
90+
}
91+
.citation-document input {
92+
width: 750px;
93+
}
94+
95+
.citation-document.document-selected * {
96+
display: inline-block;
97+
}
98+
#citations-formset .citation {
99+
margin: 1em 0 2em;
100+
}
101+
79102

80103
/* Transcript editor should be large */
81104
fieldset[name="transcript_content"] textarea {

editorsnotes/admin_custom/templates/includes/citation_formset.html

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,18 @@
88
{% for citation in citations_formset %}
99
{% with citation.instance as c %}
1010

11-
<div class="citation citation-edit {% if forloop.last %}hide{% endif %}" id="{{ citation.prefix }}">
11+
<div class="citation citation-edit {% if not c %}hide{% endif %}" id="{{ citation.prefix }}">
1212

1313
<div class="citation-document">
1414
<i class="icon-file"></i>
15-
{% if c.document %} {{ c.document.description|as_text }} {% endif %}
15+
{% if c.document %}
16+
{{ c.document.description|as_text }}
17+
{% else %}
18+
<input data-target-model="documents" data-project-slug="{{ project.slug }}" />
19+
{% endif %}
1620
</div>
1721

18-
<div class="citation-notes">{% if c.has_notes %}{{ c.notes|as_html }}{% endif %}</div>
19-
22+
{% include "includes/wysihtml5_full_toolbar.html" with toolbar_id=citation.prefix|add:"-toolbar" %}
2023
<div class="citation-fields">
2124
{% for field in citation %} {{ field }} {% endfor %}
2225
</div>
@@ -28,7 +31,6 @@
2831

2932
</div>
3033

31-
{% include "includes/wysihtml5_full_toolbar.html" with toolbar_id="citation-toolbar" %}
3234
{% include "includes/add_document_modal.html" %}
3335

3436
</div>

editorsnotes/admin_custom/templates/topic_admin.html

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,14 @@ <h3>{% if form.instance.id %}Change {% else %}Add {% endif %}topic</h3>
4040
</fieldset>
4141
{% endwith %}
4242

43+
{% comment %}
4344
{% with formset=formsets.topicassignment %}
4445
<fieldset name="related_topics_fields" class="related-topics">
4546
<legend>Related topics</legend>
4647
{% include "includes/bootstrap_formset.html" %}
4748
</fieldset>
4849
{% endwith %}
50+
{% endcomment %}
4951

5052
<fieldset name="article_fields">
5153
<legend>Article</legend>
@@ -56,14 +58,16 @@ <h3>{% if form.instance.id %}Change {% else %}Add {% endif %}topic</h3>
5658
</div>
5759

5860
<div class="article-citations" style="margin: 1em .5em;">
59-
<h4>Citations</h4>
61+
<legend>Citations</legend>
6062
{% include "includes/citation_formset.html" with citations_formset=formsets.citation %}
61-
<a href="#" id="add-citation">Add citation</a>
63+
<button type="button" class="btn btn-primary" id="add-citation">Add citation</a>
6264
</div>
6365

6466
</fieldset>
6567

66-
<button type="submit" class="btn btn-primary">Save</button>
68+
<hr />
69+
70+
<button type="submit" class="btn">Save</button>
6771

6872
</form>
6973
{% endblock %}

editorsnotes/admin_custom/tests.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99

1010
class TopicAdminTestCase(WebTest):
1111
fixtures = ['projects.json']
12+
def create_test_topic(self):
13+
project = main_models.Project.objects.get(slug='emma');
14+
node, topic = main_models.Topic.objects.create_along_with_node(
15+
u'Emma Goldman', project, project.members.get())
16+
return topic
1217
def test_create_topic(self):
1318
"A user should be able to create a topic with a unique name."
1419
topic_name = u'Emma Goldman'
@@ -33,6 +38,26 @@ def test_create_topic(self):
3338
self.assertEqual(error_messages[0].text.strip(),
3439
u'Topic with this preferred name already exists.')
3540

41+
def test_add_citation(self):
42+
topic = self.create_test_topic()
43+
document = main_models.Document.objects.create(
44+
description="a test document",
45+
project_id=topic.project_id,
46+
creator_id=topic.creator_id,
47+
last_updater_id=topic.last_updater_id)
48+
url = reverse('admin:main_topic_change',
49+
kwargs={'project_slug': 'emma', 'topic_node_id': topic.topic_node_id})
50+
51+
form = self.app.get(url, user='barry').forms[1]
52+
form['citation-0-document'] = document.id
53+
form['citation-0-notes'] = 'it was an interesting thing I found in here'
54+
55+
resp = form.submit().follow()
56+
topic = main_models.Topic.objects.get(id=topic.id)
57+
self.assertEqual(topic.summary_cites.count(), 1)
58+
self.assertEqual(topic.summary_cites.get().document_id, document.id)
59+
self.assertEqual(topic.summary_cites.get().object_id, topic.id)
60+
3661
def test_create_empty_topic_error(self):
3762
"A user should not be able to create a topic with an empty name."
3863
add_url = reverse('admin:main_topic_add', kwargs={'project_slug': 'emma'})
Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
{% load typogrify display %}
22

3-
<div class="citation">
4-
{% filter typogrify %}
5-
<div class="citation-document">
3+
<div class="note-section">
4+
<div class="citation-side">
65
<i class="icon-file"></i>
7-
<a href="{{ cite.document.get_absolute_url }}">{{ cite.document.as_html }}</a>
86
</div>
9-
{% endfilter %}
10-
{% if cite.has_notes %}
11-
<div class="citation-notes">
12-
{{ cite.notes|as_html }}
7+
<div class="citation-main">
8+
<div class="citation-document">
9+
<a href="{{ cite.document.get_absolute_url }}">{{ cite.document.as_html }}</a>
10+
</div>
11+
{% if cite.has_notes %}
12+
<div class="note-section-text-content">
13+
{{ cite.notes|as_html }}
14+
</div>
15+
{% endif %}
1316
</div>
14-
{% endif %}
1517
</div>

editorsnotes/static/function/base.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ $(document).ready(function () {
1717

1818
var baseAutocompleteOptions = {
1919
source: function(request, response, x) {
20-
var targetModel = this.element.attr('search-target') || this.element.data('search-target')
20+
var targetModel = this.element.attr('search-target') || this.element.data('targetModel')
2121
, query = {'q': request.term}
2222
, projectSlug = this.element.data('projectSlug')
2323
, url = '/api/'
@@ -28,6 +28,8 @@ $(document).ready(function () {
2828
}
2929
url += (targetModel + '/');
3030

31+
console.log(this.element);
32+
3133
modelMap = {
3234
topics: function (item) {
3335
var val = item.preferred_name;

0 commit comments

Comments
 (0)