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

Unpublish feature #89

Merged
merged 2 commits into from
Jun 17, 2024
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
141 changes: 40 additions & 101 deletions qgisfeedproject/qgisfeed/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,21 @@ class FeedsItemFormTestCase(TestCase):
fixtures = ['qgisfeed.json', 'users.json']
def setUp(self):
self.client = Client()
spatial_filter = Polygon(((0, 0), (0, 1), (1, 1), (1, 0), (0, 0)))
image_path = join(settings.MEDIA_ROOT, "feedimages", "rust.png")
self.post_data = {
'title': 'QGIS core will be rewritten in Rust',
'image': open(image_path, "rb"),
'content': '<p>Tired with C++ intricacies, the core developers have decided to rewrite QGIS in <strong>Rust</strong>',
'url': 'https://www.null.com',
'sticky': False,
'sorting': 0,
'language_filter': 'en',
'spatial_filter': str(spatial_filter),
'publish_from': '2023-10-18 14:46:00+00',
'publish_to': '2023-10-29 14:46:00+00'
}


def test_authenticated_user_access(self):
self.client.login(username='admin', password='admin')
Expand Down Expand Up @@ -469,96 +484,35 @@ def test_nonstaff_user_redirect_to_login(self):
def test_authenticated_user_add_feed(self):
# Add a feed entry test
self.client.login(username='staff', password='staff')
spatial_filter = Polygon(((0, 0), (0, 1), (1, 1), (1, 0), (0, 0)))
image_path = join(settings.MEDIA_ROOT, "feedimages", "rust.png")

post_data = {
'title': 'QGIS core will be rewritten in Rust',
'image': open(image_path, "rb"),
'content': '<p>Tired with C++ intricacies, the core developers have decided to rewrite QGIS in <strong>Rust</strong>',
'url': 'https://www.null.com',
'sticky': False,
'sorting': 0,
'language_filter': 'en',
'spatial_filter': str(spatial_filter),
'publish_from': '2023-10-18 14:46:00+00',
'publish_to': '2023-10-29 14:46:00+00'
}

response = self.client.post(reverse('feed_entry_add'), data=post_data)
response = self.client.post(reverse('feed_entry_add'), data=self.post_data)
self.assertEqual(response.status_code, 302)
self.assertRedirects(response, reverse('feeds_list'))


def test_authenticated_user_update_feed(self):
# Update a feed entry test
self.client.login(username='admin', password='admin')
spatial_filter = Polygon([(0, 0), (0, 1), (1, 1), (1, 0), (0, 0)])
image_path = join(settings.MEDIA_ROOT, "feedimages", "rust.png")

post_data = {
'title': 'QGIS core will be rewritten in Rust',
'image': open(image_path, "rb"),
'content': '<p>Tired with C++ intricacies, the core developers have decided to rewrite QGIS in <strong>Rust</strong>',
'url': 'https://www.null.com',
'sticky': False,
'sorting': 0,
'language_filter': 'en',
'spatial_filter': str(spatial_filter),
'publish_from': '2023-10-18 14:46:00+00',
'publish_to': '2023-10-29 14:46:00+00'
}

response = self.client.post(reverse('feed_entry_update', args=[3]), data=post_data)
response = self.client.post(reverse('feed_entry_update', args=[3]), data=self.post_data)
self.assertEqual(response.status_code, 302)
self.assertRedirects(response, reverse('feeds_list'))


def test_not_allowed_user_update_feed(self):
# Update a feed entry with a non allowed user
self.client.login(username='staff', password='staff')
spatial_filter = Polygon([(0, 0), (0, 1), (1, 1), (1, 0), (0, 0)])
image_path = join(settings.MEDIA_ROOT, "feedimages", "rust.png")

