示例#1
0
 def document_date_range_only_search(self, cleaned_document_keys, docrule_ids):
     log.debug('Date range search only')
     resp_list = []
     startkey = [None,]
     endkey = [None,]
     for docrule_id in docrule_ids:
         startkey = [docrule_id, str_date_to_couch(cleaned_document_keys["date"])]
         endkey = [docrule_id, str_date_to_couch(cleaned_document_keys["end_date"])]
         # Getting all documents withing this date range
         all_docs = CouchDocument.view(
             'dmscouch/search_date',
             classes={None: CouchDocument},
             startkey=startkey,
             endkey=endkey
         )
         # Appending to fetch docs list if not already there
         for doc in all_docs:
             doc_name = doc.get_id
             if not doc_name in resp_list:
                 resp_list.append(doc_name)
     if resp_list:
         log_data = resp_list.__len__()
     else:
         log_data = None
     log.debug(
         'Search results by date range: from: "%s", to: "%s", docrules: "%s", documents: "%s"' %
         (startkey[0], endkey[0], docrule_ids, log_data)
     )
     return resp_list
示例#2
0
 def document_date_range_only_search(self, cleaned_document_keys,
                                     docrule_ids):
     log.debug('Date range search only')
     resp_list = []
     startkey = [
         None,
     ]
     endkey = [
         None,
     ]
     for docrule_id in docrule_ids:
         startkey = [
             docrule_id,
             str_date_to_couch(cleaned_document_keys["date"])
         ]
         endkey = [
             docrule_id,
             str_date_to_couch(cleaned_document_keys["end_date"])
         ]
         # Getting all documents withing this date range
         all_docs = CouchDocument.view('dmscouch/search_date',
                                       classes={None: CouchDocument},
                                       startkey=startkey,
                                       endkey=endkey)
         # Appending to fetch docs list if not already there
         for doc in all_docs:
             doc_name = doc.get_id
             if not doc_name in resp_list:
                 resp_list.append(doc_name)
     if resp_list:
         log_data = resp_list.__len__()
     else:
         log_data = None
     log.debug(
         'Search results by date range: from: "%s", to: "%s", docrules: "%s", documents: "%s"'
         % (startkey[0], endkey[0], docrule_ids, log_data))
     return resp_list
示例#3
0
def unify_index_info_couch_dates_fmt(index_info):
    """
    Applies standardization to secondary keys 'date' type keys.
    """
    clean_info = {}
    index_keys = [key for key in index_info.iterkeys()]
    for index_key in index_keys:
        if not index_key=='date':
            try:
                value = index_info[index_key]
                # Simple check if we can convert it...
                datetime.datetime.strptime(value, settings.DATE_FORMAT)
                clean_info[index_key] = str_date_to_couch(value)
            except ValueError:
                clean_info[index_key] = index_info[index_key]
                pass
        else:
            clean_info[index_key] = index_info[index_key]
    return clean_info
示例#4
0
def unify_index_info_couch_dates_fmt(index_info):
    """Applies standardization to secondary keys 'date' type keys.

    @param index_info: is a index data to workout for
    """
    clean_info = {}
    index_keys = [key for key in index_info.iterkeys()]
    for index_key in index_keys:
        if not index_key == 'date':
            try:
                value = index_info[index_key]
                # Simple check if we can convert it...
                datetime.datetime.strptime(value, settings.DATE_FORMAT)
                clean_info[index_key] = str_date_to_couch(value)
            except ValueError:
                clean_info[index_key] = index_info[index_key]
                pass
        else:
            clean_info[index_key] = index_info[index_key]
    return clean_info
