Commit 3b0102732821ceb924ad478ba7bb15c89d2e5686

Authored by Zambom
1 parent 8ce9ba8b

Adding topic update

amadeus/static/js/topics.js 0 → 100644
... ... @@ -0,0 +1,37 @@
  1 +// utilizado para fazer a re-organização dos tópicos
  2 +$("#topics-accordion").sortable({
  3 + delay: 100,
  4 + distance: 5,
  5 + handler: 'i.fa-arrows',
  6 + update: function( event, ui ) {
  7 + var cont = 1;
  8 + var data = [];
  9 +
  10 + $("#topics-accordion").find('.order_inp').each(function () {
  11 + $(this).val(cont++);
  12 +
  13 + data.push({
  14 + 'topic_id': $(this).parent().find('.id_inp').val(),
  15 + 'topic_order': $(this).val()
  16 + });
  17 + });
  18 +
  19 + data = JSON.stringify(data);
  20 +
  21 + sendUpdate(data);
  22 + },
  23 +});
  24 +
  25 +function sendUpdate(data) {
  26 + $.ajax({
  27 + url: $('.url_order').val(),
  28 + dataType: 'json',
  29 + data: {'data': data},
  30 + success: function(response) {
  31 + console.log(response);
  32 + },
  33 + error: function(response) {
  34 + console.log(response);
  35 + }
  36 + });
  37 +}
0 38 \ No newline at end of file
... ...
topics/templates/topics/list.html
... ... @@ -4,6 +4,7 @@
4 4 {% subject_permissions request.user subject as has_subject_permissions %}
5 5  
6 6 <div class="panel-group subject-group" id="topics-accordion" role="tablist" aria-multiselectable="true">
  7 + <input type="hidden" class="url_order" value="{% url 'topics:update_order' %}" />
7 8 {% for topic in subject.topic_subject.all %}
8 9 {% if not topic.repository and topic.visible or has_subject_permissions %}
9 10 <div class="panel panel-info {% if not topic.visible or topic.repository %} topic-panel-invisible {% else %} topic-panel {% endif %} topic-panel">
... ... @@ -22,8 +23,7 @@
22 23 <i class="fa fa-ellipsis-v" aria-hidden="true"></i>
23 24 </a>
24 25 <ul class="dropdown-menu pull-right" aria-labelledby="moreActions">
25   - <li><a href="{% url 'subjects:replicate' subject.slug %}"><i class="fa fa-files-o fa-fw" aria-hidden="true"></i>{% trans 'Replicate' %}</a></li>
26   - <li><a href="{% url 'subjects:update' subject.slug %}"><i class="fa fa-pencil fa-fw" aria-hidden="true"></i>{% trans 'Edit' %}</a></li>
  26 + <li><a href="{% url 'topics:update' subject.slug topic.slug %}"><i class="fa fa-pencil fa-fw" aria-hidden="true"></i>{% trans 'Edit' %}</a></li>
27 27 <li><a href="javascript:delete_subject.get('{% url 'subjects:delete' subject.slug %}?view=index','#subject','#modal_subject')"><i class="fa fa-trash fa-fw" aria-hidden="true"></i>&nbsp;{% trans 'Remove' %}</a></li>
28 28 </ul>
29 29 <a href="" ><i class="fa fa-arrows" aria-hidden="true"></i></a>
... ... @@ -41,41 +41,5 @@
41 41 {% endfor %}
42 42 </div>
43 43  
44   -<script type="text/javascript">
45   - $("#topics-accordion").sortable({ // utilizado para fazer a re-organização dos tópicos
46   - delay: 100,
47   - distance: 5,
48   - handler: 'i.fa-arrows',
49   - update: function( event, ui ) {
50   - var cont = 1;
51   - var data = [];
52   -
53   - $("#topics-accordion").find('.order_inp').each(function () {
54   - $(this).val(cont++);
55   -
56   - data.push({
57   - 'topic_id': $(this).parent().find('.id_inp').val(),
58   - 'topic_order': $(this).val()
59   - });
60   - });
61   -
62   - data = JSON.stringify(data);
63   -
64   - sendUpdate(data);
65   - },
66   - });
67   -
68   - function sendUpdate(data) {
69   - $.ajax({
70   - url: '{% url "topics:update_order" %}',
71   - dataType: 'json',
72   - data: {'data': data},
73   - success: function(response) {
74   - console.log(response);
75   - },
76   - error: function(response) {
77   - console.log(response);
78   - }
79   - });
80   - }
81   -</script>
82 44 \ No newline at end of file
  45 +<script type="text/javascript" src="{% static 'js/topics.js' %}"></script>
  46 +<script type="text/javascript" src="{% static 'js/course.js' %}"></script>