post_data = {
'title': 'QGIS core will be rewritten in Rust',
'image': open(image_path, "rb"),
'content': '<p>Tired with C++ intricacies, the core developers have decided to rewrite QGIS in <strong>Rust</strong>',
'url': 'https://www.null.com',
'sticky': False,
'sorting': 0,
'language_filter': 'en',
'spatial_filter': str(spatial_filter),
'publish_from': '2023-10-18 14:46:00+00',
'publish_to': '2023-10-29 14:46:00+00',
}

response = self.client.post(reverse('feed_entry_update', args=[7]), data=post_data)
response = self.client.post(reverse('feed_entry_update', args=[7]), data=self.post_data)
self.assertEqual(response.status_code, 302)
self.assertRedirects(response, reverse('login') + '?next=' + reverse('feed_entry_update', args=[7]))
self.assertIsNone(response.context)

def test_allowed_user_publish_feed(self):
# Publish a feed entry test
self.client.login(username='admin', password='admin')
spatial_filter = Polygon([(0, 0), (0, 1), (1, 1), (1, 0), (0, 0)])
image_path = join(settings.MEDIA_ROOT, "feedimages", "rust.png")

post_data = {
'title': 'QGIS core will be rewritten in Rust',
'image': open(image_path, "rb"),
'content': '<p>Tired with C++ intricacies, the core developers have decided to rewrite QGIS in <strong>Rust</strong>',
'url': 'https://www.null.com',
'sticky': False,
'sorting': 0,
'language_filter': 'en',
'spatial_filter': str(spatial_filter),
'publish_from': '2023-10-18 14:46:00+00',
'publish_to': '2023-10-29 14:46:00+00',
'publish': 'true'
}

response = self.client.post(reverse('feed_entry_update', args=[7]), data=post_data)
self.post_data['publish'] = 1
response = self.client.post(reverse('feed_entry_update', args=[7]), data=self.post_data)
self.assertEqual(response.status_code, 302)
self.assertRedirects(response, reverse('feeds_list'))

Expand All @@ -573,30 +527,30 @@ def test_allowed_staff_publish_feed(self):
group.user_set.add(user)

self.client.login(username='staff', password='staff')
spatial_filter = Polygon([(0, 0), (0, 1), (1, 1), (1, 0), (0, 0)])
image_path = join(settings.MEDIA_ROOT, "feedimages", "rust.png")

post_data = {
'title': 'QGIS core will be rewritten in Rust',
'image': open(image_path, "rb"),
'content': '<p>Tired with C++ intricacies, the core developers have decided to rewrite QGIS in <strong>Rust</strong>',
'url': 'https://www.null.com',
'sticky': False,
'sorting': 0,
'language_filter': 'en',
'spatial_filter': str(spatial_filter),
'publish_from': '2023-10-18 14:46:00+00',
'publish_to': '2023-10-29 14:46:00+00',
'publish': 'true'
}

response = self.client.post(reverse('feed_entry_update', args=[7]), data=post_data)
self.post_data['publish'] = 1
response = self.client.post(reverse('feed_entry_update', args=[7]), data=self.post_data)
self.assertEqual(response.status_code, 302)
self.assertRedirects(response, reverse('feeds_list'))

updated_data = QgisFeedEntry.objects.get(pk=7)
self.assertTrue(updated_data.published)

def test_allowed_staff_unpublish_feed(self):
# Update a feed entry with an allowed staff user
user = User.objects.get(username='staff')
user.save()
group = Group.objects.get(name='qgisfeedentry_approver')
group.user_set.add(user)

self.client.login(username='staff', password='staff')
self.post_data['publish'] = 0

response = self.client.post(reverse('feed_entry_update', args=[7]), data=self.post_data)
self.assertEqual(response.status_code, 302)
self.assertRedirects(response, reverse('feeds_list'))

updated_data = QgisFeedEntry.objects.get(pk=7)
self.assertFalse(updated_data.published)

