Fix reddit form urls

This commit is contained in:
sonny 2020-07-22 23:29:23 +02:00
parent 0f5e9e7fca
commit 9a212c6288
3 changed files with 38 additions and 16 deletions

View file

@ -1,4 +1,5 @@
from django import forms
from django.core.exceptions import ValidationError
from django.utils.safestring import mark_safe
from django.utils.translation import gettext_lazy as _
@ -6,15 +7,17 @@ import pytz
from newsreader.news.collection.choices import RuleTypeChoices
from newsreader.news.collection.models import CollectionRule
from newsreader.news.collection.reddit import REDDIT_API_URL
from newsreader.news.core.models import Category
def get_reddit_help_text():
return mark_safe(
"Only subreddits are supported. For example: "
"<a className='link' target='_blank' rel='noopener noreferrer'"
" href='https://reddit.com/r/aww'>https://www.reddit.com/r/aww</a>."
" Note that subreddit urls should NOT include 'www' because Reddit is picky."
"Only subreddits are supported"
" see the 'listings' section in <a className='link' target='_blank' rel='noopener noreferrer'"
" href='https://www.reddit.com/dev/api#section_listings'>the reddit API docs</a>."
" For example: <a className='link' target='_blank' rel='noopener noreferrer'"
" href='https://oauth.reddit.com/r/aww'>https://oauth.reddit.com/r/aww</a>"
)
@ -65,15 +68,19 @@ class SubRedditRuleForm(CollectionRuleForm):
timezone = None
def clean_url(self):
url = self.cleaned_data["url"]
if not url.startswith(REDDIT_API_URL):
raise ValidationError(_("This does not look like an Reddit API URL"))
return url
def save(self, commit=True):
instance = super().save(commit=False)
instance.type = RuleTypeChoices.subreddit
instance.timezone = str(pytz.utc)
instance.user = self.user
if not instance.url.endswith(".json"):
instance.url = f"{instance.url}.json"
if commit:
instance.save()

View file

@ -1,11 +1,12 @@
from django.test import TestCase
from django.urls import reverse
from django.utils.translation import gettext as _
import pytz
from newsreader.news.collection.choices import RuleTypeChoices
from newsreader.news.collection.models import CollectionRule
from newsreader.news.collection.reddit import REDDIT_URL
from newsreader.news.collection.reddit import REDDIT_API_URL, REDDIT_URL
from newsreader.news.collection.tests.factories import SubredditFactory
from newsreader.news.collection.tests.views.base import CollectionRuleViewTestCase
from newsreader.news.core.tests.factories import CategoryFactory
@ -17,7 +18,7 @@ class SubRedditCreateViewTestCase(CollectionRuleViewTestCase, TestCase):
self.form_data = {
"name": "new rule",
"url": "https://www.reddit.com/r/aww",
"url": f"{REDDIT_API_URL}/r/aww",
"category": str(self.category.pk),
}
@ -31,12 +32,19 @@ class SubRedditCreateViewTestCase(CollectionRuleViewTestCase, TestCase):
rule = CollectionRule.objects.get(name="new rule")
self.assertEquals(rule.type, RuleTypeChoices.subreddit)
self.assertEquals(rule.url, "https://www.reddit.com/r/aww.json")
self.assertEquals(rule.url, f"{REDDIT_API_URL}/r/aww")
self.assertEquals(rule.timezone, str(pytz.utc))
self.assertEquals(rule.favicon, None)
self.assertEquals(rule.category.pk, self.category.pk)
self.assertEquals(rule.user.pk, self.user.pk)
def test_regular_reddit_url(self):
self.form_data.update(url=f"{REDDIT_URL}/r/aww")
response = self.client.post(self.url, self.form_data)
self.assertContains(response, _("This does not look like an Reddit API URL"))
class SubRedditUpdateViewTestCase(CollectionRuleViewTestCase, TestCase):
def setUp(self):
@ -44,7 +52,7 @@ class SubRedditUpdateViewTestCase(CollectionRuleViewTestCase, TestCase):
self.rule = SubredditFactory(
name="Python",
url=f"{REDDIT_URL}/r/python.json",
url=f"{REDDIT_API_URL}/r/python.json",
user=self.user,
category=self.category,
type=RuleTypeChoices.subreddit,
@ -97,7 +105,7 @@ class SubRedditUpdateViewTestCase(CollectionRuleViewTestCase, TestCase):
self.assertEquals(response.status_code, 404)
def test_url_change(self):
self.form_data.update(name="aww", url=f"{REDDIT_URL}/r/aww")
self.form_data.update(name="aww", url=f"{REDDIT_API_URL}/r/aww")
response = self.client.post(self.url, self.form_data)
@ -106,8 +114,15 @@ class SubRedditUpdateViewTestCase(CollectionRuleViewTestCase, TestCase):
rule = CollectionRule.objects.get(name="aww")
self.assertEquals(rule.type, RuleTypeChoices.subreddit)
self.assertEquals(rule.url, f"{REDDIT_URL}/r/aww.json")
self.assertEquals(rule.url, f"{REDDIT_API_URL}/r/aww")
self.assertEquals(rule.timezone, str(pytz.utc))
self.assertEquals(rule.favicon, None)
self.assertEquals(rule.category.pk, self.category.pk)
self.assertEquals(rule.user.pk, self.user.pk)
def test_regular_reddit_url(self):
self.form_data.update(url=f"{REDDIT_URL}/r/aww")
response = self.client.post(self.url, self.form_data)
self.assertContains(response, _("This does not look like an Reddit API URL"))

View file

@ -3,7 +3,7 @@ import factory.fuzzy
import pytz
from newsreader.accounts.tests.factories import UserFactory
from newsreader.news.collection.reddit import REDDIT_URL
from newsreader.news.collection.reddit import REDDIT_API_URL
from newsreader.news.core.models import Category, Post
@ -38,7 +38,7 @@ class FeedPostFactory(PostFactory):
class RedditPostFactory(PostFactory):
url = factory.fuzzy.FuzzyText(length=10, prefix=f"{REDDIT_URL}/")
url = factory.fuzzy.FuzzyText(length=10, prefix=f"{REDDIT_API_URL}/")
rule = factory.SubFactory(
"newsreader.news.collection.tests.factories.SubredditFactory"
)