diff --git a/requirements.txt b/requirements.txt index 85652deeb29fe62fb8dc1482836ab4e931e18016..b428d9b2b826d488c862ce07e29422c5900a88e1 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ +requests mysqlclient django django-auth-ldap diff --git a/schoolapps/dashboard/views.py b/schoolapps/dashboard/views.py index ab25a35124f7346581d1d5ce5b477353100a258c..55c86bc61236e58273664b63b9504b0480822c2f 100755 --- a/schoolapps/dashboard/views.py +++ b/schoolapps/dashboard/views.py @@ -39,7 +39,7 @@ def api_information(request): unread_notifications = request.user.notifications.all().filter(user=request.user, read=False).order_by( '-created_at') # user_type = UserInformation.user_type(request.user) - newest_articles = get_newest_articles("https://katharineum-zu-luebeck.de", 1, [22]) + newest_articles = get_newest_articles(limit=1) if len(newest_articles) >= 0: newest_article = newest_articles[0] else: diff --git a/schoolapps/helper.py b/schoolapps/helper.py index 55ca87b0767dc331bcec481409a1dd0e0d3afd61..d3facc3d7d87b2f039d3f9077f2e9143aa17b871 100644 --- a/schoolapps/helper.py +++ b/schoolapps/helper.py @@ -6,6 +6,7 @@ from datetime import datetime from django.utils import timezone, formats from ics import Calendar +import requests def path_and_rename(instance, filename): @@ -26,36 +27,60 @@ def msg_box(msg, status="success", icon="info"): return {"msg": msg, "status": status, "icon": icon} -import requests +WP_DOMAIN: str = "https://katharineum-zu-luebeck.de" -def get_newest_articles(domain, limit=0, author_blacklist=None): +def get_newest_articles(domain: str = WP_DOMAIN, + limit: int = 5, + author_whitelist: list = None, + author_blacklist: list = None, + category_whitelist: list = None, + category_blacklist: list = None + ): """ - This function returns the newest articles/posts of a wordpress-site. + This function returns the newest articles/posts of a WordPress site. :param domain: The domain to get the newest posts from (for example https://wordpress.com). Don't put a slash (/) at the end! :param limit: if 0: all posts will be shown, else nly the certain number - :param author_blacklist: if the authors id (an integer) is in this list, the article won't be displayed + :param author_whitelist: If this list is filled, only articles which are written by one of this authors will be shown + :param author_blacklist: If the author's id (an integer) is in this list, the article won't be shown + :param category_whitelist: If this list is filled, only articles which are in one of this categories will be shown + :param category_blacklist: If the category's id (an integer) is in this list, the article won't be shown :return: a list of the newest posts/articles """ + # Make mutable default arguments unmutable + if category_whitelist is None: + category_whitelist = [] + if category_blacklist is None: + category_blacklist = [] + if author_whitelist is None: + author_whitelist = [] if author_blacklist is None: author_blacklist = [] - suffix = "/wp-json/wp/v2/posts" + suffix: str = "/wp-json/wp/v2/posts" + url: str = domain + suffix - url = domain + suffix + site: requests.request = requests.get(url) + data: dict = site.json() - site = requests.get(url) - data = site.json() + posts: list = [] - posts = [] - print(data) for post in data: if post["author"] not in author_blacklist: - # Now get the link to the image - if post["_links"].get("wp:featuredmedia", False): - media_site = requests.get(post["_links"]["wp:featuredmedia"][0]["href"]).json() - image_url = media_site["guid"]["rendered"] + if len(author_whitelist) > 0 and post["author"] not in author_whitelist: + continue + + if post["categories"][0] not in category_blacklist: + if len(category_whitelist) > 0 and post["categories"][0] not in category_whitelist: + continue + + # Now get the link to the image + if post["_links"].get("wp:featuredmedia", False): + media_site: requests.request = requests.get(post["_links"]["wp:featuredmedia"][0]["href"]).json() + image_url: str = media_site["guid"]["rendered"] + else: + image_url: str = "" posts.append( { @@ -71,46 +96,59 @@ def get_newest_articles(domain, limit=0, author_blacklist=None): return posts -CALENDAR_URL = "https://nimbus.katharineum.de/remote.php/dav/public-calendars/owit7yysLB2CYNTq?export" +# Set calendar here +CALENDAR_URL: str = "https://nimbus.katharineum.de/remote.php/dav/public-calendars/owit7yysLB2CYNTq?export" +CALENDAR: Calendar = Calendar(requests.get(CALENDAR_URL).text) -def get_current_events(): - c = Calendar(requests.get(CALENDAR_URL).text) - print(c.events) - e = list(c.timeline)[0] - print(c.timeline.today()) - i = 0 - events = [] - for event in c.timeline.start_after(timezone.now()): - if i >= 5: +def get_current_events(calendar: Calendar = CALENDAR, limit: int = 5) -> list: + """ + Get upcoming events from calendar + :param calendar: The calendar object + :param limit: Count of events + :return: List of upcoming events + """ + i: int = 0 + events: list = [] + for event in calendar.timeline.start_after(timezone.now()): + # Check for limit + if i >= limit: break i += 1 + # Create formatted dates and times for begin and end begin_date_formatted = formats.date_format(event.begin) end_date_formatted = formats.date_format(event.end) begin_time_formatted = formats.time_format(event.begin.time()) end_time_formatted = formats.time_format(event.end.time()) + if event.begin.date() == event.end.date(): + # Event is only on one day formatted = begin_date_formatted + if not event.all_day: + # No all day event formatted += " " + begin_time_formatted + if event.begin.time != event.end.time(): + # Event has an end time formatted += " – " + end_time_formatted + else: + # Event is on multiple days if event.all_day: + # Event is all day formatted = "{} – {}".format(begin_date_formatted, end_date_formatted) else: + # Event has begin and end times formatted = "{} {} – {} {}".format(begin_date_formatted, begin_time_formatted, end_date_formatted, end_time_formatted) - print(formatted) - print(formats.date_format(event.begin)) + events.append({ "name": event.name, - # "begin": event.begin, - # "end": event.end, + "begin_timestamp": event.begin.timestamp, + "end_timestamp": event.end.timestamp, "formatted": formatted }) - # print(event) - print(events) - print("Event '{}' started {}".format(e.name, e.begin.humanize())) + return events