def update_theme(release_time, theme, theme_inspire, theme_author): # don't allow themes to be edited after 5 minutes prior to it's release time. if (datetime.now() - release_time > timedelta(minutes=5)): return "What has past is past. Live the present, anticipate the future, and embrace the past" if (release_time.hour != 6 and release_time.hour != 20 ) or release_time.minute != 30 or release_time.second != 0: return "Invalid release time." by_release_time = ThemeModel.find_by_release_time(release_time) if not by_release_time: return "Create theme for the release time first." by_theme = ThemeModel.find_by_theme(theme) if by_theme: if by_theme.id != by_release_time.id: return "Theme attempting to edit is already taken. Try a new theme." # update by_release_time.theme = theme by_release_time.theme_inspire = theme_inspire by_release_time.theme_author = theme_author try: by_release_time.save_to_db() except: return "Error saving updated theme." return ""
def get_for_day(year, month, day): release1 = datetime(year, month, day, 6, 30, 0) release2 = datetime(year, month, day, 20, 30, 0) result = [] try: result.append(ThemeModel.find_by_release_time(release1)) result.append(ThemeModel.find_by_release_time(release2)) except: return "Error fetching ThemeModel for the specified day.", None return "", result
def browse(index): current = datetime.now() if index < 0: return "Cannot go to the future.", None elif index > ThemeModel.get_count(current): return "No more themes.", None else: wanted_time = current + timedelta(days=index) list_of_themes = ThemeModel.list_below_by_release_time(wanted_time) if not list_of_themes: return "No more themes.", None return "", list_of_themes
def post(self): # parse_args() return only arguments added by add_argument as Namespace # Any missing added argument will stop and return help message to the browser data = Theme.parser.parse_args() # data namespace is rolled into one argument (**data) theme = ThemeModel(**data) try: theme.save_to_db() except: return {"message": "An error occurred inserting the item."}, 500 return theme.json(), 201
def delete(self, theme_id): theme = ThemeModel.find_by_id(theme_id) if theme: theme.delete_from_db() return {'message': 'Theme deleted'} return {'message': 'Theme not found'}
def update_theme(release_time, theme, theme_inspire, theme_author): if (release_time.hour != 6 and release_time.hour != 20 ) or release_time.minute != 30 or release_time.second != 0: return "Incorrect release time." if ThemeModel.find_by_release_time(release_time): return "Theme is already there for that time! Try PUT for edit." target_theme = ThemeModel.find_by_theme(theme) if not target_theme: return "Theme could not be found." target_theme.release_time = release_time target_theme.theme = theme target_theme.theme_inspire = theme_inspire target_theme.theme_author = theme_author try: target_theme.save_to_db() except: return "Error saving updated theme." return ""
def put(self, year, week): data = Theme.parser.parse_args() theme = ThemeModel.find_by_week(year, week) if theme is None: theme = ThemeModel(year, week, **data) else: theme.week_focus = data['week_focus'] theme.month = data['month'] theme.month_theme = data['month_theme'] theme.save_to_db() return theme.json()
def delete(self, year, week): claims = get_jwt_claims() if not claims['is_admin']: return {'message': 'Admin privilege required.'}, 401 theme = ThemeModel.find_by_week(year, week) if theme: theme.delete_from_db() return ({'message': 'Theme Deleted'}), 202 # deleted return ({'message': 'Theme for week {} not found'.format(week)}), 404
def browse(days, client_id): # need to get client_id from the Resouce(view) current = datetime.now() quantity = days * 2 if quantity < 0: return "Cannot go to the future.", None elif quantity >= ThemeModel.get_count( current) or quantity < ThemeModel.get_count(current): quantity = ThemeModel.get_count(current) lowbound_time = current - timedelta(days=days) highbound_time = current list_of_themes = ThemeModel.list_between_by_release_time( lowbound_time, highbound_time, quantity) new_list_of_themes = [] # print(list_of_themes) # 2EA # print(list_of_themes[1].json()) #1 if not list_of_themes: return "No more themes.", None # call UserModel.written through client_id target = UserModel.find_by_id(client_id) # print(target.written[0].json()) for theme in list_of_themes: json_written = theme.json() for post in target.written: if theme.theme == post.theme: json_written.update({'written': True}) else: json_written.update({'written': False}) new_list_of_themes.append(json_written) print(new_list_of_themes) # print(json_written) # return "", list_of_themes return "", new_list_of_themes
def put(self, theme_id): data = Theme.parser.parse_args() theme = ThemeModel.find_by_id(theme_id) if theme is None: # Create a new theme if it does not exist in the database theme = ThemeModel(**data) else: # Update the theme if it exists in the database theme.theme_name = data['theme_name'] theme.theme_update = datetime.now() theme.save_to_db() return theme.json()
def filter_by_theme(theme): """ checks if input is valid by alphabet or number, and not blank returns all the post models for that theme """ if not theme: return "Theme is needed", None theme_nospace = theme.replace(" ", "") if not theme_nospace.isalnum(): return "Theme should only be consisted of alphabets, numbers, and spaces", None if not ThemeModel.find_by_theme(theme): return "Theme doesn't exist.", None try: return "", PostModel.filter_by_theme(theme) except: return "Error in getting most saved", None
def create_post(theme, anonymity, writer_id, content): # check if that theme exists # check if that username exists # check if content is less than characters writer = UserModel.find_by_id(writer_id) if not ThemeModel.find_by_theme(theme): return "The post's theme doesn't exist.", None if not writer: return "The writer is not a user", None if len(content) > 150: return "The content is too long", None try: new_post = PostModel(theme, anonymity, writer_id, content) new_post.save_to_db() except: return "Error saving to db", None return ""
def post(self, year, week): if ThemeModel.find_by_week(year, week): return { 'message': "A theme for week '{}' already exists.".format(week) }, 400 data = Theme.parser.parse_args() theme = ThemeModel(year, week, **data) try: theme.save_to_db() except: return {"message": "An error occurred inserting the Theme."}, 500 return theme.json(), 201 # created
def create_theme(release_time, theme, theme_inspire, theme_author): if (release_time.hour != 6 and release_time.hour != 20 ) or release_time.minute != 30 or release_time.second != 0: return "Incorrect release time." if ThemeModel.find_by_release_time(release_time): return "Theme is already there for that time! Try PUT for edit." if ThemeModel.find_by_theme(theme): return "Theme has alrady been used." try: new_theme = ThemeModel(release_time, theme, theme_inspire, theme_author) new_theme.save_to_db() return "" except: return "Error in creating and saving theme."
def get_now(): # retrieve fitting theme for the time 6:30 or 20:30 current = datetime.now() release1 = datetime(current.year, current.month, current.day, 6, 30, 0) release2 = datetime(current.year, current.month, current.day, 20, 30, 0) yest_release2 = release2 - timedelta(days=1) tmr_release1 = release1 + timedelta(days=1) if current < release1: wanted_theme_time = yesterday_release2 elif current >= release1 and current < release2: wanted_theme_time = release1 elif current > release2 and current < tmr_release1: wanted_theme_time = release2 else: return "Invalid time.", None wanted_theme_model = ThemeModel.find_by_release_time(wanted_theme_time) if not wanted_theme_model: return "Daily theme not available today.", None else: return "", wanted_theme_model
def get(self, theme_id): theme = ThemeModel.find_by_id(theme_id) if theme: return theme.json() return {'message': 'Theme not found'}, 404
def get(self, year, week): theme = ThemeModel.find_by_week(year, week) if theme: return theme.json() return {'message': 'Theme for week {} not found'.format(week)}, 404
def delete(self): ThemeModel.delete_all() return {'message': 'All themes deleted'}
def get(self): return {'themes': ThemeModel.query_all()}
def get(self, year): theme_list = [ theme.json() for theme in ThemeModel.find_theme_by_year(year) ] return {'Theme List': theme_list}, 200