Browse Source

code cleanup

Davide Alberani 9 years ago
parent
commit
71508d71b3

+ 1 - 1
angular_app/event-edit.html

@@ -1,4 +1,4 @@
-<!-- show details of a single Event (editing also take place here) -->
+<!-- edit details of an Event -->
 <div class="container">
     <h1><span ng-if="!event.title">{{'New event' | translate}}</span>{{event.title}}&nbsp;
         <button ng-if="event._id" ng-click="$state.go('event.info', {id: event._id})" class="btn btn-success">

+ 1 - 0
angular_app/event-info.html

@@ -1,3 +1,4 @@
+<!-- show details of an Event -->
 <div class="container">
     <h1>{{event.title}}
         <button ng-if="event._id" ng-click="$state.go('event.edit', {id: event._id})" class="btn btn-success">

+ 1 - 0
angular_app/event-main.html

@@ -1 +1,2 @@
+<!-- main view for Event -->
 <div ui-view></div>

+ 1 - 1
angular_app/events-list.html

@@ -1,4 +1,4 @@
-<!-- show a list of Events -->
+<!-- show the list of Events -->
 <div class="container">
     <h1>{{'Events' | translate}}
         <button ng-click="$state.go('event.new')" class="btn btn-success">

+ 1 - 0
angular_app/import-main.html

@@ -1 +1,2 @@
+<!-- main view for Import -->
 <div ui-view></div>

+ 1 - 0
angular_app/import-persons.html

@@ -1,3 +1,4 @@
+<!-- import persons -->
 <div class="container">
     <h1>{{'Import persons' | translate}}</h1>
     <div class="panel panel-primary">

+ 2 - 0
angular_app/js/i18n.js

@@ -1,3 +1,5 @@
+/* i18n for Event(man) */
+
 eventManApp.config(['$translateProvider', function ($translateProvider) {
     $translateProvider.translations('it_IT', {
         'Events': 'Eventi',

+ 1 - 1
angular_app/person-edit.html

@@ -1,4 +1,4 @@
-<!-- show details of a single Person (editing also take place here) -->
+<!-- show details of a Person -->
 <div class="container">
     <h1><span ng-if="!(person.name || person.surname)">{{'New person' | translate}}</span>{{person.name}} {{person.surname}}&nbsp;
         <button ng-if="person._id" ng-click="$state.go('person.info', {id: person._id})" class="btn btn-success">

+ 1 - 1
angular_app/person-info.html

@@ -1,4 +1,4 @@
-<!-- show details of a single Person (editing also take place here) -->
+<!-- show details of a Person -->
 <div class="container">
     <h1>{{person.name}} {{person.surname}}
         <button ng-if="person._id" ng-click="$state.go('person.edit', {id: person._id})" class="btn btn-success">

+ 1 - 0
angular_app/person-main.html

@@ -1 +1,2 @@
+<!-- main view for Person -->
 <div ui-view></div>

+ 1 - 1
angular_app/persons-list.html

@@ -1,4 +1,4 @@
-<!-- show a list of Persons -->
+<!-- show the list of Persons -->
 <div class="container">
     <h1>{{'Persons' | translate}}
         <button ng-click="$state.go('person.new')" class="btn btn-success">

+ 40 - 51
backend.py

@@ -57,10 +57,14 @@ class EventManDB(object):
         return self.db
 
     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
+
+        :return: object that can be stored in MongoDB.
         """
+        if obj is None:
+            return None
         try:
             return ObjectId(obj)
         except:
@@ -72,6 +76,13 @@ class EventManDB(object):
         return obj
 
     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):
             d = {}
             for key, item in seq.iteritems():
@@ -122,6 +133,7 @@ class EventManDB(object):
         :rtype: dict
         """
         db = self.connect()
+        data = self.convert(data)
         _id = db[collection].insert(data)
         return self.get(collection, _id)
 
@@ -137,77 +149,54 @@ class EventManDB(object):
         :rtype: bool
         """
         db = self.connect()
+        data = self.convert(data)
         ret = db[collection].update(data, {'$set': data}, upsert=True)
         return ret['updatedExisting']
 
+    def _buildSearchPattern(self, data, searchBy):
+        """Return an OR condition."""
+        _or = []
+        for searchPattern in searchBy:
+            try:
+                _or.append(dict([(k, data[k]) for k in searchPattern if k in data]))
+            except KeyError:
+                continue
+        return _or
+
     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
         :type collection: str
-        :param _id: unique ID of the document to be updated
-        :type _id: str or :class:`~bson.objectid.ObjectId`
+        :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
+        :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: the document, after the update
-        :rtype: dict
+        :return: a boolean (True if an existing document was updated) and the document after the update
+        :rtype: tuple of (bool, 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)}
+        data = self.convert(data or {})
+        _id_or_query = self.convert(_id_or_query)
+        if 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 = []
-        for searchPattern in searchBy:
-            try:
-                _or.append(dict([(k, data[k]) for k in searchPattern]))
-            except KeyError:
-                continue
-        return _or
-
-    def merge(self, collection, data, searchBy, operator='$set'):
-        """Update an existing document.
-
-        :param collection: update a document in this collection
-        :type collection: str
-        :param data: the document to store or merge with an existing one
-        :type data: dict
-
-        :return: a tuple with a boolean (True if an existing document was updated, and the _id of the document)
-        :rtype: tuple
-        """
-        db = self.connect()
-        _or = []
-        for searchPattern in searchBy:
-            try:
-                _or.append(dict([(k, data[k]) for k in searchPattern]))
-            except KeyError:
-                continue
-        if not _or:
-            return False, None
-        # Two-steps merge/find to count the number of merged documents
-        ret = db[collection].update({'$or': _or}, {operator: data}, upsert=True)
-        _id = ret.get('upserted')
-        if _id is None:
-            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):
         """Remove one or more documents from a collection.
 

+ 6 - 15
eventman_server.py

@@ -61,11 +61,8 @@ class CollectionHandler(BaseHandler):
         if resource:
             method = getattr(self, 'handle_get_%s' % resource, None)
             if method and callable(method):
-                try:
-                    self.write(method(id_, resource_id, **kwargs))
-                    return
-                except:
-                    pass
+                self.write(method(id_, resource_id, **kwargs))
+                return
         if id_ is not None:
             # read a single document
             self.write(self.db.get(self.collection, id_))
@@ -82,11 +79,8 @@ class CollectionHandler(BaseHandler):
         if resource:
             method = getattr(self, 'handle_%s_%s' % (self.request.method.lower(), resource), None)
             if method and callable(method):
-                try:
-                    self.write(method(id_, resource_id, data, **kwargs))
-                    return
-                except:
-                    pass
+                self.write(method(id_, resource_id, data, **kwargs))
+                return
         if id_ is None:
             newData = self.db.add(self.collection, data)
         else:
@@ -101,11 +95,8 @@ class CollectionHandler(BaseHandler):
         if resource:
             method = getattr(self, 'handle_delete_%s' % resource, None)
             if method and callable(method):
-                try:
-                    self.write(method(id_, resource_id, **kwargs))
-                    return
-                except:
-                    raise
+                self.write(method(id_, resource_id, **kwargs))
+                return
         self.db.delete(self.collection, id_)
 
 

+ 0 - 1
utils.py

@@ -16,7 +16,6 @@ See the License for the specific language governing permissions and
 limitations under the License.
 """
 
-import re
 import csv
 import json
 import datetime