Skip to content

Commit

Permalink
Fix issues with multilanguage news
Browse files Browse the repository at this point in the history
  • Loading branch information
mikaelGusse authored and ihalaij1 committed Apr 10, 2024
1 parent 20e3b78 commit cdd023b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
30 changes: 28 additions & 2 deletions news/templates/news/list.html
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@
</a>
</td>
<td>
<a href="{{ item|url:'news-remove' }}" role="button" class="aplus-button--danger aplus-button--xs">
<button id="remove-btn-{{item.id}}" role="button" class="aplus-button--danger aplus-button--xs" value="{{item.id}}">
<span class="glyphicon glyphicon-remove" aria-hidden="true"></span>
{% translate "REMOVE" %}
</a>
</button>
</td>
</tr>
{% endfor %}
Expand All @@ -76,4 +76,30 @@
{% translate "ADD_NEWS_ITEM" %}
</a>
</p>

<script>
window.addEventListener('DOMContentLoaded', function() {
const removeBtns = document.querySelectorAll("[id^='remove-btn']");

for (let i = 0; i < removeBtns.length; i++) {
removeBtns[i].addEventListener('click', function(event) {
event.preventDefault();

const currentUrl = window.location.href;
const newsId = this.getAttribute('value');

fetch(currentUrl + newsId + '/remove/', {
method: 'POST',
headers: {
'X-CSRFToken': '{{ csrf_token }}',
},
})
.then((response) => {
window.location = currentUrl;
});
});
}
});
</script>

{% endblock %}
4 changes: 1 addition & 3 deletions news/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ class ListNewsView(CourseInstanceBaseView):
def get_common_objects(self):
super().get_common_objects()
self.news = self.instance.news.all()
print(self.news)

self.note("news")

Expand All @@ -37,7 +36,6 @@ def post(self, request, *args, **kwargs):

for new in self.news:
if str(new.id) in selected_news_ids:
print("Deleting ", new)
new.delete()

return self.redirect(self.instance.get_url("news-list"))
Expand Down Expand Up @@ -98,6 +96,6 @@ def get_resource_objects(self):
)
self.note("news_item")

def get(self, request, *args, **kwargs):
def post(self, request, *args, **kwargs):
self.news_item.delete()
return self.redirect(self.instance.get_url("news-list"))

0 comments on commit cdd023b

Please sign in to comment.