Merge branch 'fix-reddit-form-urls' into 'development'
Fix reddit form urls Closes #53 See merge request sonny/newsreader!29
This commit is contained in:
commit
a3a812b949
3 changed files with 38 additions and 16 deletions
|
|
@ -1,4 +1,5 @@
|
||||||
from django import forms
|
from django import forms
|
||||||
|
from django.core.exceptions import ValidationError
|
||||||
from django.utils.safestring import mark_safe
|
from django.utils.safestring import mark_safe
|
||||||
from django.utils.translation import gettext_lazy as _
|
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.choices import RuleTypeChoices
|
||||||
from newsreader.news.collection.models import CollectionRule
|
from newsreader.news.collection.models import CollectionRule
|
||||||
|
from newsreader.news.collection.reddit import REDDIT_API_URL
|
||||||
from newsreader.news.core.models import Category
|
from newsreader.news.core.models import Category
|
||||||
|
|
||||||
|
|
||||||
def get_reddit_help_text():
|
def get_reddit_help_text():
|
||||||
return mark_safe(
|
return mark_safe(
|
||||||
"Only subreddits are supported. For example: "
|
"Only subreddits are supported"
|
||||||
"<a className='link' target='_blank' rel='noopener noreferrer'"
|
" see the 'listings' section in <a className='link' target='_blank' rel='noopener noreferrer'"
|
||||||
" href='https://reddit.com/r/aww'>https://www.reddit.com/r/aww</a>."
|
" href='https://www.reddit.com/dev/api#section_listings'>the reddit API docs</a>."
|
||||||
" Note that subreddit urls should NOT include 'www' because Reddit is picky."
|
" 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
|
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):
|
def save(self, commit=True):
|
||||||
instance = super().save(commit=False)
|
instance = super().save(commit=False)
|
||||||
|
|
||||||
instance.type = RuleTypeChoices.subreddit
|
instance.type = RuleTypeChoices.subreddit
|
||||||
instance.timezone = str(pytz.utc)
|
instance.timezone = str(pytz.utc)
|
||||||
instance.user = self.user
|
|
||||||
|
|
||||||
if not instance.url.endswith(".json"):
|
|
||||||
instance.url = f"{instance.url}.json"
|
|
||||||
|
|
||||||
if commit:
|
if commit:
|
||||||
instance.save()
|
instance.save()
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,12 @@
|
||||||
from django.test import TestCase
|
from django.test import TestCase
|
||||||
from django.urls import reverse
|
from django.urls import reverse
|
||||||
|
from django.utils.translation import gettext as _
|
||||||
|
|
||||||
import pytz
|
import pytz
|
||||||
|
|
||||||
from newsreader.news.collection.choices import RuleTypeChoices
|
from newsreader.news.collection.choices import RuleTypeChoices
|
||||||
from newsreader.news.collection.models import CollectionRule
|
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.factories import SubredditFactory
|
||||||
from newsreader.news.collection.tests.views.base import CollectionRuleViewTestCase
|
from newsreader.news.collection.tests.views.base import CollectionRuleViewTestCase
|
||||||
from newsreader.news.core.tests.factories import CategoryFactory
|
from newsreader.news.core.tests.factories import CategoryFactory
|
||||||
|
|
@ -17,7 +18,7 @@ class SubRedditCreateViewTestCase(CollectionRuleViewTestCase, TestCase):
|
||||||
|
|
||||||
self.form_data = {
|
self.form_data = {
|
||||||
"name": "new rule",
|
"name": "new rule",
|
||||||
"url": "https://www.reddit.com/r/aww",
|
"url": f"{REDDIT_API_URL}/r/aww",
|
||||||
"category": str(self.category.pk),
|
"category": str(self.category.pk),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -31,12 +32,19 @@ class SubRedditCreateViewTestCase(CollectionRuleViewTestCase, TestCase):
|
||||||
rule = CollectionRule.objects.get(name="new rule")
|
rule = CollectionRule.objects.get(name="new rule")
|
||||||
|
|
||||||
self.assertEquals(rule.type, RuleTypeChoices.subreddit)
|
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.timezone, str(pytz.utc))
|
||||||
self.assertEquals(rule.favicon, None)
|
self.assertEquals(rule.favicon, None)
|
||||||
self.assertEquals(rule.category.pk, self.category.pk)
|
self.assertEquals(rule.category.pk, self.category.pk)
|
||||||
self.assertEquals(rule.user.pk, self.user.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):
|
class SubRedditUpdateViewTestCase(CollectionRuleViewTestCase, TestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
|
|
@ -44,7 +52,7 @@ class SubRedditUpdateViewTestCase(CollectionRuleViewTestCase, TestCase):
|
||||||
|
|
||||||
self.rule = SubredditFactory(
|
self.rule = SubredditFactory(
|
||||||
name="Python",
|
name="Python",
|
||||||
url=f"{REDDIT_URL}/r/python.json",
|
url=f"{REDDIT_API_URL}/r/python.json",
|
||||||
user=self.user,
|
user=self.user,
|
||||||
category=self.category,
|
category=self.category,
|
||||||
type=RuleTypeChoices.subreddit,
|
type=RuleTypeChoices.subreddit,
|
||||||
|
|
@ -97,7 +105,7 @@ class SubRedditUpdateViewTestCase(CollectionRuleViewTestCase, TestCase):
|
||||||
self.assertEquals(response.status_code, 404)
|
self.assertEquals(response.status_code, 404)
|
||||||
|
|
||||||
def test_url_change(self):
|
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)
|
response = self.client.post(self.url, self.form_data)
|
||||||
|
|
||||||
|
|
@ -106,8 +114,15 @@ class SubRedditUpdateViewTestCase(CollectionRuleViewTestCase, TestCase):
|
||||||
rule = CollectionRule.objects.get(name="aww")
|
rule = CollectionRule.objects.get(name="aww")
|
||||||
|
|
||||||
self.assertEquals(rule.type, RuleTypeChoices.subreddit)
|
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.timezone, str(pytz.utc))
|
||||||
self.assertEquals(rule.favicon, None)
|
self.assertEquals(rule.favicon, None)
|
||||||
self.assertEquals(rule.category.pk, self.category.pk)
|
self.assertEquals(rule.category.pk, self.category.pk)
|
||||||
self.assertEquals(rule.user.pk, self.user.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"))
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,7 @@ import factory.fuzzy
|
||||||
import pytz
|
import pytz
|
||||||
|
|
||||||
from newsreader.accounts.tests.factories import UserFactory
|
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
|
from newsreader.news.core.models import Category, Post
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -38,7 +38,7 @@ class FeedPostFactory(PostFactory):
|
||||||
|
|
||||||
|
|
||||||
class RedditPostFactory(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(
|
rule = factory.SubFactory(
|
||||||
"newsreader.news.collection.tests.factories.SubredditFactory"
|
"newsreader.news.collection.tests.factories.SubredditFactory"
|
||||||
)
|
)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue