Skip to content

Commit

Permalink
add paganation support
Browse files Browse the repository at this point in the history
  • Loading branch information
datawhores committed Oct 4, 2024
1 parent 1510c51 commit 2b538b9
Show file tree
Hide file tree
Showing 8 changed files with 149 additions and 114 deletions.
32 changes: 18 additions & 14 deletions ofscraper/classes/table/fields/numfield.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,48 +6,52 @@


class NumField(Horizontal):
def __init__(self, name: str) -> None:
def __init__(self, name: str,default=None) -> None:
name = name.lower()
super().__init__(id=name)
self.filter_name = name
self._set_default(default)
def _set_default(self, default):
self.default=str(default if str(default).isnumeric() else "")




def compose(self):
yield IntegerInput(
placeholder=self.filter_name.capitalize().replace("_", " "),
id=f"{self.filter_name}_input",
value=self.default,
)

def empty(self):
status[self.filter_name] = ""
return self.query_one(IntegerInput).value == ""
self.query_one(IntegerInput).value=self.default
return self.query_one(IntegerInput).value


def update_table_val(self, val):
if isinstance(val,list):
val=",".join(val)
val=self.query_one(IntegerInput).validate_value(val)
self.query_one(IntegerInput).value = val
status[self.filter_name] = val
if not str(val).isnumeric():
return
self.query_one(IntegerInput).value = str(val)
return val


def reset(self):
self.query_one(IntegerInput).value = ""
status[self.filter_name] = ""
self.query_one(IntegerInput).value = self.default


class OtherMediaNumField(NumField):
def empty(self):
status[self.filter_name] = ""
return self.query_one(IntegerInput).value == ""
self.query_one(IntegerInput).value = self.default
return self.query_one(IntegerInput).value

def update_table_val(self, val):
val = str(len(eval(val)))
val = str(val)
self.query_one(IntegerInput).value = val
status[self.filter_name] = val


def reset(self):
self.query_one(IntegerInput).value = ""
status[self.filter_name] = ""
self.query_one(IntegerInput).value = self.default

44 changes: 0 additions & 44 deletions ofscraper/classes/table/inputs/filterinput.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,50 +11,6 @@ def __init__(self, *args: Any, placeholder=None, **kwargs: Any) -> None:
placeholder = placeholder or kwargs.get("id") or "placeholder"
super().__init__(*args, placeholder=placeholder, **kwargs)
# TODO: Workaround for https://github.com/Textualize/textual/issues/1216
self.update_table_val(self.value)

def validate_value(self, value: str) -> str:
"""Validate the input.
Args:
value: The value to validate.
Returns:
The acceptable value.
"""
# If the input field isn't empty...
if value.strip():
try:
# ...run it through the casting function. We don't care
# about what comes out of the other end, we just case that
# it makes it through at all.
_ = self.CAST(value)
except ValueError:
# It's expected that the casting function will throw a
# ValueError if there's a problem with the conversion (see
# int and float for example) so, here we are. Make a
# noise...
self.app.bell()
# ...and return what's in the input now because we're
# rejecting the new value.
return self.value
# The value to test is either empty, or valid. Let's accept it.
return value

def update_table_val(self, val):
if isinstance(val,list):
val=",".join(val)
self.value = self.validate_value(val)
status[self.key] = self.value
return self.value


def on_input_changed(self):
status[self.key] = self.value

def on_input_submitted(self):
status[self.key] = self.value

@property
def key(self):
return self.id.replace("_input", "")
10 changes: 9 additions & 1 deletion ofscraper/classes/table/inputs/intergerinput.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
from ofscraper.classes.table.inputs.filterinput import FilterInput
from ofscraper.classes.table.status import status


class IntegerInput(FilterInput):
CAST = int
def __init__(self,*args, **kwargs):
super().__init__(*args,type="integer",**kwargs)

def on_input_changed(self):
status[self.key] = int(self.value) if self.value else ""

def on_input_submitted(self):
status[self.key] = int(self.value) if self.value else ""
10 changes: 9 additions & 1 deletion ofscraper/classes/table/inputs/strinput.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
from ofscraper.classes.table.inputs.filterinput import FilterInput
from ofscraper.classes.table.status import status



class StrInput(FilterInput):
CAST = str
def __init__(self,*args,**kwargs):
super().__init__(*args,type="text",**kwargs)
def on_input_changed(self):
status[self.key] = self.value

def on_input_submitted(self):
status[self.key] = self.value
3 changes: 3 additions & 0 deletions ofscraper/classes/table/status.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ def _set_defaults(self):
self._status.setdefault("max_length", {})
self._status.setdefault("media_id", None)
self._status.setdefault("post_id", None)
self._status.setdefault("page", None)
self._status.setdefault("num_per_page", None)


def validate(self, key, test):
key = key.lower()
Expand Down
Loading

0 comments on commit 2b538b9

Please sign in to comment.