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

Word view #141

Merged
merged 6 commits into from
Jul 27, 2018
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
2 changes: 2 additions & 0 deletions api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,6 @@
url(r'^user/token', views.user_by_token),
url(r'^similar_words/$', views.similar_words, name="similar_words"),

url(r'^word/descriptions/$', views.word_descriptions, name="api_word_descriptions"),
url(r'^word/synonyms/$', views.word_synonyms, name="api_word_synonyms")
]
28 changes: 27 additions & 1 deletion api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from rest_framework.response import Response

from language.models import Language
from word.models import Word, WordVersion
from word.models import Word, WordVersion, Description
from .permissions import IsModeratorPermission
from . import serializers

Expand Down Expand Up @@ -107,3 +107,29 @@ def similar_words(request):
else:
response = {"error": "do GET request"}
return JsonResponse(response)


@csrf_exempt
@api_view(['GET'])
@authentication_classes([])
@permission_classes([])
def word_descriptions(request):
word_id = request.GET.get("word_id")
language_str = request.GET.get("language_str")
word = Word.objects.get(id=word_id)
descs = word.desc.all().filter(language__id=language_str).values()
return JsonResponse(list(descs), safe=False)

@csrf_exempt
@api_view(['GET'])
@authentication_classes([])
@permission_classes([])
def word_synonyms(request):
word_id = request.GET.get("word_id")
language_str = request.GET.get("language_str")
version = WordVersion.objects.all().filter(language__id=language_str)
version_values_list = list(version.values())
version_id = version_values_list[0]["id"]
word = Word.objects.get(id=word_id)
syn = word.synonyms.all().filter(version__id=version_id).values()
return JsonResponse(list(syn), safe=False)
140 changes: 93 additions & 47 deletions word/templates/word/display.html
Original file line number Diff line number Diff line change
@@ -1,66 +1,72 @@
{% extends 'share/base_site.html' %}
{% load i18n %}


{% block content %}
<div class="container-fluid">
<div class="row">
<div class="col-md-4">
<div class="col-md-10">
<h1>
{{ object.word }}
{{ object.word }}&nbsp<small>({{ object.ipa }})&nbsp<i class="glyphicon glyphicon-volume-up"></i><a
href="">&nbsp{{ object.audio }}</a></small>
</h1>
<p><strong>Status: </strong>{{ object.status }}</p>
<p><strong>Tags: </strong>{% for tag in tags %}
{{ tag }}
{% endfor %}
</p>
<p><strong>Wiktionary link: </strong>{{ object.wiktionary_link }}</p>

</div>
<div class="col-md-8">
<div class="col-md-2">
<a class="button" href="{% url 'word:edit_view' object.id %}" id="suggest_translation">{% trans "Edit" %}</a>
<p>{{ object.version }}</p>
</div>
</div>

<div class="row">
<div class="col-md-12">
<h3>
{% trans "Description" %}
</h3>
{% for desc in descriptions %}
<h5>
{{ desc.language.name }}
</h5>
<p>
{{ desc.short }}
</p>
<p>
{{ desc.extended }}
</p>
{% endfor %}
<div class="col-md-6">
<div class="row">
<div class="col-md-6">
<h4><strong>{% trans "Description" %}</h4></strong>
</div>
<div class="col-md-6">
<select id="language_desc" class="btn btn-default dropdown-toggle"">
<option class="dropdown-menu" value="">Select language</option>
{% for desc in descriptions|dictsort:'language.id' %}
{% ifchanged %}
<option value="{{ desc.language.id }}">{{ desc.language.name }}</option>
{% endifchanged %}
{% endfor %}
</select>
</div>
</div>
<div id="desc"><br><br>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<table class="table">
<thead>
<tr>
<th>
{% trans "Language" %}
</th>
<th>
{% trans "Word" %}
</th>
</tr>
</thead>
<tbody>
{% for synonym in synonyms %}
<tr>
<td>
{{ synonym.version.language.name }}
</td>
<td>
<a class="{% if synonym.status == 'SUG' %}lightgrey-text{% else %}black-text{% endif %}" href="{% url 'word:word_view' synonym.id %}">
{{ synonym.word }}
</a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
<div class="col-sm-12 col-md-6">
<div class="row">
<div class="col-md-6">
<h4><strong>{% trans "Synonyms" %}</h4></strong>
</div>
<div class="col-md-6">
<select id="synonyms_language" class="btn btn-default dropdown-toggle"">
<option class="dropdown-menu" value="">Select language</option>
{% for synonym in synonyms|dictsort:'version.language.id' %}
{% ifchanged %}
<option value="{{ synonym.version.language.id }}">{{ synonym.version.language.name }}</option>
{% endifchanged %}
{% endfor %}
</select>
</div>

</div>
<div id="synonyms"><br><br>
</div>
</div>
</div>


<div id="map" style="width:100%;height:500px"></div>
</div>
{% endblock %}
Expand Down Expand Up @@ -96,6 +102,46 @@ <h5>
});
{% endfor %}
}

$(document).ready(function() {
$('#language').selectize({
create: true,
sortField: 'text'
});

$('#language_desc').click(function(){
var language_option = $('#language_desc').val();
var word_id = "{{ object.id }}"

$.getJSON('/api/word/descriptions/?word_id=' + word_id + '&language_str=' + language_option, function (result) {
var desc_short = result[0].short;
var desc_long = result[0].extended;
$( "#desc" ).empty();
$( "#desc" ).append('<p> ' + desc_short + '</p>' + '<p> ' + desc_long + '</p>');
});
});

$('#synonyms_language').click(function(){
var language_option = $('#synonyms_language').val();
var word_id = "{{ object.id }}";

$.getJSON('/api/word/synonyms/?word_id=' + word_id + '&language_str=' + language_option, function (result) {
$( "#synonyms" ).empty();

$.each(result, function(index, value) {
var synonym = value.word;
var synonym_id = value.id;
if (value.status == 'SUG') {
$( "#synonyms" ).append('<a class="lightgrey-text" href="/word/view/'+ synonym_id + '">' + synonym + '</a> ')
}
else {
$( "#synonyms" ).append('<a class="black-text" href="/word/view/'+ synonym_id + '">' + synonym + '</a> ')
}
});
});
});

});
</script>

<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyBhu-iZmNwpCjsuQyqH5p1ea69HU0xbDgo&callback=myMap&libraries=visualization">
Expand Down
1 change: 1 addition & 0 deletions word/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def get_context_data(self, **kwargs):
'descriptions': self.object.desc.all(),
'synonyms': self.object.synonyms.all(),
'locations': self.object.locations.all(),
'tags': self.object.tags.all(),
})
return context

Expand Down