Skip to content

Commit

Permalink
Merge pull request #99 from filips123/solsis-api
Browse files Browse the repository at this point in the history
Solsis API
  • Loading branch information
filips123 authored Aug 27, 2024
2 parents 0bf1e87 + 6ec6dac commit 5cef1f8
Show file tree
Hide file tree
Showing 13 changed files with 596 additions and 169 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ web_modules/

# GimVicUrnik Configuration & Database
config.yaml
app.db
*.db

# Do not ignore .gitkeep files
!.gitkeep
5 changes: 3 additions & 2 deletions API/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ You will also need to install [one of SQLAlchemy dialects](https://docs.sqlalche

GimVičUrnik API uses YAML file for configuration. Example file can be found at [`config.yaml.sample`](config.yaml.sample). You can also see default values and the schema [in the source code](gimvicurnik/config/__init__.py). If you don't plan to use Sentry, you can delete its section entirely. Logging section will by default display INFO or higher log levels to stdout, but you can also delete or change it if you don't want that.

You need to obtain the e-classroom token as specified in the [Moodle Forum Discussion](https://moodle.org/mod/forum/discuss.php?d=193857).
You need to obtain the e-classroom token as specified in the [Moodle Forum Discussion](https://moodle.org/mod/forum/discuss.php?d=193857). To run Solsis updater, you will also need Solsis API token. If you don't have one, you can use the e-classroom updater with the parse substitutions option instead, to parse substitution from PDF files.

It is recommended to set the configuration file as `GIMVICURNIK_CONFIG` environment variable, but setting `--config` argument also mostly works.

Expand All @@ -42,8 +42,9 @@ You need to run `gimvicurnik create-database` to create all required database ta
Data need to be fetched and updated in separate commands from the web server. Most likely you want to execute them periodically in a cron job.

* `gimvicurnik update-timetable`: Update the timetable data
* `gimvicurnik update-eclassroom`: Update the e-classroom data (substitutions, lunch schedule, circulars)
* `gimvicurnik update-eclassroom`: Update the e-classroom data (~~substitutions~~, lunch schedules, circulars)
* `gimvicurnik update-menu`: Update the menu data (snack and lunch menu)
* `gimvicurnik update-solsis`: Update the Solsis data (substitutions)

### Starting Server

Expand Down
4 changes: 4 additions & 0 deletions API/config.yaml.sample
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ sources:
course: 118
menu:
url: https://www.gimvic.org/delovanjesole/solske_sluzbe_in_solski_organi/solska_prehrana/
solsis:
url: https://solsis.gimvic.org/
serverName: solsis.gimvic.org
apiKey: YOUR-API-KEY-HERE

urls:
website: https://urnik.gimvic.org
Expand Down
2 changes: 2 additions & 0 deletions API/gimvicurnik/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
create_database_command,
update_eclassroom_command,
update_menu_command,
update_solsis_command,
cleanup_database_command,
update_timetable_command,
)
Expand Down Expand Up @@ -246,6 +247,7 @@ def register_commands(self) -> None:
self.app.cli.add_command(update_timetable_command)
self.app.cli.add_command(update_eclassroom_command)
self.app.cli.add_command(update_menu_command)
self.app.cli.add_command(update_solsis_command)
self.app.cli.add_command(cleanup_database_command)
self.app.cli.add_command(create_database_command)

Expand Down
29 changes: 26 additions & 3 deletions API/gimvicurnik/commands/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from sqlalchemy import and_, or_

from ..database import Base, SessionFactory, Document, DocumentType
from ..updaters import EClassroomUpdater, MenuUpdater, TimetableUpdater
from ..updaters import EClassroomUpdater, MenuUpdater, TimetableUpdater, SolsisUpdater
from ..utils.sentry import with_transaction

if typing.TYPE_CHECKING:
Expand All @@ -29,15 +29,16 @@ def update_timetable_command() -> None:


@click.command("update-eclassroom", help="Update the e-classroom data.")
@click.option("--parse-substitutions", "-p", help="Parse substitutions.", is_flag=True)
@with_transaction(name="update-eclassroom", op="command")
def update_eclassroom_command() -> None:
def update_eclassroom_command(parse_substitutions: bool = False) -> None:
"""Update data from the e-classroom."""

logging.getLogger(__name__).info("Updating the e-classroom data")

with SessionFactory.begin() as session:
gimvicurnik: GimVicUrnik = current_app.config["GIMVICURNIK"]
updater = EClassroomUpdater(gimvicurnik.config.sources.eclassroom, session)
updater = EClassroomUpdater(gimvicurnik.config.sources.eclassroom, session, parse_substitutions, True)
updater.update()


Expand All @@ -54,6 +55,28 @@ def update_menu_command() -> None:
updater.update()


@click.command("update-solsis", help="Update the Solsis data.")
@click.option("--date-span", "-s", nargs=2, type=str, help="Start and end date to get substitutions for.")
@with_transaction(name="update-solsis", op="command")
def update_solsis_command(date_span: tuple[str, str]) -> None:
"""Update data from Solsis."""

# The default span is 7 days inclusive
date_from = datetime.now().date()
date_to = date_from + timedelta(days=6)

if date_span:
date_from = datetime.strptime(date_span[0], "%Y-%m-%d").date()
date_to = datetime.strptime(date_span[1], "%Y-%m-%d").date()

logging.getLogger(__name__).info("Updating the Solsis data (%s - %s)", date_from, date_to)

with SessionFactory.begin() as session:
gimvicurnik: GimVicUrnik = current_app.config["GIMVICURNIK"]
updater = SolsisUpdater(gimvicurnik.config.sources.solsis, session, date_from, date_to)
updater.update()


@click.command("cleanup-database", help="Clean up the database.")
@with_transaction(name="cleanup-database", op="command")
def cleanup_database_command() -> None:
Expand Down
8 changes: 8 additions & 0 deletions API/gimvicurnik/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,19 @@ class ConfigSourcesMenu:
url: str


@define(kw_only=True)
class ConfigSourcesSolsis:
url: str
serverName: str
apiKey: str


@define(kw_only=True)
class ConfigSources:
timetable: ConfigSourcesTimetable
eclassroom: ConfigSourcesEClassroom
menu: ConfigSourcesMenu
solsis: ConfigSourcesSolsis


# --------- URLS CONFIG ---------
Expand Down
1 change: 1 addition & 0 deletions API/gimvicurnik/errors/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@
LunchScheduleFormatError,
)
from .menu import MenuApiError, MenuDateError, MenuFormatError
from .solsis import SolsisApiError
from .timetable import TimetableApiError
5 changes: 5 additions & 0 deletions API/gimvicurnik/errors/solsis.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from .base import GimVicUrnikError


class SolsisApiError(GimVicUrnikError):
pass
1 change: 1 addition & 0 deletions API/gimvicurnik/updaters/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from .eclassroom import EClassroomUpdater
from .menu import MenuUpdater
from .solsis import SolsisUpdater
from .timetable import TimetableUpdater
Loading

0 comments on commit 5cef1f8

Please sign in to comment.