callbacks.py 5.14 KB
Newer Older
LE GAC Renaud's avatar
LE GAC Renaud committed
1 2 3 4 5 6 7 8 9 10
"""a collections of functions to be used in _before_delete, before_insert
and _before_update callbacks.

@author: R. Le Gac

"""
from check_tools import (duplicate_article,
                         duplicate_conference,
                         duplicate_report)
from gluon import current
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
from plugin_dbui import (CALLBACK_ERRORS, 
                         get_id, 
                         get_where_query)


def INHIBIT_CASCADE_DELETE(set):
    """Inhibit the delete when publications use the reference field.
    
    @type set: gluon.dal.Set
    @param set:
    
    @rtype: bool
    @return:
    
    """
    db, T = current.globalenv['db'], current.T
    field = set.query.first
     
    # protection
    # the query of the set should be "table.id == 45"
    if field._db._adapter.EQ != set.query.op:
        return False
    
    # protection
    # check that the table is ones of the publication reference tables
    tables = (db.authors_roles,
              db.categories,
              db.collaborations,
              db.countries,
              db.projects,
              db.publishers,
              db.reports,
              db.status,
              db.teams)
    
    if field._table not in tables:
        return False
     
    # inhibit the delete if publications use the reference field
    query = get_where_query(db.publications)
    query = (query) & (set.query)
    
    if db(query).count():
        field._table[CALLBACK_ERRORS] = \
        T("Can't delete this record since several publications refer to it.")
        return True

    return False
LE GAC Renaud's avatar
LE GAC Renaud committed
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180


def INHIBIT_DUPLICATE_PUBLICATION(publication):
    """Inhibit the insert operation when the publication already exists.
    
    @type publication: dict
    @param publication: publications fields passed to insert
    
    @rtype: bool
    @return: C{True} when the insert is inhibited
    
    """
    db, T = current.globalenv['db'], current.T
    id_category = publication['id_categories']
    
    # articles
    id_cats = (get_id(db.categories, code='ACL'),
               get_id(db.categories, code='ACLN'))

    if id_category in id_cats:
        ids = duplicate_article(publication)
        if ids:
      
            db.publications[CALLBACK_ERRORS] = [
            T("Can't insert the article."),
            T("An article already exists with the same:"),
            T("• title, publisher, volume and pages"),
            T("• or publisher, volume and pages"),
            T("• or publisher and title."),
            T("See publication id(s) %s") % ', '.join(ids)]
             
            return True
 
    # talks and proceedings
    id_cats = (get_id(db.categories, code='ACTI'),
               get_id(db.categories, code='ACTN'),
               get_id(db.categories, code='COM'))
 
    if id_category in id_cats:
        ids = duplicate_conference(publication)
        if ids:

            db.publications[CALLBACK_ERRORS] = [
            T("Can't insert the talk/proceeding."),
            T("A talk/proceeding already exists with the same:"),
            T("• title, conference title, date and town"),
            T("• or title, conference date and town"),
            T("• or title, conference title and town"),
            T("See publication id(s) %s") % ', '.join(ids)]
        
            return True
 
    # reports
    id_cats = (get_id(db.categories, code='AP'),)
 
    if id_category in id_cats:
        ids = duplicate_report(publication)
        if ids:

            db.publications[CALLBACK_ERRORS] = [
            T("Can't insert the report."),
            T("A report already exists with the same title"),
            T("See publication id(s) %s") % ', '.join(ids)]
            
            return True
    
    return False


def INHIBIT_PUBLICATION_DELETE_ON_OK(s):
    """Inhibit the delete operation when the status of the publication is OK.
    
    @type s: gluon.dal.Set
    @param s: Set object used for delete.
    
    @rtype: bool
    @return: C{True} when the delete is inhibited
    
    """
    db, T = current.globalenv['db'], current.T
    id_ok = get_id(db.status, code='OK')

    # also call in cascade delete (teams, ...)
    # in normal case the query is "publications.id = 567"
    # in cascade delete the query is "publications.id_teams IN (21)"

    if s.query.first is db.publications.id:    
    
        id =  s.query.second
        if db.publications[id].id_status == id_ok:
            db.publications[CALLBACK_ERRORS] = \
            T("Can't delete a publication marked OK.")
            return True
    
    return False


def INHIBIT_PUBLICATION_UPDATE_ON_OK(s, f):
    """Inhibit the update operation when the status of the publication is OK.
    
    @type s: gluon.dal.Set
    @param s: Set object used for update.
    
    @type f: dict
    @param f: the dict of publications fields passed to update
    
    @rtype: bool
    @return: C{True} when the update is inhibited
    
    """
    db, T = current.globalenv['db'], current.T
    id_ok = get_id(db.status, code='OK')

    id = f["id"]
    
    if db.publications[id].id_status == id_ok:
        db.publications[CALLBACK_ERRORS] = \
        T("Can't updated a publication marked OK.")
        return True
    
    return False