def test_authenticated_user_add_invalid_data(self):
# Add a feed entry that contains invalid data
Expand Down Expand Up @@ -643,24 +597,9 @@ def test_get_field_max_length(self):
def test_add_feed_with_reviewer(self):
# Add a feed entry with specified reviewer test
self.client.login(username='staff', password='staff')
spatial_filter = Polygon(((0, 0), (0, 1), (1, 1), (1, 0), (0, 0)))
image_path = join(settings.MEDIA_ROOT, "feedimages", "rust.png")

post_data = {
'title': 'QGIS core will be rewritten in Rust',
'image': open(image_path, "rb"),
'content': '<p>Tired with C++ intricacies, the core developers have decided to rewrite QGIS in <strong>Rust</strong>',
'url': 'https://www.null.com',
'sticky': False,
'sorting': 0,
'language_filter': 'en',
'spatial_filter': str(spatial_filter),
'publish_from': '2023-10-18 14:46:00+00',
'publish_to': '2023-10-29 14:46:00+00',
'reviewers': [1]
}
self.post_data['reviewers'] = [1]

response = self.client.post(reverse('feed_entry_add'), data=post_data)
response = self.client.post(reverse('feed_entry_add'), data=self.post_data)
self.assertEqual(response.status_code, 302)
self.assertRedirects(response, reverse('feeds_list'))

Expand Down
10 changes: 6 additions & 4 deletions qgisfeedproject/qgisfeed/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,12 +372,14 @@ def post(self, request, pk):
user = request.user
user_is_approver = user.has_perm("qgisfeed.publish_qgisfeedentry")
form = self.form_class(request.POST, request.FILES, instance=feed_entry)

if form.is_valid():
instance = form.save(commit=False)
publish = bool(request.POST.get('publish', ''))
if publish and user_is_approver:
instance.published = True
if request.POST.get('publish') and user_is_approver:
try:
publish = bool(int(request.POST.get('publish')))
instance.published = publish
except ValueError:
pass

instance.save()
success = True
Expand Down
21 changes: 21 additions & 0 deletions qgisfeedproject/templates/feeds/confirmation_modal.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<div id="confirmation-modal" class="modal">
<div class="modal-background"></div>
<div class="modal-card">
<header class="modal-card-head">
<p class="modal-card-title">Unpublish Feed Entry</p>
<button class="delete" aria-label="close" type="button"></button>
</header>
<section class="modal-card-body">
Are you sure you want to unpublish this feed item?
</section>
<footer class="modal-card-foot is-justify-content-flex-end">
<button class="button is-danger" type="submit" form="feedItemForm" name="publish" value="0">
<span>Confirm</span>
</button>
<button class="button is-default" type="button">
<span>Cancel</span>
</button>

</footer>
</div>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
</button>

{% elif not published and user_is_approver and form.instance.pk%}
<button class="button is-success" type="submit" form="feedItemForm" name="publish" value="true">
<button class="button is-success" type="submit" form="feedItemForm" name="publish" value="1">
<span class="icon">
<i class="fa fa-check" aria-hidden="true"></i>
</span>
Expand Down
24 changes: 24 additions & 0 deletions qgisfeedproject/templates/feeds/feed_item_form_widgets.html
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,29 @@
{% endfor %}
</div>
</div>

{% if published and user_is_approver and form.instance.pk %}
<div class="columns">
<div class="column is-6 field">
<label class="label">
Unpublish this entry:
</label>
<button
name="formConfirmationBtn"
class="button is-block is-danger is-fullwidth js-modal-trigger"
type="button"
form="feedItemForm"
data-target="confirmation-modal"
>
<span class="icon">
<i class="fa fa-unlink" aria-hidden="true"></i>
</span>
<span>Unpublish</span>
</button>
</div>
</div>
{% endif %}

{% if not form.instance.pk %}
<div class="columns">
<div class="column is-12 field">
Expand All @@ -210,4 +233,5 @@
<p class="form-error">{{ error }}</p>
{% endfor %}
{% include 'feeds/set_spatial_filter_modal.html' %}
{% include 'feeds/confirmation_modal.html' %}
</form>
Loading