Created
November 11, 2016 15:15
-
-
Save alastairparagas/d3967550976471c411797fc8e84ed312 to your computer and use it in GitHub Desktop.
Sample Python Script - Functionally organized, of course
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from firebase import Firebase | |
| import pydash as _ | |
| MYHONORS_FIREBASE_URL = '' | |
| MYHONORS_FIREBASE_AUTH_TOKEN = '' | |
| EVENTS_NEW_TIMESTAMP = '' | |
| """ | |
| Authorizes our access into the MyHonors Firebase database that we need | |
| to reset and prepare for the next upcoming academic year. | |
| @returns Authorized Firebase object | |
| """ | |
| def authorize(): | |
| return Firebase( | |
| MYHONORS_FIREBASE_URL, | |
| auth_token=MYHONORS_FIREBASE_AUTH_TOKEN | |
| ) | |
| """ | |
| Clears attendance, RSVPs, lastActivity and volunteerHours data from | |
| individual user_profiles nodes under user_profiles. | |
| * attendance is a user's history of events attendance | |
| * rsvp is a history of RSVP actions | |
| * lastActivity is the timestamp of the last time a user logged | |
| into MyHonors | |
| * volunteerHours is the count of community service hours | |
| @returns void | |
| """ | |
| def userprofiles_reset(): | |
| firebase_ref = authorize() | |
| user_profiles = firebase_ref.child("user_profiles").get() | |
| if user_profiles is None: | |
| return | |
| def reset_map(user_profile_tuple): | |
| panther_id, user_profile = user_profile_tuple | |
| properties_to_remove = [ | |
| "attendance", "rsvps", "lastActivity", "volunteerHours" | |
| ] | |
| for property_to_remove in properties_to_remove: | |
| if property_to_remove in user_profile: | |
| del user_profile[property_to_remove] | |
| return (panther_id, user_profile) | |
| user_profiles_reset = dict(_.map_( | |
| user_profiles.items(), reset_map | |
| )) | |
| firebase_ref.child("user_profiles").put(user_profiles_reset) | |
| """ | |
| Clears comments | |
| @returns void | |
| """ | |
| def comments_reset(): | |
| firebase_ref = authorize() | |
| firebase_ref.child("comments").delete() | |
| """ | |
| Clears the events for each club and events data before a certain | |
| unix epoch of time | |
| @param int unix_milliseconds_timestamp | |
| @returns void | |
| """ | |
| def events_reset(unix_milliseconds_timestamp): | |
| firebase_ref = authorize() | |
| events = firebase_ref.child("events").get() | |
| if events is None: | |
| return | |
| def reset_filter(events_tuple): | |
| event_id, event = events_tuple | |
| if event_id in [ | |
| "attendance", "mmclabswipe", | |
| "bbclabswipe", "studyroomswipe" | |
| ]: | |
| return True | |
| event_date_ends = _.get(event, "date.ends", None) | |
| event_date_starts = _.get(event, "date.starts", None) | |
| if event_date_ends and event_date_starts: | |
| if (event_date_ends <= unix_milliseconds_timestamp or | |
| event_date_starts <= unix_milliseconds_timestamp): | |
| return False | |
| club_name = event.get("clubs") | |
| if isinstance(club_name, basestring) and club_name.strip(): | |
| return False | |
| return True | |
| events_reset = dict(_.filter_( | |
| events.items(), reset_filter | |
| )) | |
| if "attendance" in events_reset: | |
| del events_reset["attendance"] | |
| for event_id in [ | |
| "mmclabswipe", "bbclabswipe", "studyroomswipe" | |
| ]: | |
| if "attendance" in events_reset[event_id]: | |
| del events_reset[event_id]["attendance"] | |
| firebase_ref.child("events").put(events_reset) | |
| """ | |
| Clears volunteerHours | |
| @returns void | |
| """ | |
| def volunteerhours_reset(): | |
| firebase_ref = authorize() | |
| firebase_ref.child("volunteerHours").delete() | |
| if __name__ == "__main__": | |
| userprofiles_reset() | |
| comments_reset() | |
| events_reset(int(EVENTS_NEW_TIMESTAMP)) | |
| volunteerhours_reset() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment