def delete(self, act_str, do_exec = True): if not self._session.has_key('userid'): from germ.error.no_valid_keys import no_valid_keys raise no_valid_keys() self.check_exec(do_exec) del self._session['userid']
def submit(self, act_str, do_exec = True): if self._session.has_key('userid'): from germ.error.no_valid_keys import no_valid_keys raise no_valid_keys() self.check_exec(do_exec) self.require_parms(act_str) self.check_pwd(cf.pwd_str) self._session['userid'] = self._attr_map['username'].get()
def generate_keylist(self, act_str): # generate table references table_ref_vec = [] for rel in self.__joins: table_ref_vec.append(rel.get_table_spec() + \ self.__get_join_cond(rel)) table_ref = "\n JOIN ".join(table_ref_vec) if len(self.__joins) == 0: # This should only happen if there are only non-relational # PKs. rel0 = self.__outer_joins.pop(0) # make sure the relation is registerated in the rel_map self.__get_join_cond(rel0) table_ref = rel0.get_table_spec() # TODO: find out if this is possible and handle it appropriately #if len(self.__outer_joins) > 0: # rel1 = self.__outer_joins.pop(0) # table_ref += "\n OUTER JOIN " + rel1.get_table_spec() + \ # self.__get_join_cond(rel1) for rel in self.__outer_joins: table_ref += "\n " + rel.get_outer_join() + " JOIN " + \ rel.get_table_spec() + self.__get_join_cond(rel) else: for rel in self.__outer_joins: table_ref += "\n " + rel.get_outer_join() + " JOIN " + \ rel.get_table_spec() + self.__get_join_cond(rel) # generate search condition missing_lock = False missing_pk_lock = False pk_set = self.__ent.get_pk_set() #non_rel_missing_lock = False lock_cond = [] to_lock_cond = [] to_lock_vec = [] for key, rel in self.__rel_map.iteritems(): #from germ.error.error import error #error(error.debug, 'getting lock condition', 'key: %s, rel: %s' \ # % (key, rel)) attr = self.__ent.get_attr_nocheck(key) if attr.is_locked(): # TODO: pk_submit_relation should probably do something # different self.__add_lock_cond_if(rel, key, attr, lock_cond) elif attr.is_to_be_locked(): self.__add_lock_cond_if(rel, key, attr, to_lock_cond) to_lock_vec.append(key) else: if attr.dyn_perm(act_str): missing_lock = True if key in pk_set: missing_pk_lock = True if len(self.__joins) == 0: return (missing_lock, missing_pk_lock) #if rel.is_outer_join(): # non_rel_missing_lock = True search_cond = [] to_lock_search_cond = [] for rel in self.__joins + self.__outer_joins: cond_str = rel.get_cond(act_str) if cond_str is None: continue cond_str = '(%s)' % self.__substitute_vars(cond_str) # TODO: Think about this. # This makes sure a check for uniqueness does not collide with the # entry itself. if rel.get_table() == self.__ent.get_name(): if self.__ent.pks_locked(): cond_str = '(%s OR (%s))' % (cond_str, self.__ent.get_attr_sql_pk_alias(rel.get_alias())) # if act_str == 'edit' and rel.is_outer_join() and \ # rel.get_table() == self.__ent.get_name(): # if not self.__ent.pks_locked(): # continue # # cond_str = '(%s OR (%s))' % (cond_str, # self.__ent.get_attr_sql_pk_alias(rel.get_alias())) if len(self.__joins) == 0: cond_str = 'NOT ' + cond_str if rel.is_to_be_locked(): # If we fail, simply omit the to_lock_search_cond in the # second query. This is a nasty trick that will allow us to # get our reference keys even though the join condition is # wrong. This will only work if the user interface behaves # nicely. If both the non-relational and the relational # keys are wrong, we have a problem (because invalid # relational keys will be conceived as correct) # # Or no? Maybe this is not such a nasty trick after all. # The outer joins are easily controlled by the search # condition. Omitting it will have the same effect as if we # had not supplied the outer join at all. An outer join # will never give additional results. There can only be # fewer if the IS NULL condition is used. to_lock_search_cond.append(cond_str) else: search_cond.append(cond_str) # generate sort order specification # preserve order given by entity definition key_vec = [] for attr in self.__ent.get_attr_ids(): if self.has_fk(attr): key_vec.append(attr) sort_spec = ", ".join(key_vec) # generate column specification col_spec_vec = [] for key in key_vec: rel = self.__rel_map[key] col_spec_vec.append("\n %s AS %s" % \ (rel.get_colref(key), key)) #col_spec_vec.append("\n %s.%s AS %s" % \ # (rel.get_alias(), rel.get_realkey(key), key)) col_spec = ", ".join(col_spec_vec) #from germ.error.error import error #error(error.debug, 'constructing search condition', 'search_cond: %s, ' \ # 'lock_cond: %s, to_lock_cond: %s' % \ # (search_cond, lock_cond, to_lock_cond)) # execute query res_ok, rset = self.__query_if(len(self.__joins), col_spec, table_ref, search_cond + to_lock_search_cond + lock_cond + \ to_lock_cond, sort_spec) if len(to_lock_vec) != 0: if res_ok: # Assert key locks for key in to_lock_vec: self.__ent.get_attr_nocheck(key).lock() else: # Possible scenario: User locks a non-relational key and in # combination with the locked parameters this results in an # empty result set. # So we simply tell the user this key is not available and # prompt for the same values again. for key in to_lock_vec: self.__ent.get_attr_nocheck(key).invalid_key() if key in pk_set: missing_pk_lock = True missing_lock = True if len(self.__joins) == 0: return (missing_lock, missing_pk_lock) res_ok, rset = self.__query_if(len(self.__joins), col_spec, table_ref, search_cond + lock_cond, sort_spec) # It's also possible that there are simply no valid entries available. # In this case the user interface should not give the possibility to # execute this action in the first place. if not res_ok: if missing_lock: pass #self.__key_map = dict(zip(key_vec, len(key_vec)*[[]])) else: # This can happen if user tampers with locked parameters or if # the user interface is buggy and changes locked parameters. # Thus, a reason for this error can be pre-locking (which is # essentially tampering with locked parameters), rasing a # no_valid_keys exception is probably reasonable. pass #from germ.error.error import error #from germ.txt import errmsg #raise error(error.fail, errmsg.invalid_key, 'entity: %s, ' \ # 'action: %s, res_ok: %s, missing_lock: %s' % \ # (self.__ent.get_name(), act_str, # res_ok, missing_lock)) from germ.error.no_valid_keys import no_valid_keys raise no_valid_keys() if len(self.__joins) > 0: self.__key_map = dict(zip(key_vec, zip(*rset))) return (missing_lock, missing_pk_lock)