def category_delete(): # Check if notes name already exists. Normalize the name. category = request.get_json() try: category_id = category.get('category_id') the_category = Category.query.filter_by(id=category_id).first() db.session.delete(the_category) db.session.commit() cache.clear() return {"success": "Category Deleted"} except: return {"error": "Invalid input"}
def category_create(): # Check if Category name already exists. Normalize the name. category = request.get_json() if category: category_name = category.get('name') category_color = category.get('color') new_category = Category( category_name=category_name, category_color=category_color if category_color else None) db.session.add(new_category) db.session.commit() cache.clear() return {"success": "Category added"} else: return {"error": "invalid input"}
def clear_cache(): if helper.is_empty_content_length(request): app.logger.error(f'Exception: {request.content_type}') return make_response(jsonify({'message': 'Payload can not be empty'}), 411) if not helper.is_json_content(request): app.logger.error(f'Exception: {request.content_type}') return make_response(jsonify({'message': 'Unsupported Media Type'}), 415) if request.method == 'POST': try: cache_key = request.json['cache_key'] except KeyError as err: app.logger.error(f'Exception: {err}') return make_response( jsonify({'message': 'Some parameter is missing on request'}), 400) if cache_key is None or not cache_key.strip(): return make_response( jsonify({ 'message': 'You must send cache_key value on payload and it can not be empty' }), 400) cache_key = cache_key.strip() app.logger.info(f'cache_key: {cache_key}') try: if cache_key == 'all': cache.clear() else: cache.delete(cache_key) return make_response(jsonify({'message': 'Success!'}), 200) except Exception as err: app.logger.error(f'Exception: {err}') return make_response( jsonify({'message': 'Error while clearing cache'}), 500)
def test_city_not_found(self): """ Test a not found city and history is empty """ cache.clear() # clear all object in cache data = 'xxxxxxxxxxxxxxxxxx' response = self.app.get('/weather/' + data, headers={"Content-Type": "application/json"}) result = response.json history_response = self.app.get( '/weather', headers={"Content-Type": "application/json"}) history = history_response.json self.assertEqual(result.get('id'), None) # check if the id exists # check if there are cached cities self.assertEqual(len(history.get('history')), 0) self.assertEqual(200, response.status_code) self.assertEqual(200, history_response.status_code)
def notes_delete(): # Check if notes name already exists. Normalize the name. note = request.get_json() token = request.args.get('token') current_user = User.verify_token(token) print(current_user) try: note_id = note.get('note_id') the_note = Notes.query.filter_by(id=note_id).first() if the_note.user_id != current_user.id: raise Exception('credentials invalid') reminder = Reminders.query.filter_by(note_id=note_id).all() for rem in reminder: db.session.delete(rem) db.session.commit() cache.clear() db.session.delete(the_note) db.session.commit() return {"success": "Note Deleted"} except Exception as e: print(e) return {"error": "Invalid input"}
def notes_update(): # Check if notes name already exists. Normalize the name. note = request.get_json() token = request.args.get('token') current_user = User.verify_token(token) try: note_text = note.get('text') note_id = note.get('note_id') due_date = note.get('due_date') due_date = datetime.datetime.strptime(due_date, '%Y-%m-%dT%H:%M:%S.%fZ') the_note = Notes.query.filter_by(id=note_id).first() if the_note.user_id != current_user.id: raise Exception('credentials invalid') the_note.note_text = note_text the_note.due_date = due_date cache.clear() db.session.commit() return {"success": "Note Updated"} except Exception as e: print(e) return {"error": "Invalid input"}
def notes_create(): # Check if notes name already exists. Normalize the name. note = request.get_json() token = request.args.get('token') current_user = User.verify_token(token) if note: note_text = note.get('text') category_id = note.get('category_id') due_date = note.get('due_date', None) public_key = uuid.uuid4().hex user_id = current_user.id if due_date: due_date = datetime.datetime.strptime(due_date, '%Y-%m-%dT%H:%M:%S.%fZ') new_note = Notes(note_text=note_text, user_id=user_id, public_key=public_key, category_id=category_id, due_date=due_date) db.session.add(new_note) db.session.commit() reminder = Reminders(note=new_note, reminder_time=due_date, reminder_sent=False) db.session.add(new_note) db.session.commit() else: new_note = Notes(note_text=note_text, user_id=user_id, public_key=public_key, category_id=category_id) db.session.add(new_note) db.session.commit() cache.clear() return {"success": "Note added"} else: return {"error": "Invalid Input"}