Skip to content

Commit

Permalink
Merge pull request #909 from ubyssey/optimize-article-list
Browse files Browse the repository at this point in the history
add articleListSerialzier
  • Loading branch information
Rowansdabomb authored Mar 2, 2019
2 parents 9c04870 + c1d1624 commit ce89cf3
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 7 deletions.
12 changes: 12 additions & 0 deletions dispatch/api/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ def __init__(self, *args, **kwargs):
super(DispatchModelSerializer, self).__init__(*args, **kwargs)

self.hide_authenticated_fields()
self.exclude_fields()

def is_authenticated(self):
return (self.context.get('request') and
Expand All @@ -102,6 +103,17 @@ def hide_authenticated_fields(self):
if not self.is_authenticated():
for field in authenticated_fields:
self.fields.pop(field)

def exclude_fields(self):
"""Excludes fields that are included in the queryparameters"""
request = self.context.get('request')
if request:
exclude = request.query_params.get('exclude', None)
if exclude is None: return

excluded_fields = exclude.split(',')
for field in excluded_fields:
self.fields.pop(field)

class DispatchPublishableSerializer(object):
def publish(self):
Expand Down
2 changes: 1 addition & 1 deletion dispatch/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -651,7 +651,7 @@ class ArticleSerializer(DispatchModelSerializer, DispatchPublishableSerializer):
integrations = JSONField(required=False)

currently_breaking = serializers.BooleanField(source='is_currently_breaking', read_only=True)

class Meta:
model = Article
fields = (
Expand Down
5 changes: 1 addition & 4 deletions dispatch/static/manager/.babelrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
{
"presets": [
"es2015",
"react"
]
"presets": ["es2015", "react"]
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,12 @@ function ArticlePageComponent(props) {
filters={filters}
hasFilters={hasFilters}
headers={[ 'Headline', '', 'Authors', 'Published', 'Revisions']}
exclude={'subsection,subsection_id'}
extraColumns={[
item => item.currently_breaking ? breakingNews : '',
item => item.authors_string,
item => item.published_at ? humanizeDatetime(item.published_at) : 'Unpublished',
item => item.latest_version + ' revisions'
item => item.latest_version + ' revisions'
]}
shouldReload={(prevProps, props) => {
return (
Expand Down
3 changes: 2 additions & 1 deletion dispatch/static/manager/src/js/pages/ItemIndexPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ export default class ListItemsPageComponent extends React.Component {
getQuery() {
var query = {
limit: DEFAULT_LIMIT,
offset: (this.getCurrentPage() - 1) * DEFAULT_LIMIT
offset: (this.getCurrentPage() - 1) * DEFAULT_LIMIT,
exclude: this.props.exclude
}

// If listItem is present, add to query
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,10 @@ function JSONToBlock(acc, block) {
}

function fromJSON(jsonBlocks) {
if (typeof(jsonBlocks) === 'undefined'){
return ContentState.createFromText('')
}

if (!jsonBlocks.length) {
return ContentState.createFromText('')
}
Expand Down
19 changes: 19 additions & 0 deletions dispatch/tests/test_api_articles.py
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,25 @@ def test_list_published_articles(self):
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data['count'], 1)
self.assertEqual(response.data['results'][0]['slug'], 'article-2')
def test_article_exclude_query(self):
"""Should be able to exclude fields from articles by query params"""

article_1 = DispatchTestHelpers.create_article(self.client, headline='Article 1', slug='article-1')
article_2 = DispatchTestHelpers.create_article(self.client, headline='Article 2', slug='article-2')

url = '%s?exclude=%s' % (reverse('api-articles-list'), 'content,section_id')
response = self.client.get(url, format='json')

data = response.data

self.assertNotIn('content', data['results'][0])
self.assertNotIn('section_id', data['results'][0])
self.assertIn('slug', data['results'][0])
self.assertNotIn('content', data['results'][1])
self.assertNotIn('section_id', data['results'][1])
self.assertIn('slug', data['results'][1])
self.assertEqual(data['count'], 2)


def test_article_headline_query(self):
"""Should be able to search for articles by headline"""
Expand Down

0 comments on commit ce89cf3

Please sign in to comment.