- assert isinstance(person, PLC.Persons.Person)
- assert 'person_id' in person
-
- site_id = self['site_id']
- person_id = person['person_id']
- self.api.db.do("DELETE FROM person_site" \
- " WHERE person_id = %(person_id)d" \
- " AND site_id = %(site_id)d",
- locals())
-
- if commit:
- self.api.db.commit()
-
- if 'person_ids' in self and person_id in self['person_ids']:
- self['person_ids'].remove(person_id)
-
- if 'site_ids' in person and site_id in person['site_ids']:
- person['site_ids'].remove(site_id)
-
- def sync(self, commit = True):
- """
- Flush changes back to the database.
- """
-
- self.validate()
-
- try:
- if not self['name'] or \
- not self['abbreviated_name'] or \
- not self['login_base']:
- raise KeyError
- except KeyError:
- raise PLCInvalidArgument, "name, abbreviated_name, and login_base must all be specified"
-
- # Fetch a new site_id if necessary
- if 'site_id' not in self:
- rows = self.api.db.selectall("SELECT NEXTVAL('sites_site_id_seq') AS site_id")
- if not rows:
- raise PLCDBError, "Unable to fetch new site_id"
- self['site_id'] = rows[0]['site_id']
- insert = True
- else:
- insert = False
-
- # Filter out fields that cannot be set or updated directly
- sites_fields = self.api.db.fields('sites')
- fields = dict(filter(lambda (key, value): key in sites_fields,
- self.items()))
- for ro_field in 'date_created', 'last_updated':
- if ro_field in fields:
- del fields[ro_field]
-
- # Parameterize for safety
- keys = fields.keys()
- values = [self.api.db.param(key, value) for (key, value) in fields.items()]
-
- if insert:
- # Insert new row in sites table
- sql = "INSERT INTO sites (%s) VALUES (%s)" % \
- (", ".join(keys), ", ".join(values))
- else:
- # Update existing row in sites table
- columns = ["%s = %s" % (key, value) for (key, value) in zip(keys, values)]
- sql = "UPDATE sites SET " + \
- ", ".join(columns) + \
- " WHERE site_id = %(site_id)d"
-
- self.api.db.do(sql, fields)
-
- if commit:
- self.api.db.commit()
+ assert self.table_name
+
+ self.api.db.do("UPDATE %s SET last_updated = CURRENT_TIMESTAMP " % (self.table_name) + \
+ " where site_id = %d" % (self['site_id']) )
+ self.sync(commit)
+
+
+ def associate_persons(self, auth, field, value):
+ """
+ Adds persons found in value list to this site (using AddPersonToSite).
+ Deletes persons not found in value list from this site (using DeletePersonFromSite).
+ """
+
+ assert 'person_ids' in self
+ assert 'site_id' in self
+ assert isinstance(value, list)
+
+ (person_ids, emails) = self.separate_types(value)[0:2]
+
+ # Translate emails into person_ids
+ if emails:
+ persons = Persons(self.api, emails, ['person_id']).dict('person_id')
+ person_ids += persons.keys()
+
+ # Add new ids, remove stale ids
+ if self['person_ids'] != person_ids:
+ from PLC.Methods.AddPersonToSite import AddPersonToSite
+ from PLC.Methods.DeletePersonFromSite import DeletePersonFromSite
+ new_persons = set(person_ids).difference(self['person_ids'])
+ stale_persons = set(self['person_ids']).difference(person_ids)
+
+ for new_person in new_persons:
+ AddPersonToSite.__call__(AddPersonToSite(self.api), auth, new_person, self['site_id'])
+ for stale_person in stale_persons:
+ DeletePersonFromSite.__call__(DeletePersonFromSite(self.api), auth, stale_person, self['site_id'])
+
+ def associate_addresses(self, auth, field, value):
+ """
+ Deletes addresses_ids not found in value list (using DeleteAddress).
+ Adds address if slice_fields w/o address_id found in value list (using AddSiteAddress).
+ Update address if slice_fields w/ address_id found in value list (using UpdateAddress).
+ """
+
+ assert 'address_ids' in self
+ assert 'site_id' in self
+ assert isinstance(value, list)
+
+ (address_ids, blank, addresses) = self.separate_types(value)
+
+ for address in addresses:
+ if 'address_id' in address:
+ address_ids.append(address['address_id'])
+
+ # Add new ids, remove stale ids
+ if self['address_ids'] != address_ids:
+ from PLC.Methods.DeleteAddress import DeleteAddress
+ stale_addresses = set(self['address_ids']).difference(address_ids)
+
+ for stale_address in stale_addresses:
+ DeleteAddress.__call__(DeleteAddress(self.api), auth, stale_address)
+
+ if addresses:
+ from PLC.Methods.AddSiteAddress import AddSiteAddress
+ from PLC.Methods.UpdateAddress import UpdateAddress
+
+ updated_addresses = filter(lambda address: 'address_id' in address, addresses)
+ added_addresses = filter(lambda address: 'address_id' not in address, addresses)
+
+ for address in added_addresses:
+ AddSiteAddress.__call__(AddSiteAddress(self.api), auth, self['site_id'], address)
+ for address in updated_addresses:
+ address_id = address.pop('address_id')
+ UpdateAddress.__call__(UpdateAddress(self.api), auth, address_id, address)