Skip to content
Snippets Groups Projects
Select Git revision
  • 1ea3a065c01cb59c5f1154229c202457ea8c0070
  • develop default protected
  • ical-export
  • feature/audit_log
  • fix/index
  • badge-redeem-404
  • 720-schedule_source
  • room-docu
  • chore/event-views
  • 511-schedule-foo-fixed
  • 607-schedule-versions
  • deploy/curl-verbose
  • fix/public-badge-access-rights
  • 445-schedule-redirects
  • 623-wiki-im-baustellenmodus-sollte-mal-als-wiki-admin-trotzdem-seiten-anlegen-bearbeiten-konnen
  • fix/registration_mail_subject
  • feature/conference-query-set
  • feature/568-habitatmanagement
  • feat/unit-integration-tests
  • camp23-prod
  • production
  • prod-2024-12-27_20-15
  • prod-2024-12-27_16-37
  • prod-2024-12-27_16-01
  • prod-2024-12-27_13-29
  • prod-2024-12-27_00-34
  • prod-2024-12-26_21-45
  • prod-2024-12-26_13-12
  • prod-2024-12-26_00-21
  • prod-2024-12-25_21-04
  • prod-2024-12-25_15-54
  • prod-2024-12-25_01-29
  • prod-2024-12-24_14-48
  • prod-2024-12-23_23-39
  • prod-2024-12-22_21-12
  • prod-2024-12-22_17-25
  • prod-2024-12-22_01-34
  • prod-2024-12-22_00-55
  • prod-2024-12-21_13-42
  • prod-2024-12-21_10-44
  • prod-2024-12-20_12-25
41 results

workadventure.py

Blame
  • Forked from hub / hub
    Source project has a limited visibility.
    misc.py 2.43 KiB
    from uffd.database import db
    
    # pylint completely fails to understand SQLAlchemy's query functions
    # pylint: disable=no-member
    
    feature_flag_table = db.Table('feature_flag',
    	db.Column('name', db.String(32), primary_key=True),
    )
    
    class FeatureFlag:
    	def __init__(self, name):
    		self.name = name
    		self.enable_hooks = []
    		self.disable_hooks = []
    
    	@property
    	def expr(self):
    		return db.exists().where(feature_flag_table.c.name == self.name)
    
    	def __bool__(self):
    		return db.session.execute(db.select([self.expr])).scalar()
    
    	def enable_hook(self, func):
    		self.enable_hooks.append(func)
    		return func
    
    	def enable(self):
    		db.session.execute(db.insert(feature_flag_table).values(name=self.name))
    		for func in self.enable_hooks:
    			func()
    
    	def disable_hook(self, func):
    		self.disable_hooks.append(func)
    		return func
    
    	def disable(self):
    		db.session.execute(db.delete(feature_flag_table).where(feature_flag_table.c.name == self.name))
    		for func in self.disable_hooks:
    			func()
    
    FeatureFlag.unique_email_addresses = FeatureFlag('unique-email-addresses')
    
    lock_table = db.Table('lock',
    	db.Column('name', db.String(32), primary_key=True),
    )
    
    class Lock:
    	ALL_LOCKS = set()
    
    	def __init__(self, name):
    		self.name = name
    		assert name not in self.ALL_LOCKS
    		self.ALL_LOCKS.add(name)
    
    	def acquire(self):
    		'''Acquire the lock until the end of the current transaction
    
    		Calling acquire while the specific lock is already held has no effect.'''
    		if db.engine.name == 'sqlite':
    			# SQLite does not support with_for_update, but we can lock the whole DB
    			# with any write operation. So we do a dummy update.
    			db.session.execute(db.update(lock_table).where(False).values(name=None))
    		elif db.engine.name in ('mysql', 'mariadb'):
    			result = db.session.execute(db.select([lock_table.c.name]).where(lock_table.c.name == self.name).with_for_update()).scalar()
    			if result is not None:
    				return
    			# We add all lock rows with migrations so we should never end up here
    			raise Exception(f'Lock "{self.name}" is missing')
    		else:
    			raise NotImplementedError()
    
    # Only executed when lock_table is created with db.create/db.create_all (e.g.
    # during testing). Otherwise the rows are inserted with migrations.
    @db.event.listens_for(lock_table, 'after_create') # pylint: disable=no-member
    def insert_lock_rows(target, connection, **kwargs): # pylint: disable=unused-argument
    	for name in Lock.ALL_LOCKS:
    		db.session.execute(db.insert(lock_table).values(name=name))
    	db.session.commit()