-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #68 from olinlibrary/newsch/restructuring
Newsch/restructuring
- Loading branch information
Showing
8 changed files
with
107 additions
and
82 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Empty file.
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() |
Empty file.
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
#!/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 icalendar import Calendar | ||
import isodate | ||
|
||
from abe.helpers import * | ||
|
||
import pdb | ||
import requests | ||
|
||
import logging | ||
|
||
from abe 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) |