code cleanup
This commit is contained in:
parent
a81eb33b69
commit
71508d71b3
14 changed files with 61 additions and 75 deletions
|
@ -1,4 +1,4 @@
|
||||||
<!-- show details of a single Event (editing also take place here) -->
|
<!-- edit details of an Event -->
|
||||||
<div class="container">
|
<div class="container">
|
||||||
<h1><span ng-if="!event.title">{{'New event' | translate}}</span>{{event.title}}
|
<h1><span ng-if="!event.title">{{'New event' | translate}}</span>{{event.title}}
|
||||||
<button ng-if="event._id" ng-click="$state.go('event.info', {id: event._id})" class="btn btn-success">
|
<button ng-if="event._id" ng-click="$state.go('event.info', {id: event._id})" class="btn btn-success">
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
<!-- show details of an Event -->
|
||||||
<div class="container">
|
<div class="container">
|
||||||
<h1>{{event.title}}
|
<h1>{{event.title}}
|
||||||
<button ng-if="event._id" ng-click="$state.go('event.edit', {id: event._id})" class="btn btn-success">
|
<button ng-if="event._id" ng-click="$state.go('event.edit', {id: event._id})" class="btn btn-success">
|
||||||
|
|
|
@ -1 +1,2 @@
|
||||||
|
<!-- main view for Event -->
|
||||||
<div ui-view></div>
|
<div ui-view></div>
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
<!-- show a list of Events -->
|
<!-- show the list of Events -->
|
||||||
<div class="container">
|
<div class="container">
|
||||||
<h1>{{'Events' | translate}}
|
<h1>{{'Events' | translate}}
|
||||||
<button ng-click="$state.go('event.new')" class="btn btn-success">
|
<button ng-click="$state.go('event.new')" class="btn btn-success">
|
||||||
|
|
|
@ -1 +1,2 @@
|
||||||
|
<!-- main view for Import -->
|
||||||
<div ui-view></div>
|
<div ui-view></div>
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
<!-- import persons -->
|
||||||
<div class="container">
|
<div class="container">
|
||||||
<h1>{{'Import persons' | translate}}</h1>
|
<h1>{{'Import persons' | translate}}</h1>
|
||||||
<div class="panel panel-primary">
|
<div class="panel panel-primary">
|
||||||
|
|
2
angular_app/js/i18n.js
vendored
2
angular_app/js/i18n.js
vendored
|
@ -1,3 +1,5 @@
|
||||||
|
/* i18n for Event(man) */
|
||||||
|
|
||||||
eventManApp.config(['$translateProvider', function ($translateProvider) {
|
eventManApp.config(['$translateProvider', function ($translateProvider) {
|
||||||
$translateProvider.translations('it_IT', {
|
$translateProvider.translations('it_IT', {
|
||||||
'Events': 'Eventi',
|
'Events': 'Eventi',
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
<!-- show details of a single Person (editing also take place here) -->
|
<!-- show details of a Person -->
|
||||||
<div class="container">
|
<div class="container">
|
||||||
<h1><span ng-if="!(person.name || person.surname)">{{'New person' | translate}}</span>{{person.name}} {{person.surname}}
|
<h1><span ng-if="!(person.name || person.surname)">{{'New person' | translate}}</span>{{person.name}} {{person.surname}}
|
||||||
<button ng-if="person._id" ng-click="$state.go('person.info', {id: person._id})" class="btn btn-success">
|
<button ng-if="person._id" ng-click="$state.go('person.info', {id: person._id})" class="btn btn-success">
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
<!-- show details of a single Person (editing also take place here) -->
|
<!-- show details of a Person -->
|
||||||
<div class="container">
|
<div class="container">
|
||||||
<h1>{{person.name}} {{person.surname}}
|
<h1>{{person.name}} {{person.surname}}
|
||||||
<button ng-if="person._id" ng-click="$state.go('person.edit', {id: person._id})" class="btn btn-success">
|
<button ng-if="person._id" ng-click="$state.go('person.edit', {id: person._id})" class="btn btn-success">
|
||||||
|
|
|
@ -1 +1,2 @@
|
||||||
|
<!-- main view for Person -->
|
||||||
<div ui-view></div>
|
<div ui-view></div>
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
<!-- show a list of Persons -->
|
<!-- show the list of Persons -->
|
||||||
<div class="container">
|
<div class="container">
|
||||||
<h1>{{'Persons' | translate}}
|
<h1>{{'Persons' | translate}}
|
||||||
<button ng-click="$state.go('person.new')" class="btn btn-success">
|
<button ng-click="$state.go('person.new')" class="btn btn-success">
|
||||||
|
|
97
backend.py
97
backend.py
|
@ -57,10 +57,14 @@ class EventManDB(object):
|
||||||
return self.db
|
return self.db
|
||||||
|
|
||||||
def convert_obj(self, obj):
|
def convert_obj(self, obj):
|
||||||
"""Convert a string to an object for MongoDB.
|
"""Convert an object in a format suitable to be stored in MongoDB.
|
||||||
|
|
||||||
:param obj: object to convert
|
:param obj: object to convert
|
||||||
|
|
||||||
|
:return: object that can be stored in MongoDB.
|
||||||
"""
|
"""
|
||||||
|
if obj is None:
|
||||||
|
return None
|
||||||
try:
|
try:
|
||||||
return ObjectId(obj)
|
return ObjectId(obj)
|
||||||
except:
|
except:
|
||||||
|
@ -72,6 +76,13 @@ class EventManDB(object):
|
||||||
return obj
|
return obj
|
||||||
|
|
||||||
def convert(self, seq):
|
def convert(self, seq):
|
||||||
|
"""Convert an object in a format suitable to be stored in MongoDB,
|
||||||
|
descending lists, tuples and dictionaries (a copy is returned).
|
||||||
|
|
||||||
|
:param seq: sequence or object to convert
|
||||||
|
|
||||||
|
:return: object that can be stored in MongoDB.
|
||||||
|
"""
|
||||||
if isinstance(seq, dict):
|
if isinstance(seq, dict):
|
||||||
d = {}
|
d = {}
|
||||||
for key, item in seq.iteritems():
|
for key, item in seq.iteritems():
|
||||||
|
@ -122,6 +133,7 @@ class EventManDB(object):
|
||||||
:rtype: dict
|
:rtype: dict
|
||||||
"""
|
"""
|
||||||
db = self.connect()
|
db = self.connect()
|
||||||
|
data = self.convert(data)
|
||||||
_id = db[collection].insert(data)
|
_id = db[collection].insert(data)
|
||||||
return self.get(collection, _id)
|
return self.get(collection, _id)
|
||||||
|
|
||||||
|
@ -137,76 +149,53 @@ class EventManDB(object):
|
||||||
:rtype: bool
|
:rtype: bool
|
||||||
"""
|
"""
|
||||||
db = self.connect()
|
db = self.connect()
|
||||||
|
data = self.convert(data)
|
||||||
ret = db[collection].update(data, {'$set': data}, upsert=True)
|
ret = db[collection].update(data, {'$set': data}, upsert=True)
|
||||||
return ret['updatedExisting']
|
return ret['updatedExisting']
|
||||||
|
|
||||||
def update(self, collection, _id_or_query, data, operator='$set', create=True):
|
def _buildSearchPattern(self, data, searchBy):
|
||||||
"""Update an existing document.
|
"""Return an OR condition."""
|
||||||
|
|
||||||
:param collection: update a document in this collection
|
|
||||||
:type collection: str
|
|
||||||
:param _id: unique ID of the document to be updated
|
|
||||||
:type _id: str or :class:`~bson.objectid.ObjectId`
|
|
||||||
:param data: the updated information to store
|
|
||||||
:type data: dict
|
|
||||||
|
|
||||||
:return: the document, after the update
|
|
||||||
:rtype: dict
|
|
||||||
"""
|
|
||||||
db = self.connect()
|
|
||||||
data = data or {}
|
|
||||||
if _id_or_query is None:
|
|
||||||
_id_or_query = {'_id': None}
|
|
||||||
elif isinstance(_id_or_query, (list, tuple)):
|
|
||||||
_id_or_query = {'$or': self.buildSearchPattern(data, _id_or_query)}
|
|
||||||
elif not isinstance(_id_or_query, dict):
|
|
||||||
_id_or_query = {'_id': _id_or_query}
|
|
||||||
_id_or_query = self.convert(_id_or_query)
|
|
||||||
if '_id' in data:
|
|
||||||
del data['_id']
|
|
||||||
data = self.convert(data)
|
|
||||||
res = db[collection].find_and_modify(query=_id_or_query,
|
|
||||||
update={operator: data}, full_response=True, new=True, upsert=create)
|
|
||||||
lastErrorObject = res.get('lastErrorObject') or {}
|
|
||||||
return lastErrorObject.get('updatedExisting', False), res.get('value') or {}
|
|
||||||
|
|
||||||
def buildSearchPattern(self, data, searchBy):
|
|
||||||
_or = []
|
_or = []
|
||||||
for searchPattern in searchBy:
|
for searchPattern in searchBy:
|
||||||
try:
|
try:
|
||||||
_or.append(dict([(k, data[k]) for k in searchPattern]))
|
_or.append(dict([(k, data[k]) for k in searchPattern if k in data]))
|
||||||
except KeyError:
|
except KeyError:
|
||||||
continue
|
continue
|
||||||
return _or
|
return _or
|
||||||
|
|
||||||
def merge(self, collection, data, searchBy, operator='$set'):
|
def update(self, collection, _id_or_query, data, operator='$set', create=True):
|
||||||
"""Update an existing document.
|
"""Update an existing document or create it, if requested.
|
||||||
|
_id_or_query can be an ID, a dict representing a query or a list of tuples.
|
||||||
|
In the latter case, the tuples are put in OR; a tuple match if all of its
|
||||||
|
items from 'data' are contained in the document.
|
||||||
|
|
||||||
:param collection: update a document in this collection
|
:param collection: update a document in this collection
|
||||||
:type collection: str
|
:type collection: str
|
||||||
:param data: the document to store or merge with an existing one
|
:param _id_or_query: ID of the document to be updated, or a query or a list of attributes in the data that must match
|
||||||
|
:type _id_or_query: str or :class:`~bson.objectid.ObjectId` or iterable
|
||||||
|
:param data: the updated information to store
|
||||||
:type data: dict
|
:type data: dict
|
||||||
|
:param operator: operator used to update the document
|
||||||
|
:type operator: str
|
||||||
|
:param create: if True, the document is created if no document matches
|
||||||
|
:type create: bool
|
||||||
|
|
||||||
:return: a tuple with a boolean (True if an existing document was updated, and the _id of the document)
|
:return: a boolean (True if an existing document was updated) and the document after the update
|
||||||
:rtype: tuple
|
:rtype: tuple of (bool, dict)
|
||||||
"""
|
"""
|
||||||
db = self.connect()
|
db = self.connect()
|
||||||
_or = []
|
data = self.convert(data or {})
|
||||||
for searchPattern in searchBy:
|
_id_or_query = self.convert(_id_or_query)
|
||||||
try:
|
if isinstance(_id_or_query, (list, tuple)):
|
||||||
_or.append(dict([(k, data[k]) for k in searchPattern]))
|
_id_or_query = {'$or': self._buildSearchPattern(data, _id_or_query)}
|
||||||
except KeyError:
|
elif not isinstance(_id_or_query, dict):
|
||||||
continue
|
_id_or_query = {'_id': _id_or_query}
|
||||||
if not _or:
|
if '_id' in data:
|
||||||
return False, None
|
del data['_id']
|
||||||
# Two-steps merge/find to count the number of merged documents
|
res = db[collection].find_and_modify(query=_id_or_query,
|
||||||
ret = db[collection].update({'$or': _or}, {operator: data}, upsert=True)
|
update={operator: data}, full_response=True, new=True, upsert=create)
|
||||||
_id = ret.get('upserted')
|
lastErrorObject = res.get('lastErrorObject') or {}
|
||||||
if _id is None:
|
return lastErrorObject.get('updatedExisting', False), res.get('value') or {}
|
||||||
newDoc = db[collection].find_one(data)
|
|
||||||
if newDoc:
|
|
||||||
_id = newDoc['_id']
|
|
||||||
return ret['updatedExisting'], _id
|
|
||||||
|
|
||||||
def delete(self, collection, _id_or_query=None, force=False):
|
def delete(self, collection, _id_or_query=None, force=False):
|
||||||
"""Remove one or more documents from a collection.
|
"""Remove one or more documents from a collection.
|
||||||
|
|
|
@ -61,11 +61,8 @@ class CollectionHandler(BaseHandler):
|
||||||
if resource:
|
if resource:
|
||||||
method = getattr(self, 'handle_get_%s' % resource, None)
|
method = getattr(self, 'handle_get_%s' % resource, None)
|
||||||
if method and callable(method):
|
if method and callable(method):
|
||||||
try:
|
self.write(method(id_, resource_id, **kwargs))
|
||||||
self.write(method(id_, resource_id, **kwargs))
|
return
|
||||||
return
|
|
||||||
except:
|
|
||||||
pass
|
|
||||||
if id_ is not None:
|
if id_ is not None:
|
||||||
# read a single document
|
# read a single document
|
||||||
self.write(self.db.get(self.collection, id_))
|
self.write(self.db.get(self.collection, id_))
|
||||||
|
@ -82,11 +79,8 @@ class CollectionHandler(BaseHandler):
|
||||||
if resource:
|
if resource:
|
||||||
method = getattr(self, 'handle_%s_%s' % (self.request.method.lower(), resource), None)
|
method = getattr(self, 'handle_%s_%s' % (self.request.method.lower(), resource), None)
|
||||||
if method and callable(method):
|
if method and callable(method):
|
||||||
try:
|
self.write(method(id_, resource_id, data, **kwargs))
|
||||||
self.write(method(id_, resource_id, data, **kwargs))
|
return
|
||||||
return
|
|
||||||
except:
|
|
||||||
pass
|
|
||||||
if id_ is None:
|
if id_ is None:
|
||||||
newData = self.db.add(self.collection, data)
|
newData = self.db.add(self.collection, data)
|
||||||
else:
|
else:
|
||||||
|
@ -101,11 +95,8 @@ class CollectionHandler(BaseHandler):
|
||||||
if resource:
|
if resource:
|
||||||
method = getattr(self, 'handle_delete_%s' % resource, None)
|
method = getattr(self, 'handle_delete_%s' % resource, None)
|
||||||
if method and callable(method):
|
if method and callable(method):
|
||||||
try:
|
self.write(method(id_, resource_id, **kwargs))
|
||||||
self.write(method(id_, resource_id, **kwargs))
|
return
|
||||||
return
|
|
||||||
except:
|
|
||||||
raise
|
|
||||||
self.db.delete(self.collection, id_)
|
self.db.delete(self.collection, id_)
|
||||||
|
|
||||||
|
|
||||||
|
|
1
utils.py
1
utils.py
|
@ -16,7 +16,6 @@ See the License for the specific language governing permissions and
|
||||||
limitations under the License.
|
limitations under the License.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import re
|
|
||||||
import csv
|
import csv
|
||||||
import json
|
import json
|
||||||
import datetime
|
import datetime
|
||||||
|
|
Loading…
Reference in a new issue