Skip to content

Commit

Permalink
Fix data retreival issue on GetData
Browse files Browse the repository at this point in the history
  • Loading branch information
daniviga committed Apr 21, 2024
1 parent aa02404 commit de4cb77
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 29 deletions.
77 changes: 49 additions & 28 deletions ram/portal/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,15 @@
)


def order_by_fields():
def get_items_per_page():
try:
items_per_page = get_site_conf().items_per_page
except (OperationalError, ProgrammingError):
items_per_page = 6
return items_per_page


def get_order_by_field():
try:
order_by = get_site_conf().items_ordering
except (OperationalError, ProgrammingError):
Expand All @@ -46,19 +54,22 @@ class GetData(View):
title = "Home"
template = "roster.html"
item_type = "rolling_stock"
queryset = RollingStock.objects.order_by(*order_by_fields())
filter = Q() # empty filter by default

def get(self, request, page=1):
site_conf = get_site_conf()
def get_data(self):
return RollingStock.objects.order_by(
*get_order_by_field()
).filter(self.filter)

def get(self, request, page=1):
data = []
for item in self.queryset:
for item in self.get_data():
data.append({
"type": self.item_type,
"item": item
})

paginator = Paginator(data, site_conf.items_per_page)
paginator = Paginator(data, get_items_per_page())
data = paginator.get_page(page)
page_range = paginator.get_elided_page_range(
data.number, on_each_side=2, on_ends=1
Expand All @@ -80,12 +91,13 @@ def get(self, request, page=1):
class GetRoster(GetData):
title = "Roster"
item_type = "rolling_stock"
queryset = RollingStock.objects.order_by(*order_by_fields())

def get_data(self):
return RollingStock.objects.order_by(*get_order_by_field())


class SearchObjects(View):
def run_search(self, request, search, _filter, page=1):
site_conf = get_site_conf()
if _filter is None:
query = reduce(
operator.or_,
Expand Down Expand Up @@ -130,7 +142,7 @@ def run_search(self, request, search, _filter, page=1):
rolling_stock = (
RollingStock.objects.filter(query)
.distinct()
.order_by(*order_by_fields())
.order_by(*get_order_by_field())
)
for item in rolling_stock:
data.append({
Expand Down Expand Up @@ -162,7 +174,7 @@ def run_search(self, request, search, _filter, page=1):
"item": item
})

paginator = Paginator(data, site_conf.items_per_page)
paginator = Paginator(data, get_items_per_page())
data = paginator.get_page(page)
page_range = paginator.get_elided_page_range(
data.number, on_each_side=2, on_ends=1
Expand Down Expand Up @@ -217,11 +229,9 @@ def post(self, request, page=1):

class GetManufacturerItem(View):
def get(self, request, manufacturer, search="all", page=1):
site_conf = get_site_conf()

if search != "all":
rolling_stock = get_list_or_404(
RollingStock.objects.order_by(*order_by_fields()),
RollingStock.objects.order_by(*get_order_by_field()),
Q(
Q(manufacturer__name__iexact=manufacturer)
& Q(item_number__exact=search)
Expand All @@ -233,7 +243,7 @@ def get(self, request, manufacturer, search="all", page=1):
)
else:
rolling_stock = get_list_or_404(
RollingStock.objects.order_by(*order_by_fields()),
RollingStock.objects.order_by(*get_order_by_field()),
Q(rolling_class__manufacturer__slug__iexact=manufacturer)
| Q(manufacturer__slug__iexact=manufacturer)
)
Expand All @@ -248,7 +258,7 @@ def get(self, request, manufacturer, search="all", page=1):
"item": item
})

paginator = Paginator(data, site_conf.items_per_page)
paginator = Paginator(data, get_items_per_page())
data = paginator.get_page(page)
page_range = paginator.get_elided_page_range(
data.number, on_each_side=2, on_ends=1
Expand All @@ -269,8 +279,6 @@ def get(self, request, manufacturer, search="all", page=1):

class GetObjectsFiltered(View):
def run_filter(self, request, search, _filter, page=1):
site_conf = get_site_conf()

if _filter == "type":
title = get_object_or_404(RollingStockType, slug__iexact=search)
query = Q(rolling_class__type__slug__iexact=search)
Expand All @@ -294,7 +302,7 @@ def run_filter(self, request, search, _filter, page=1):
rolling_stock = (
RollingStock.objects.filter(query)
.distinct()
.order_by(*order_by_fields())
.order_by(*get_order_by_field())
)

data = []
Expand Down Expand Up @@ -327,7 +335,7 @@ def run_filter(self, request, search, _filter, page=1):
except NameError:
pass

paginator = Paginator(data, site_conf.items_per_page)
paginator = Paginator(data, get_items_per_page())
data = paginator.get_page(page)
page_range = paginator.get_elided_page_range(
data.number, on_each_side=2, on_ends=1
Expand Down Expand Up @@ -401,7 +409,7 @@ def get(self, request, uuid):
Q(item_number__exact=rolling_stock.item_number)
& Q(set=True)
)
).order_by(*order_by_fields())]
).order_by(*get_order_by_field())]

return render(
request,
Expand All @@ -423,12 +431,13 @@ def get(self, request, uuid):
class Consists(GetData):
title = "Consists"
item_type = "consist"
queryset = Consist.objects.all()

def get_data(self):
return Consist.objects.all()


class GetConsist(View):
def get(self, request, uuid, page=1):
site_conf = get_site_conf()
try:
consist = Consist.objects.get(uuid=uuid)
except ObjectDoesNotExist:
Expand All @@ -438,7 +447,7 @@ def get(self, request, uuid, page=1):
"item": RollingStock.objects.get(uuid=r.rolling_stock_id)
} for r in consist.consist_item.all()]

paginator = Paginator(data, site_conf.items_per_page)
paginator = Paginator(data, get_items_per_page())
data = paginator.get_page(page)
page_range = paginator.get_elided_page_range(
data.number, on_each_side=2, on_ends=1
Expand All @@ -459,38 +468,50 @@ def get(self, request, uuid, page=1):
class Manufacturers(GetData):
title = "Manufacturers"
item_type = "manufacturer"
queryset = None # Set via method get

def get_data(self):
return Manufacturer.objects.filter(self.filter)

# overload get method to filter by category
def get(self, request, category, page=1):
if category not in ("real", "model"):
raise Http404
self.queryset = Manufacturer.objects.filter(category=category)
self.filter = Q(category=category)

return super().get(request, page)


class Companies(GetData):
title = "Companies"
item_type = "company"
queryset = Company.objects.all()

def get_data(self):
return Company.objects.all()


class Scales(GetData):
title = "Scales"
item_type = "scale"
queryset = Scale.objects.all()

def get_data(self):
return Scale.objects.all()


class Types(GetData):
title = "Types"
item_type = "rolling_stock_type"
queryset = RollingStockType.objects.all()

def get_data(self):
return RollingStockType.objects.all()


class Books(GetData):
title = "Books"
item_type = "book"
queryset = Book.objects.all()

def get_data(self):
return Book.objects.all()


class GetBook(View):
Expand Down
2 changes: 1 addition & 1 deletion ram/ram/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from ram.utils import git_suffix

__version__ = "0.12.1"
__version__ = "0.12.2"
__version__ += git_suffix(__file__)

0 comments on commit de4cb77

Please sign in to comment.