Skip to content

Commit 46c1fcf

Browse files
committed
Move get_prepared_attributes from model to view
We're in the process of moving logic off the models. This model method was only used by one view. This moves the logic from the model to the view.
1 parent 2d15e45 commit 46c1fcf

2 files changed

Lines changed: 39 additions & 36 deletions

File tree

cbv/models.py

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -261,40 +261,6 @@ def get_attributes(self) -> models.QuerySet["KlassAttribute"]:
261261
self._attributes = attrs
262262
return self._attributes
263263

264-
def get_prepared_attributes(self) -> models.QuerySet["KlassAttribute"]:
265-
attributes = self.get_attributes()
266-
# Make a dictionary of attributes based on name
267-
attribute_names: dict[str, list[KlassAttribute]] = {}
268-
for attr in attributes:
269-
try:
270-
attribute_names[attr.name] += [attr]
271-
except KeyError:
272-
attribute_names[attr.name] = [attr]
273-
274-
ancestors = self.get_all_ancestors()
275-
276-
# Find overridden attributes
277-
for name, attrs in attribute_names.items():
278-
# Skip if we have only one attribute.
279-
if len(attrs) == 1:
280-
continue
281-
282-
# Sort the attributes by ancestors.
283-
def _key(a: KlassAttribute) -> int:
284-
try:
285-
# If ancestor, return the index (>= 0)
286-
return ancestors.index(a.klass)
287-
except ValueError: # Raised by .index if item is not in list.
288-
# else a.klass == self, so return -1
289-
return -1
290-
291-
sorted_attrs = sorted(attrs, key=_key)
292-
293-
# Mark overriden KlassAttributes
294-
for a in sorted_attrs[1:]:
295-
a.overridden = True
296-
return attributes
297-
298264
def basic_yuml_data(self, first: bool = False) -> list[str]:
299265
self._basic_yuml_data: list[str]
300266
if hasattr(self, "_basic_yuml_data"):

cbv/views.py

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22

33
import attrs
44
from django import http
5+
from django.db import models
56
from django.urls import reverse
67
from django.views.generic import RedirectView, TemplateView, View
78

8-
from cbv.models import DjangoURLService, Klass, Module, ProjectVersion
9+
from cbv.models import DjangoURLService, Klass, KlassAttribute, Module, ProjectVersion
910
from cbv.queries import NavBuilder
1011

1112

@@ -87,7 +88,7 @@ def get_context_data(self, **kwargs):
8788
return {
8889
"all_ancestors": ancestors,
8990
"all_children": children,
90-
"attributes": klass.get_prepared_attributes(),
91+
"attributes": self.get_prepared_attributes(klass),
9192
"canonical_url": self.request.build_absolute_uri(canonical_url_path),
9293
"klass": klass,
9394
"methods": list(klass.get_methods()),
@@ -98,6 +99,42 @@ def get_context_data(self, **kwargs):
9899
"yuml_url": klass.basic_yuml_url(),
99100
}
100101

102+
def get_prepared_attributes(
103+
self, class_: Klass
104+
) -> models.QuerySet["KlassAttribute"]:
105+
attributes = class_.get_attributes()
106+
# Make a dictionary of attributes based on name
107+
attribute_names: dict[str, list[KlassAttribute]] = {}
108+
for attr in attributes:
109+
try:
110+
attribute_names[attr.name] += [attr]
111+
except KeyError:
112+
attribute_names[attr.name] = [attr]
113+
114+
ancestors = class_.get_all_ancestors()
115+
116+
# Find overridden attributes
117+
for name, attrs_ in attribute_names.items():
118+
# Skip if we have only one attribute.
119+
if len(attrs_) == 1:
120+
continue
121+
122+
# Sort the attributes by ancestors.
123+
def _key(a: KlassAttribute) -> int:
124+
try:
125+
# If ancestor, return the index (>= 0)
126+
return ancestors.index(a.klass)
127+
except ValueError: # Raised by .index if item is not in list.
128+
# else a.klass == self, so return -1
129+
return -1
130+
131+
sorted_attrs = sorted(attrs_, key=_key)
132+
133+
# Mark overriden KlassAttributes
134+
for a in sorted_attrs[1:]:
135+
a.overridden = True
136+
return attributes
137+
101138

102139
class LatestKlassRedirectView(RedirectView):
103140
def get_redirect_url(self, **kwargs):

0 commit comments

Comments
 (0)