From c4cc51bb1957f77bb57f3346b82be160c16f390f Mon Sep 17 00:00:00 2001 From: Evan Lloyd New-Schmidt Date: Wed, 19 Jul 2017 12:41:56 -0400 Subject: [PATCH 1/4] Add event documents and resource models --- .../event_documents.py} | 9 --- .../event_resources.py} | 70 +------------------ 2 files changed, 1 insertion(+), 78 deletions(-) rename abe/{document_models.py => document_models/event_documents.py} (89%) rename abe/{resource_models.py => resource_models/event_resources.py} (73%) diff --git a/abe/document_models.py b/abe/document_models/event_documents.py similarity index 89% rename from abe/document_models.py rename to abe/document_models/event_documents.py index 951c01e6..b65bbd37 100644 --- a/abe/document_models.py +++ b/abe/document_models/event_documents.py @@ -57,12 +57,3 @@ class Event(Document): meta = {'allow_inheritance': True} # TODO: set indexes # TODO: look into clean() function for more advanced data validation - -class Label(Document): - """Model for labels of events""" - name = StringField(required=True, unique=True) # TODO: set to primary key? - description = StringField() - url = URLField() - -class ICS(Document): - url = StringField() diff --git a/abe/resource_models.py b/abe/resource_models/event_resources.py similarity index 73% rename from abe/resource_models.py rename to abe/resource_models/event_resources.py index 856a3159..8aa43cb4 100644 --- a/abe/resource_models.py +++ b/abe/resource_models/event_resources.py @@ -152,74 +152,6 @@ def delete(self, event_id, rec_id=None): return mongo_to_dict(result) -class LabelApi(Resource): - """API for interacting with all labels (searching, creating)""" - def get(self, label_name=None): - """Retrieve labels""" - if label_name: # use label name/object id if present - logging.debug('Label requested: ' + label_name) - search_fields = ['name', 'id'] - result = multi_search(db.Label, label_name, search_fields) - if not result: - return "Label not found with identifier '{}'".format(label_name), 404 - else: - return mongo_to_dict(result) - else: # search database based on parameters - # TODO: search based on terms - results = db.Label.objects() - if not results: - return [] - else: - return [mongo_to_dict(result) for result in results] - - def post(self): - """Create new label with parameters passed in through args or form""" - received_data = request_to_dict(request) - logging.debug("Received POST data: {}".format(received_data)) - try: - new_label = db.Label(**received_data) - new_label.save() - except ValidationError as error: - logging.warning("POST request rejected: {}".format(str(error))) - return {'error_type': 'validation', - 'validation_errors': [str(err) for err in error.errors], - 'error_message': error.message}, 400 - else: # return success - return mongo_to_dict(new_label), 201 - - def put(self, label_name): - """Modify individual label""" - logging.debug('Label requested: ' + label_name) - search_fields = ['name', 'id'] - result = multi_search(db.Label, label_name, search_fields) - if not result: - return "Label not found with identifier '{}'".format(label_name), 404 - - try: - result.update(**received_data) - result.reload() # load the new document data into the local object - except ValidationError as error: - return {'error_type': 'validation', - 'validation_errors': [str(err) for err in error.errors], - 'error_message': error.message}, 400 - - else: # return success - return mongo_to_dict(result) - - def delete(self, label_name): - """Delete individual label""" - logging.debug('Label requested: ' + label_name) - search_fields = ['name', 'id'] - result = multi_search(db.Label, label_name, search_fields) - if not result: - return "Label not found with identifier '{}'".format(label_name), 404 - - received_data = request_to_dict(request) - logging.debug("Received DELETE data: {}".format(received_data)) - result.delete() - return mongo_to_dict(result) - - class ICSFeed(Resource): """API for interacting with ics feeds""" def get(self, ics_name=None): @@ -245,7 +177,7 @@ def post(self): labels = ['unlabeled'] extract_ics(cal, url['url'], labels) - + def put(self, ics_name): pass From e710bb98d69163ef5de66dff1c01081bffa00dc9 Mon Sep 17 00:00:00 2001 From: Evan Lloyd New-Schmidt Date: Wed, 19 Jul 2017 12:48:36 -0400 Subject: [PATCH 2/4] Add label documents and resource models --- abe/document_models/label_documents.py | 11 +++ abe/resource_models/label_resources.py | 93 ++++++++++++++++++++++++++ 2 files changed, 104 insertions(+) create mode 100644 abe/document_models/label_documents.py create mode 100644 abe/resource_models/label_resources.py diff --git a/abe/document_models/label_documents.py b/abe/document_models/label_documents.py new file mode 100644 index 00000000..ce0c8e18 --- /dev/null +++ b/abe/document_models/label_documents.py @@ -0,0 +1,11 @@ +#!/usr/bin/env python3 +"""Document models for mongoengine""" +from mongoengine import * +from bson import ObjectId + + +class Label(Document): + """Model for labels of events""" + name = StringField(required=True, unique=True) # TODO: set to primary key? + description = StringField() + url = URLField() diff --git a/abe/resource_models/label_resources.py b/abe/resource_models/label_resources.py new file mode 100644 index 00000000..b61a196e --- /dev/null +++ b/abe/resource_models/label_resources.py @@ -0,0 +1,93 @@ +#!/usr/bin/env python3 +"""Resource models for flask""" + +from flask import jsonify, request, abort, Response, make_response +from flask_restful import Resource +from mongoengine import ValidationError +from bson.objectid import ObjectId +from pprint import pprint, pformat +from bson import json_util, objectid +from datetime import datetime, timedelta +from dateutil.rrule import rrule, MONTHLY, WEEKLY, DAILY, YEARLY +from .helpers import ( + mongo_to_dict, request_to_dict, mongo_to_ics, event_query, get_to_event_search, + recurring_to_full, update_sub_event, ics_to_mongo + ) +from icalendar import Calendar +import isodate + +from .helpers import * + +import pdb +import requests + +import logging + +from . import database as db + + +class LabelApi(Resource): + """API for interacting with all labels (searching, creating)""" + def get(self, label_name=None): + """Retrieve labels""" + if label_name: # use label name/object id if present + logging.debug('Label requested: ' + label_name) + search_fields = ['name', 'id'] + result = multi_search(db.Label, label_name, search_fields) + if not result: + return "Label not found with identifier '{}'".format(label_name), 404 + else: + return mongo_to_dict(result) + else: # search database based on parameters + # TODO: search based on terms + results = db.Label.objects() + if not results: + return [] + else: + return [mongo_to_dict(result) for result in results] + + def post(self): + """Create new label with parameters passed in through args or form""" + received_data = request_to_dict(request) + logging.debug("Received POST data: {}".format(received_data)) + try: + new_label = db.Label(**received_data) + new_label.save() + except ValidationError as error: + logging.warning("POST request rejected: {}".format(str(error))) + return {'error_type': 'validation', + 'validation_errors': [str(err) for err in error.errors], + 'error_message': error.message}, 400 + else: # return success + return mongo_to_dict(new_label), 201 + + def put(self, label_name): + """Modify individual label""" + logging.debug('Label requested: ' + label_name) + search_fields = ['name', 'id'] + result = multi_search(db.Label, label_name, search_fields) + if not result: + return "Label not found with identifier '{}'".format(label_name), 404 + + try: + result.update(**received_data) + except ValidationError as error: + return {'error_type': 'validation', + 'validation_errors': [str(err) for err in error.errors], + 'error_message': error.message}, 400 + + else: # return success + return mongo_to_dict(result) + + def delete(self, label_name): + """Delete individual label""" + logging.debug('Label requested: ' + label_name) + search_fields = ['name', 'id'] + result = multi_search(db.Label, label_name, search_fields) + if not result: + return "Label not found with identifier '{}'".format(label_name), 404 + + received_data = request_to_dict(request) + logging.debug("Received DELETE data: {}".format(received_data)) + result.delete() + return mongo_to_dict(result) From 2bc2a886940a39ee21c08152b61abc03e6ec7926 Mon Sep 17 00:00:00 2001 From: Evan Lloyd New-Schmidt Date: Wed, 19 Jul 2017 12:58:12 -0400 Subject: [PATCH 3/4] Fix imports of moved/split modules --- abe/app.py | 3 ++- abe/database.py | 3 ++- abe/resource_models/event_resources.py | 4 ++-- abe/resource_models/label_resources.py | 8 ++------ 4 files changed, 8 insertions(+), 10 deletions(-) diff --git a/abe/app.py b/abe/app.py index c7e65518..b4cba5b5 100644 --- a/abe/app.py +++ b/abe/app.py @@ -11,7 +11,8 @@ FORMAT = "%(levelname)s:ABE: _||_ %(message)s" logging.basicConfig(level=logging.DEBUG, format=FORMAT) -from .resource_models import EventApi, LabelApi, ICSFeed +from .resource_models.event_resources import EventApi, ICSFeed +from .resource_models.label_resources import LabelApi app = Flask(__name__) CORS(app) diff --git a/abe/database.py b/abe/database.py index 1780fd5b..eee9b34e 100644 --- a/abe/database.py +++ b/abe/database.py @@ -1,7 +1,8 @@ #!/usr/bin/env python3 """Connect to mongodb""" from mongoengine import * -from .document_models import Event, Label, RecurringEventExc, ICS +from .document_models.event_documents import Event, RecurringEventExc +from .document_models.label_documents import Label import os import logging logging.basicConfig(level=logging.DEBUG) diff --git a/abe/resource_models/event_resources.py b/abe/resource_models/event_resources.py index 8aa43cb4..9cf65575 100644 --- a/abe/resource_models/event_resources.py +++ b/abe/resource_models/event_resources.py @@ -12,14 +12,14 @@ from icalendar import Calendar import isodate -from .helpers import * +from abe.helpers import * import pdb import requests import logging -from . import database as db +from abe import database as db class EventApi(Resource): diff --git a/abe/resource_models/label_resources.py b/abe/resource_models/label_resources.py index b61a196e..d6454b34 100644 --- a/abe/resource_models/label_resources.py +++ b/abe/resource_models/label_resources.py @@ -9,21 +9,17 @@ from bson import json_util, objectid from datetime import datetime, timedelta from dateutil.rrule import rrule, MONTHLY, WEEKLY, DAILY, YEARLY -from .helpers import ( - mongo_to_dict, request_to_dict, mongo_to_ics, event_query, get_to_event_search, - recurring_to_full, update_sub_event, ics_to_mongo - ) from icalendar import Calendar import isodate -from .helpers import * +from abe.helpers import * import pdb import requests import logging -from . import database as db +from abe import database as db class LabelApi(Resource): From 6a0f9faaa6399cce301272fa9914661185762539 Mon Sep 17 00:00:00 2001 From: Evan Lloyd New-Schmidt Date: Wed, 19 Jul 2017 13:14:17 -0400 Subject: [PATCH 4/4] Add __init__.py to new folders --- abe/document_models/__init__.py | 0 abe/resource_models/__init__.py | 0 2 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 abe/document_models/__init__.py create mode 100644 abe/resource_models/__init__.py diff --git a/abe/document_models/__init__.py b/abe/document_models/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/abe/resource_models/__init__.py b/abe/resource_models/__init__.py new file mode 100644 index 00000000..e69de29b