83 47 \ No newline at end of file
... ...
topics/templates/topics/update.html 0 → 100644
... ... @@ -0,0 +1,20 @@
  1 +{% extends 'subjects/view.html' %}
  2 +
  3 +{% load django_bootstrap_breadcrumbs %}
  4 +
  5 +{% block breadcrumbs %}
  6 + {{ block.super }}
  7 + {% breadcrumb 'Update Topic' 'topics:update' subject.slug topic.slug %}
  8 +{% endblock %}
  9 +
  10 +{% block content %}
  11 + <div class="card">
  12 + <div class="card-content">
  13 + <div class="card-body">
  14 + {% include 'topics/_form.html' %}
  15 + </div>
  16 + </div>
  17 + </div>
  18 + <br clear="all" />
  19 + <br clear="all" />
  20 +{% endblock %}
... ...
topics/urls.py
... ... @@ -5,5 +5,6 @@ from . import views
5 5  
6 6 urlpatterns = [
7 7 url(r'^create/(?P<slug>[\w_-]+)/$', views.CreateView.as_view(), name = 'create'),
  8 + url(r'^update/(?P<sub_slug>[\w_-]+)/(?P<slug>[\w_-]+)/$', views.UpdateView.as_view(), name = 'update'),
8 9 url(r'^update_order/$', views.update_order, name = 'update_order'),
9 10 ]
... ...
topics/views.py
... ... @@ -70,6 +70,50 @@ class CreateView(LoginRequiredMixin, generic.edit.CreateView):
70 70  
71 71 return reverse_lazy('subjects:view', kwargs = {'slug': self.object.subject.slug})
72 72  
  73 +class UpdateView(LoginRequiredMixin, generic.UpdateView):
  74 + login_url = reverse_lazy("users:login")
  75 + redirect_field_name = 'next'
  76 +
  77 + template_name = 'topics/update.html'
  78 + form_class = TopicForm
  79 + model = Topic
  80 + context_object_name = 'topic'
  81 +
  82 + def dispatch(self, request, *args, **kwargs):
  83 + slug = self.kwargs.get('sub_slug', '')
  84 + subject = get_object_or_404(Subject, slug = slug)
  85 +
  86 + if not has_subject_permissions(request.user, subject):
  87 + return redirect(reverse_lazy('subjects:home'))
  88 +
  89 + return super(UpdateView, self).dispatch(request, *args, **kwargs)
  90 +
  91 + def get_initial(self):
  92 + initial = super(UpdateView, self).get_initial()
  93 +
  94 + slug = self.kwargs.get('sub_slug', '')
  95 +
  96 + initial['subject'] = get_object_or_404(Subject, slug = slug)
  97 +
  98 + return initial
  99 +
  100 + def get_context_data(self, **kwargs):
  101 + context = super(UpdateView, self).get_context_data(**kwargs)
  102 +
  103 + context['title'] = _('Update Topic')
  104 +
  105 + slug = self.kwargs.get('sub_slug', '')
  106 + subject = get_object_or_404(Subject, slug = slug)
  107 +
  108 + context['subject'] = subject
  109 +
  110 + return context
  111 +
  112 + def get_success_url(self):
  113 + messages.success(self.request, _('Topic "%s" was updated on virtual enviroment "%s" successfully!')%(self.object.name, self.object.subject.name))
  114 +
  115 + return reverse_lazy('subjects:view', kwargs = {'slug': self.object.subject.slug})
  116 +
73 117 def update_order(request):
74 118 data = request.GET.get('data', None)
75 119  
... ...