def save(self): """ Save user account data to user account file on disk. This saves all member variables, except "id" and "valid" and those starting with an underscore. """ if not self.id: return user_dir = self._cfg.user_dir if not os.path.exists(user_dir): os.makedirs(user_dir) self.last_saved = str(time.time()) # !!! should write to a temp file here to avoid race conditions, # or even better, use locking data = codecs.open(self.__filename(), "w", config.charset) data.write("# Data saved '%s' for id '%s'\n" % (time.strftime( self._cfg.datetime_fmt, time.localtime(time.time())), self.id)) attrs = self.persistent_items() attrs.sort() for key, value in attrs: # Encode list values if isinstance(value, list): key += '[]' value = encodeList(value) # Encode dict values elif isinstance(value, dict): key += '{}' value = encodeDict(value) line = u"%s=%s" % (key, unicode(value)) line = line.replace('\n', ' ').replace('\r', ' ') # no lineseps data.write(line + '\n') data.close() arena = 'user' key = 'name2id' caching.CacheEntry(self._request, arena, key, scope='wiki').remove() try: del self._request.cfg.cache.name2id except: pass key = 'openid2id' caching.CacheEntry(self._request, arena, key, scope='wiki').remove() try: del self._request.cfg.cache.openid2id except: pass if not self.disabled: self.valid = 1 if not self._stored: self._stored = True event = events.UserCreatedEvent(self._request, self) events.send_event(event)
def test_user_created_event(request): py.test.skip( "Test is wrong, because it assumes send_notification will be called - but if there is no superuser subscribed to UserCreatedEvent, then no notification needs to be sent." ) class server_dummy: def __init__(self): self.sent = False def send_notification(self, *args): self.sent = True event = events.UserCreatedEvent(request, User(request)) request.cfg.notification_server = server_dummy() request.cfg.secrets = "thisisnotsecret" jabbernotify.handle_user_created(event) assert request.cfg.notification_server.sent is True
def save(self): """ Save user account data to user account file on disk. This saves all member variables, except "id" and "valid" and those starting with an underscore. """ if not self.id: return user_dir = self._cfg.user_dir if not os.path.exists(user_dir): os.makedirs(user_dir) self.last_saved = str(time.time()) # !!! should write to a temp file here to avoid race conditions, # or even better, use locking temp = file(os.path.join(user_dir, 'temp-' + uuid.uuid4().get_hex()), 'w') try: data = codecs.getwriter(config.charset)(temp) data.write("# Data saved '%s' for id '%s'\n" % ( time.strftime(self._cfg.datetime_fmt, time.localtime(time.time())), self.id)) attrs = self.persistent_items() attrs.sort() for key, value in attrs: # Encode list values if isinstance(value, list): key += '[]' value = encodeList(value) # Encode dict values elif isinstance(value, dict): key += '{}' value = encodeDict(value) line = u"%s=%s" % (key, unicode(value)) line = line.replace('\n', ' ').replace('\r', ' ') # no lineseps data.write(line + '\n') # atomically put it in place (except on windows) filesys.rename(temp.name, self.__filename()) except IOError as err: _ = self._request.getText # throw a nicer exception if err.errno == errno.ENOSPC: raise SaveError( _("Cannot save user %s, no storage space left.") % self.name) else: raise SaveError( _("An I/O error occurred while saving user %s (errno=%d)")\ % (self.name, err.errno)) finally: try: os.remove(temp.name) except: pass # we don't care for errors in the os.remove finally: temp.close() if not self.disabled: self.valid = 1 self.updateLookupCaches() if not self._stored: self._stored = True event = events.UserCreatedEvent(self._request, self) events.send_event(event) # update page subscriber's cache after saving user preferences self.updatePageSubCache()