示例#5
0
    def convert_to_search_keys_for_date_range(self, document_keys, pkey, docrule_id, end=False, date_range=False):
        """
        Makes proper keys request set for 'dmscouch/search' CouchDB view.

        Takes date range into account.
        """
        req_params = []
        dd_range = self.document_date_range_present_in_keys(document_keys)
        for key, value in document_keys.iteritems():
            if key == pkey:
                if not date_range:
                    # Simple key DB search request for document dates (or without document dates) range
                    if not dd_range:
                        req_params = [key, value, docrule_id]
                    else:
                        if not end:
                            req_params = [key, value, docrule_id, str_date_to_couch(document_keys["date"])]
                        else:
                            end_date = self.alter_end_date(document_keys["end_date"])
                            req_params = [key, value, docrule_id, str_date_to_couch(end_date)]
                else:
                    # Assuming date range is our date tuple
                    # Creating DB request for document dates (or without document dates) range
                    # Adding 1 day to date range finish to complu CouchDB search conditions and include finish date results.
                    if not end:
                        if dd_range:
                            req_params = [key, str_date_to_couch(value[0]), docrule_id, str_date_to_couch(document_keys["date"])]
                        else:
                            req_params = [key, str_date_to_couch(value[0]), docrule_id]
                    else:
                        end_key_date = self.alter_end_date(value[1])
                        if dd_range:
                            end_date = self.alter_end_date(document_keys["end_date"])
                            req_params = [key, str_date_to_couch(end_key_date), docrule_id, str_date_to_couch(end_date)]
                        else:
                            req_params = [key, str_date_to_couch(end_key_date), docrule_id]
        return req_params
示例#6
0
    def update_indexes_revision(self, document):
        """Updates CouchDB document with new revision of indexing data.

        @param document: DMS Document() instance

        Old indexing data is stored in revision. E.g.:
        Document only created:

            couchdoc.index_revisions = None

        Document updated once:

            couchdoc.index_revisions = { '1': { ... }, }

        Document updated again and farther:

            couchdoc.index_revisions = {
                '1': { ... },
                '2': { ... },
                ...
            }

        This method handles storing of indexing data changes (old one's) are stored into revisions.
        New data are populated into couchdoc thus making them current.
        """
        if document.new_indexes:
            # Creating clean self.mdt_indexes
            secondary_indexes = {}
            for secondary_index_name, secondary_index_value in document.new_indexes.iteritems():
                if not secondary_index_name in ['description', 'metadata_user_name', 'metadata_user_id',]:
                    # Converting date format to couch if secondary index is DMS date type
                    try:
                        datetime.strptime(secondary_index_value, settings.DATE_FORMAT)
                        secondary_indexes[secondary_index_name] = str_date_to_couch(secondary_index_value)
                    except ValueError:
                        secondary_indexes[secondary_index_name] = secondary_index_value
                        pass
            # Only for update without docrule change (it makes it's own indexes backup)
            if not document.old_docrule:
                # Storing current index data into new revision
                if not 'index_revisions' in self:
                    # Creating index_revisions initial data dictionary.
                    self.index_revisions = {'1': self.construct_index_revision_dict(), }
                else:
                    # Appending new document indexes revision to revisions dict
                    new_revision = self.index_revisions.__len__() + 1
                    self.index_revisions[new_revision] = self.construct_index_revision_dict()
            # Populating self with new provided data
            self.mdt_indexes = secondary_indexes
            # Making desc and user data optional, taking them from current user
            if 'description' in document.new_indexes:
                self.metadata_description = document.new_indexes['description']
            else:
                self.metadata_description = 'N/A'
            if 'metadata_user_id' in document.new_indexes:
                self.metadata_user_id = document.new_indexes['metadata_user_id']
            else:
                self.metadata_user_id = unicode(document.user.id)
            if 'metadata_user_name' in document.new_indexes:
                self.metadata_user_id = document.new_indexes['metadata_user_name']
            else:
                self.metadata_user_name = document.user.username
        return document
