You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
93 lines
2.5 KiB
Python
93 lines
2.5 KiB
Python
import click
|
|
|
|
from ffx.model.pattern import Pattern
|
|
|
|
|
|
class PatternController():
|
|
|
|
def __init__(self, context):
|
|
|
|
self.context = context
|
|
self.Session = self.context['database_session'] # convenience
|
|
|
|
|
|
def updatePattern(self, show_id, pattern):
|
|
|
|
try:
|
|
s = self.Session()
|
|
q = s.query(Pattern).filter(Pattern.show_id == int(show_id), Pattern.pattern == str(pattern))
|
|
|
|
if not q.count():
|
|
pattern = Pattern(show_id = int(show_id), pattern = str(pattern))
|
|
s.add(pattern)
|
|
s.commit()
|
|
return True
|
|
|
|
except Exception as ex:
|
|
raise click.ClickException(f"PatternController.updatePattern(): {repr(ex)}")
|
|
finally:
|
|
s.close()
|
|
|
|
|
|
|
|
def findPattern(self, showId, pattern):
|
|
|
|
try:
|
|
s = self.Session()
|
|
q = s.query(Pattern).filter(Pattern.show_id == int(showId), Pattern.pattern == str(pattern))
|
|
|
|
if q.count():
|
|
pattern = q.first()
|
|
return int(pattern.id)
|
|
else:
|
|
return None
|
|
|
|
except Exception as ex:
|
|
raise click.ClickException(f"PatternController.findPattern(): {repr(ex)}")
|
|
finally:
|
|
s.close()
|
|
|
|
|
|
def getPatternDescriptor(self, patternId):
|
|
|
|
try:
|
|
s = self.Session()
|
|
q = s.query(Pattern).filter(Pattern.id == int(patternId))
|
|
|
|
patternDescriptor = {}
|
|
if q.count():
|
|
pattern = q.first()
|
|
|
|
patternDescriptor['id'] = pattern.id
|
|
patternDescriptor['pattern'] = pattern.pattern
|
|
patternDescriptor['show_id'] = pattern.show_id
|
|
|
|
return patternDescriptor
|
|
|
|
except Exception as ex:
|
|
raise click.ClickException(f"PatternController.getPatternDescriptor(): {repr(ex)}")
|
|
finally:
|
|
s.close()
|
|
|
|
|
|
def deletePattern(self, patternId):
|
|
try:
|
|
s = self.Session()
|
|
q = s.query(Pattern).filter(Pattern.id == int(patternId))
|
|
|
|
if q.count():
|
|
|
|
#DAFUQ: https://stackoverflow.com/a/19245058
|
|
# q.delete()
|
|
pattern = q.first()
|
|
s.delete(pattern)
|
|
|
|
s.commit()
|
|
return True
|
|
return False
|
|
|
|
except Exception as ex:
|
|
raise click.ClickException(f"PatternController.deletePattern(): {repr(ex)}")
|
|
finally:
|
|
s.close()
|