Skip to content

Commit

Permalink
refactors PageElementList as a mixin
Browse files Browse the repository at this point in the history
  • Loading branch information
smirolo committed Nov 16, 2024
1 parent 13d2a8f commit 2e2ee70
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 63 deletions.
2 changes: 1 addition & 1 deletion pages/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@
PEP 386-compliant version number for the pages django app.
"""

__version__ = '0.8.2'
__version__ = '0.8.3-dev'
106 changes: 56 additions & 50 deletions pages/api/elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,61 @@
LOGGER = logging.getLogger(__name__)


class PageElementAPIView(TrailMixin, generics.ListAPIView):
class PageElementListMixin(TrailMixin):

@property
def visibility(self):
return None

@property
def owners(self):
return None

def attach(self, elements):
return elements

def get_cut(self):
cut_param = self.get_query_param('cut')
return ContentCut(cut_param) if cut_param else None

def get_results(self):
if self.element:
content_tree = build_content_tree(
roots=[self.element], prefix=self.full_path,
cut=self.get_cut(),
visibility=self.visibility,
accounts=self.owners)
items = flatten_content_tree(
content_tree, sort_by_key=False, depth=-1)
items.pop(0)
else:
cut = self.get_cut()
if not cut:
cut = ContentCut()
content_tree = build_content_tree(
roots=None, prefix=self.full_path,
cut=cut,
visibility=self.visibility,
accounts=self.owners)
# We do not re-sort the roots such that member-only content
# appears at the top.
items = flatten_content_tree(content_tree, sort_by_key=False)

results = []
for item in items:
searchable = get_extra(item, 'searchable', False)
if searchable:
results += [item]

return results

def get_queryset(self):
results = self.get_results()
self.attach(results)
return results


class PageElementAPIView(PageElementListMixin, generics.ListAPIView):
"""
Lists tree of page elements matching prefix
Expand Down Expand Up @@ -102,7 +156,6 @@ class PageElementAPIView(TrailMixin, generics.ListAPIView):
]
}
"""
queryset = PageElement.objects.all()
serializer_class = PageElementSerializer

search_fields = (
Expand All @@ -116,56 +169,9 @@ class PageElementAPIView(TrailMixin, generics.ListAPIView):

filter_backends = (SearchFilter, OrderingFilter,)

@property
def visibility(self):
return None

@property
def owners(self):
return None

def attach(self, elements):
return elements

def get_cut(self):
cut_param = self.request.query_params.get('cut')
return ContentCut(cut_param) if cut_param else None

def get_results(self):
if self.element:
content_tree = build_content_tree(
roots=[self.element], prefix=self.full_path,
cut=self.get_cut(),
visibility=self.visibility,
accounts=self.owners)
items = flatten_content_tree(
content_tree, sort_by_key=False, depth=-1)
items.pop(0)
else:
cut = self.get_cut()
if not cut:
cut = ContentCut()
content_tree = build_content_tree(
roots=None, prefix=self.full_path,
cut=cut,
visibility=self.visibility,
accounts=self.owners)
# We do not re-sort the roots such that member-only content
# appears at the top.
items = flatten_content_tree(content_tree, sort_by_key=False)

results = []
for item in items:
searchable = get_extra(item, 'searchable', False)
if searchable:
results += [item]

return results

def list(self, request, *args, **kwargs):
#pylint:disable=unused-argument
results = self.get_results()
self.attach(results)
results = self.get_queryset()

# We have multiple roots so we create an unifying top-level root.
element = self.element if self.element else PageElement()
Expand Down
6 changes: 3 additions & 3 deletions pages/api/progress.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ class EnumeratedProgressResetAPIView(SequenceProgressMixin, DestroyAPIView):
"""
Resets a user's progress on a sequence
**Tags**: editors, progress
**Tags**: editors, progress, provider
**Example**
Expand Down Expand Up @@ -206,7 +206,7 @@ class LiveEventAttendanceAPIView(EnumeratedProgressRetrieveAPIView):
"""
Retrieves attendance to live event
**Tags**: content, progress
**Tags**: content, progress, provider
**Examples**
Expand Down Expand Up @@ -235,7 +235,7 @@ def post(self, request, *args, **kwargs):
Indicates that a user attended a live event, hence fullfilling
the requirements for the element of the sequence.
**Tags**: editors, live-events, attendance
**Tags**: editors, live-events, attendance, provider
**Example**
Expand Down
12 changes: 6 additions & 6 deletions pages/api/reactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class FollowAPIView(PageElementMixin, generics.CreateAPIView):
The authenticated user making the request will receive notification
whenever someone comments on the practice.
**Tags**: content
**Tags**: content, user
**Examples**
Expand Down Expand Up @@ -81,7 +81,7 @@ class UnfollowAPIView(PageElementMixin, generics.CreateAPIView):
The authenticated user making the request will stop receiving notification
whenever someone comments on the practice.
**Tags**: content
**Tags**: content, user
**Examples**
Expand Down Expand Up @@ -118,7 +118,7 @@ class UpvoteAPIView(PageElementMixin, generics.CreateAPIView):
The authenticated user making the request indicates the practice is
considered worthwhile implementing.
**Tags**: content
**Tags**: content, user
**Examples**
Expand Down Expand Up @@ -155,7 +155,7 @@ class DownvoteAPIView(PageElementMixin, generics.CreateAPIView):
The authenticated user making the request indicates the practice is
not worth implementing.
**Tags**: content
**Tags**: content, user
**Examples**
Expand Down Expand Up @@ -230,7 +230,7 @@ def post(self, request, *args, **kwargs):
"""
Comments on a page element
**Tags**: content
**Tags**: content, user
**Examples**
Expand Down Expand Up @@ -276,7 +276,7 @@ class NewsFeedListAPIView(UserMixin, generics.ListAPIView):
"""
Retrieves relevant news for a user
**Tags**: content
**Tags**: content, user
**Examples**
Expand Down
7 changes: 7 additions & 0 deletions pages/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,13 @@ def get_full_element_path(self, path):
results = candidates[0]
return results

def get_query_param(self, key, default_value=None):
try:
return self.request.query_params.get(key, default_value)
except AttributeError:
pass
return self.request.GET.get(key, default_value)

def get_reverse_kwargs(self):
"""
List of kwargs taken from the url that needs to be passed through
Expand Down
9 changes: 6 additions & 3 deletions testsite/wsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,19 @@
from django.core.wsgi import get_wsgi_application


def save_coverage():
def save_coverage(*args):
sys.stderr.write("saving coverage\n")
cov.stop()
cov.save()

if os.getenv('DJANGO_COVERAGE'):
import atexit, sys
import coverage
cov = coverage.coverage(data_file=os.path.join(os.getenv('DJANGO_COVERAGE'),
".coverage.%d" % os.getpid()))
data_file=os.path.join(os.getenv('DJANGO_COVERAGE'),
".coverage.%d" % os.getpid())
cov = coverage.coverage(data_file=data_file)
sys.stderr.write("start recording coverage in %s\n" % str(data_file))
cov.set_option("run:relative_files", True)
cov.start()
atexit.register(save_coverage)
try:
Expand Down

0 comments on commit 2e2ee70

Please sign in to comment.