示例#7
0
    def convert_to_search_keys_for_date_range(self,
                                              document_keys,
                                              pkey,
                                              docrule_id,
                                              end=False,
                                              date_range=False):
        """
        Makes proper keys request set for 'dmscouch/search' CouchDB view.

        Takes date range into account.
        """
        req_params = []
        dd_range = self.document_date_range_present_in_keys(document_keys)
        for key, value in document_keys.iteritems():
            if key == pkey:
                if not date_range:
                    # Simple key DB search request for document dates (or without document dates) range
                    if not dd_range:
                        req_params = [key, value, docrule_id]
                    else:
                        if not end:
                            req_params = [
                                key, value, docrule_id,
                                str_date_to_couch(document_keys["date"])
                            ]
                        else:
                            end_date = self.alter_end_date(
                                document_keys["end_date"])
                            req_params = [
                                key, value, docrule_id,
                                str_date_to_couch(end_date)
                            ]
                else:
                    # Assuming date range is our date tuple
                    # Creating DB request for document dates (or without document dates) range
                    # Adding 1 day to date range finish to complu CouchDB search conditions and include finish date results.
                    if not end:
                        if dd_range:
                            req_params = [
                                key,
                                str_date_to_couch(value[0]), docrule_id,
                                str_date_to_couch(document_keys["date"])
                            ]
                        else:
                            req_params = [
                                key,
                                str_date_to_couch(value[0]), docrule_id
                            ]
                    else:
                        end_key_date = self.alter_end_date(value[1])
                        if dd_range:
                            end_date = self.alter_end_date(
                                document_keys["end_date"])
                            req_params = [
                                key,
                                str_date_to_couch(end_key_date), docrule_id,
                                str_date_to_couch(end_date)
                            ]
                        else:
                            req_params = [
                                key,
                                str_date_to_couch(end_key_date), docrule_id
                            ]
        return req_params
示例#8
0
    def update_indexes_revision(self, document):
        """Updates CouchDB document with new revision of indexing data.

        @param document: DMS Document() instance

        Old indexing data is stored in revision. E.g.:
        Document only created:

            couchdoc.index_revisions = None

        Document updated once:

            couchdoc.index_revisions = { '1': { ... }, }

        Document updated again and farther:

            couchdoc.index_revisions = {
                '1': { ... },
                '2': { ... },
                ...
            }

        This method handles storing of indexing data changes (old one's) are stored into revisions.
        New data are populated into couchdoc thus making them current.
        """
        if document.new_indexes:
            # Creating clean self.mdt_indexes
            secondary_indexes = {}
            for secondary_index_name, secondary_index_value in document.new_indexes.iteritems(
            ):
                if not secondary_index_name in [
                        'description',
                        'metadata_user_name',
                        'metadata_user_id',
                ]:
                    # Converting date format to couch if secondary index is DMS date type
                    try:
                        datetime.strptime(secondary_index_value,
                                          settings.DATE_FORMAT)
                        secondary_indexes[
                            secondary_index_name] = str_date_to_couch(
                                secondary_index_value)
                    except ValueError:
                        secondary_indexes[
                            secondary_index_name] = secondary_index_value
                        pass
            # Only for update without docrule change (it makes it's own indexes backup)
            if not document.old_docrule:
                # Storing current index data into new revision
                if not 'index_revisions' in self:
                    # Creating index_revisions initial data dictionary.
                    self.index_revisions = {
                        '1': self.construct_index_revision_dict(),
                    }
                else:
                    # Appending new document indexes revision to revisions dict
                    new_revision = self.index_revisions.__len__() + 1
                    self.index_revisions[
                        new_revision] = self.construct_index_revision_dict()
            # Populating self with new provided data
            self.mdt_indexes = secondary_indexes
            # Making desc and user data optional, taking them from current user
            if 'description' in document.new_indexes:
                self.metadata_description = document.new_indexes['description']
            else:
                self.metadata_description = 'N/A'
            if 'metadata_user_id' in document.new_indexes:
                self.metadata_user_id = document.new_indexes[
                    'metadata_user_id']
            else:
                self.metadata_user_id = unicode(document.user.id)
            if 'metadata_user_name' in document.new_indexes:
                self.metadata_user_id = document.new_indexes[
                    'metadata_user_name']
            else:
                self.metadata_user_name = document.user.username
        return document