-
Notifications
You must be signed in to change notification settings - Fork 0
override springboard homepage view #1
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
Merged
Rizziepit
merged 11 commits into
develop
from
feature/issue-1-override-springboard-homepage-view
May 26, 2015
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
4dabac4
override home view to add recent_content to context
Rizziepit e59ffe8
use springboard feature branch for now
Rizziepit 5353fff
correct pip git url format
Rizziepit 28bc0c1
and again
Rizziepit e084bb1
and hopefully the last time
Rizziepit 072e6b4
simplify
Rizziepit 79487fe
remove caching
Rizziepit a128bf7
use 1 query to get random categories, use seed for hourly changes
Rizziepit ce99bbf
fixes and fewer queries
Rizziepit 9b2b74c
test index view, fix pep8
Rizziepit 01d3321
style improvements
Rizziepit File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| springboard | ||
| springboard>=1.0.0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| import re | ||
|
|
||
| from datetime import datetime, timedelta | ||
|
|
||
| from pyramid import testing | ||
|
|
||
| from springboard.tests import SpringboardTestCase | ||
|
|
||
| from springboard_iogt.views import IoGTViews | ||
| from springboard_iogt.application import main | ||
|
|
||
|
|
||
| class TestIoGTViews(SpringboardTestCase): | ||
|
|
||
| def setUp(self): | ||
| self.workspace = self.mk_workspace() | ||
| self.config = testing.setUp(settings={ | ||
| 'unicore.repos_dir': self.working_dir, | ||
| 'unicore.content_repo_urls': self.workspace.working_dir, | ||
| }) | ||
|
|
||
| def tearDown(self): | ||
| testing.tearDown() | ||
|
|
||
| def test_recent_content(self): | ||
| category_p1, category_p3 = self.mk_categories(self.workspace, count=2) | ||
| [page1] = self.mk_pages( | ||
| self.workspace, count=1, | ||
| primary_category=category_p1.uuid, | ||
| created_at=datetime.utcnow().isoformat()) | ||
| [page2] = self.mk_pages( | ||
| self.workspace, count=1, | ||
| primary_category=None, | ||
| created_at=(datetime.utcnow() - timedelta(hours=1)).isoformat()) | ||
| [page3] = self.mk_pages( | ||
| self.workspace, count=1, | ||
| primary_category=category_p3.uuid, | ||
| created_at=(datetime.utcnow() - timedelta(hours=2)).isoformat()) | ||
| views = IoGTViews(self.mk_request()) | ||
|
|
||
| results = views.recent_content()(limit=2) | ||
| self.assertEqual(len(results), 2) | ||
| self.assertEqual( | ||
| {(category_p1.uuid, page1.uuid), (None, page2.uuid)}, | ||
| set((c.uuid if c else None, p.uuid) for c, p in results)) | ||
|
|
||
| results = views.recent_content()(limit=3) | ||
| self.assertEqual(len(results), 3) | ||
| self.assertEqual( | ||
| {(category_p1.uuid, page1.uuid), (None, page2.uuid), | ||
| (category_p3.uuid, page3.uuid)}, | ||
| set((c.uuid if c else None, p.uuid) for c, p in results)) | ||
|
|
||
| def test_index_view(self): | ||
| [category] = self.mk_categories(self.workspace, count=1) | ||
| [page1, page2] = self.mk_pages( | ||
| self.workspace, count=2, | ||
| created_at=datetime.utcnow().isoformat()) | ||
| page1 = page1.update({'primary_category': category.uuid}) | ||
| self.workspace.save(page1, 'Update page category') | ||
| self.workspace.refresh_index() | ||
| app = self.mk_app(self.workspace, main=main) | ||
|
|
||
| response = app.get('/') | ||
| self.assertEqual(response.status_int, 200) | ||
| html = response.html | ||
| re_page_url = re.compile(r'/page/.{32}/') | ||
| re_category_url = re.compile(r'/category/.{32}/') | ||
| self.assertEqual(len(html.find_all('a', href=re_page_url)), 2) | ||
| self.assertEqual(len(html.find_all('a', href=re_category_url)), 2) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| def randomize_query(s_obj, seed=None): | ||
| return s_obj.query_raw({ | ||
| "function_score": { | ||
| "functions": [ | ||
| {"random_score": {"seed": seed}} | ||
| ], | ||
| "score_mode": "sum" | ||
| }}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| import random | ||
| from datetime import datetime | ||
| from itertools import chain | ||
|
|
||
| from pyramid.view import view_config | ||
| from elasticutils import F | ||
|
|
||
| from springboard.views.base import SpringboardViews | ||
|
|
||
| from springboard_iogt.utils import randomize_query | ||
|
|
||
|
|
||
| class IoGTViews(SpringboardViews): | ||
|
|
||
| @view_config(route_name='home', | ||
| renderer='springboard_iogt:templates/home.jinja2') | ||
| def index_view(self): | ||
| return self.context(recent_content=self.recent_content()) | ||
|
|
||
| def recent_content(self): | ||
| # get 2 most recent pages and their categories | ||
| [page1, page2] = self.all_pages.filter( | ||
| language=self.language).order_by('-created_at')[:2] | ||
| categories = self.all_categories.filter( | ||
| uuid__in=filter( | ||
| None, [page1.primary_category, page2.primary_category])) | ||
| categories = dict((category.uuid, category) for category in categories) | ||
| category1 = categories.get(page1.primary_category) | ||
| category2 = categories.get(page2.primary_category) | ||
|
|
||
| # random seed that changes hourly | ||
| seed = datetime.utcnow().hour | ||
|
|
||
| # get random categories and exclude categories of 2 most recent pages | ||
| categories = self.all_categories.filter( | ||
| ~F(uuid__in=categories.keys()), language=self.language) | ||
| categories = randomize_query(categories, seed=seed) | ||
| # filter to exclude the 2 most recent pages | ||
| f_exclude_pages = ~F(uuid__in=[page1.uuid, page2.uuid]) | ||
|
|
||
| def do_query(limit): | ||
| most_recent = [(category1, page1), (category2, page2)] | ||
| most_recent_per_category = [] | ||
| # NOTE: this is bad if limit is large | ||
| # We are fetching pages individually, because we | ||
| # can't use facets or aggregates. | ||
| for category in categories[:limit - 2]: | ||
| try: | ||
| [page] = self.all_pages.filter( | ||
| f_exclude_pages, | ||
| primary_category=category.uuid).order_by( | ||
| '-created_at')[:1] | ||
| most_recent_per_category.append((category, page)) | ||
| except ValueError: | ||
| pass | ||
|
|
||
| results = [(c.to_object() if c else None, p.to_object()) | ||
| for c, p | ||
| in chain(most_recent, most_recent_per_category)] | ||
| # randomize position of most_recent content | ||
| random.seed(seed) | ||
| random.shuffle(results) | ||
| return results | ||
|
|
||
| return do_query | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are we returning a function here to try & not evaluate this stuff unless called explicitly in the template?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That, but mostly I want the template to decide how many items are needed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it be a good idea to turn this into a re-usable macro / filter for Jinja2 then?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can give it a shot, but can I postpone doing that until change requests come in?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