Compare commits

4 Commits

3 changed files with 102 additions and 27 deletions

View File

@@ -25,9 +25,10 @@ At the moment, an alpha version is undertaken, with these features:
- Show mastered movements for activated user (`sm`) - Show mastered movements for activated user (`sm`)
- display, if work or movement is mastered from active user when viewing works and movements - display, if work or movement is mastered from active user when viewing works and movements
- Works are shown sorted by composer and opus/work_directory-number (`s`, `smo`) or by composer and collection (`sm`) - Works are shown sorted by composer and opus/work_directory-number (`s`, `smo`) or by composer and collection (`sm`)
- (planned) Remove work or movement from being mastered for active user - Remove work or movement from being mastered for active user (`r number_of_work` or `r number_of_work number_of_movement`)
- (planned) Remove user - Remove user (`delete_my_user_record_including_all_mastered_works`)
- (planned) Fix known bugs - Fixed bug: double marking crashes the program
- Fixed bug: check for user before setting
_(planned) Release piano_repertoire_cli V 0.0.1 alpha (learn about versioning before)_ _(planned) Release piano_repertoire_cli V 0.0.1 alpha (learn about versioning before)_
@@ -44,4 +45,5 @@ _(planned) Port funtionality to a kivy-GUI_
## Known Bugs ## Known Bugs
- Marking a piece as mastered the second time, the database wont insert the entry and the program will crash - Trying to mark works as mastered that are not present crashes the program
- Providing alphabetic chars where numbers are expected crashes the program

View File

@@ -9,9 +9,11 @@ class Session:
self.resultstring = 'no results yet' self.resultstring = 'no results yet'
self.commands = {'a': (self.set_user, 'Set an active user\n Usage: a number_of_user'), self.commands = {'a': (self.set_user, 'Set an active user\n Usage: a number_of_user'),
'c': (self.show_composers, 'Shows composers'), 'c': (self.show_composers, 'Shows composers'),
'delete_my_user_record_including_all_mastered_works': (self.delete_user, 'DangerZone: deletes user and all marks of mastered works'),
'h': (self.show_help, 'Displays this help document'), 'h': (self.show_help, 'Displays this help document'),
'm': (self.mark_work_as_mastered, 'Marks work or movement as mastered for the active user\n Usage: m number_of_work, m number_of_work number_of_movement'), 'm': (self.mark_work_as_mastered, 'Marks work or movement as mastered for the active user\n Usage: m number_of_work, m number_of_work number_of_movement'),
'n': (self.create_new_user, 'Creates new user\n Usage: n firstname name'), 'n': (self.create_new_user, 'Creates new user\n Usage: n firstname name'),
'r': (self.remove_work_as_mastered, 'Unmarks work or movement as mastered for the active user\n Usage: r number_of_work, r number_of_work number_of_movement'),
's': (self.show_works, 'Show the works stored in the database\n Usage: s, s number_of_composer'), 's': (self.show_works, 'Show the works stored in the database\n Usage: s, s number_of_composer'),
'sm': (self.show_mastered, 'Show mastered movements for activated user'), 'sm': (self.show_mastered, 'Show mastered movements for activated user'),
'smo': (self.show_mastered_opus, 'Show mastered movements for activated user, sorted by opus.'), 'smo': (self.show_mastered_opus, 'Show mastered movements for activated user, sorted by opus.'),
@@ -21,9 +23,19 @@ class Session:
} }
def set_user(self, arguments): def set_user(self, arguments):
resultstring = ''
db_index = arguments[0] db_index = arguments[0]
self.user = db_index sql_command = f'''
return 'User set' SELECT *
FROM pianist
WHERE id = {db_index}
'''
if self.db_agent.execute(sql_command).fetchone():
self.user = db_index
resultstring += 'User set'
else:
resultstring += 'No such user'
return resultstring
def get_active_user(self): def get_active_user(self):
sql_command = f''' sql_command = f'''
@@ -80,6 +92,21 @@ class Session:
self.db_agent.execute(sql_command) self.db_agent.execute(sql_command)
return f'Created new user {first_name} {name}.' return f'Created new user {first_name} {name}.'
def delete_user(self, _):
resultstring = ''
if self.user == -1:
resultstring += 'Please activate user'
else:
user_name = self.get_active_user()
sql_command = f'''
DELETE FROM pianist
WHERE id = {self.user}
'''
self.db_agent.execute(sql_command)
resultstring += f'Deleted user {user_name}'
return resultstring
def show_composers(self, _): def show_composers(self, _):
sql_command = ''' sql_command = '''
SELECT id, first_name, name SELECT id, first_name, name
@@ -193,25 +220,69 @@ class Session:
return fun_resultstring return fun_resultstring
def mark_work_as_mastered(self, arguments): def mark_work_as_mastered(self, arguments):
work_id = arguments[0] if self.user == -1:
resultstring = 'adding:\n' resultstring = 'Please activate user'
work = work_under_id(work_id, self.db_agent) else:
if len(arguments) > 1: # in case there is a movement number given work_id = arguments[0]
mov_number = int(arguments[1]) resultstring = 'adding:\n'
sql_command = f''' work = work_under_id(work_id, self.db_agent)
INSERT INTO is_able_to_play (work_id, mov_id, pianist_id) if len(arguments) > 1: # in case there is a movement number given
VALUES ({work_id}, {mov_number}, {self.user}) mov_number = int(arguments[1])
''' if self.movement_is_mastered(work_id, mov_number):
self.db_agent.execute(sql_command) resultstring += f'Already marked as mastered: {work.pretty_mov(mov_number)}\n'
resultstring += f'{work.pretty_mov(mov_number)}\n' else:
else: # mark all movements of the work as mastered sql_command = f'''
for mov_number in work.values['movements'].keys(): INSERT INTO is_able_to_play (work_id, mov_id, pianist_id)
sql_command = f''' VALUES ({work_id}, {mov_number}, {self.user})
INSERT INTO is_able_to_play (work_id, mov_id, pianist_id) '''
VALUES ({work_id}, {mov_number}, {self.user}) self.db_agent.execute(sql_command)
''' resultstring += f'{work.pretty_mov(mov_number)}\n'
self.db_agent.execute(sql_command) else: # mark all movements of the work as mastered
resultstring += f'{work.pretty_mov(mov_number)}\n' for mov_number in work.values['movements'].keys():
if self.movement_is_mastered(work_id, mov_number):
resultstring += f'Already marked as mastered: {work.pretty_mov(mov_number)}\n'
else:
sql_command = f'''
INSERT INTO is_able_to_play (work_id, mov_id, pianist_id)
VALUES ({work_id}, {mov_number}, {self.user})
'''
self.db_agent.execute(sql_command)
resultstring += f'{work.pretty_mov(mov_number)}\n'
return resultstring
def remove_work_as_mastered(self, arguments):
if self.user == -1:
resultstring = 'Please activate user'
else:
work_id = arguments[0]
resultstring = 'removing:\n'
work = work_under_id(work_id, self.db_agent)
if len(arguments) > 1: # in case there is a movment number given
mov_number = int(arguments[1])
if self.movement_is_mastered(work_id, mov_number):
sql_command = f'''
DELETE FROM is_able_to_play
WHERE work_id = {work_id}
AND mov_id = {mov_number}
AND pianist_id = {self.user}
'''
self.db_agent.execute(sql_command)
resultstring += f'{work.pretty_mov(mov_number)}\n'
else:
resultstring += f'Movement was not marked as mastered: {work.pretty_mov(mov_number)}\n'
else:
for mov_number in work.values['movements'].keys():
if self.movement_is_mastered(work_id, mov_number):
sql_command = f'''
DELETE FROM is_able_to_play
WHERE work_id = {work_id}
AND mov_id = {mov_number}
AND pianist_id = {self.user}
'''
self.db_agent.execute(sql_command)
resultstring += f'{work.pretty_mov(mov_number)}\n'
else:
resultstring += f'Movement was not marked as mastered: {work.pretty_mov(mov_number)}\n'
return resultstring return resultstring
def quit(self, _): def quit(self, _):
@@ -367,6 +438,8 @@ def command_line_loop(session):
def main(): def main():
con = sqlite3.connect('repertoire.db') con = sqlite3.connect('repertoire.db')
db_agent = con.cursor() db_agent = con.cursor()
sql_command = 'PRAGMA foreign_keys = ON'
db_agent.execute(sql_command)
session = Session(db_agent) session = Session(db_agent)
command_line_loop(session) command_line_loop(session)
con.commit() con.commit()

View File

@@ -52,7 +52,7 @@ CREATE TABLE is_able_to_play(
recording VARCHAR(255), recording VARCHAR(255),
PRIMARY KEY(work_id, mov_id, pianist_id), PRIMARY KEY(work_id, mov_id, pianist_id),
FOREIGN KEY(work_id, mov_id) REFERENCES movement(work_id, mov_number), FOREIGN KEY(work_id, mov_id) REFERENCES movement(work_id, mov_number),
FOREIGN KEY(pianist_id) REFERENCES pianist(id) FOREIGN KEY(pianist_id) REFERENCES pianist(id) ON DELETE CASCADE
); );
CREATE TABLE plays_in( CREATE TABLE plays_in(
@@ -63,5 +63,5 @@ CREATE TABLE plays_in(
PRIMARY KEY(concert_id, pianist_id, work_id, mov_id), PRIMARY KEY(concert_id, pianist_id, work_id, mov_id),
FOREIGN KEY(concert_id) REFERENCES concert(id), FOREIGN KEY(concert_id) REFERENCES concert(id),
FOREIGN KEY(work_id, mov_id) REFERENCES movement(work_id, mov_number), FOREIGN KEY(work_id, mov_id) REFERENCES movement(work_id, mov_number),
FOREIGN KEY(pianist_id) REFERENCES pianist(id) FOREIGN KEY(pianist_id) REFERENCES pianist(id) ON DELETE CASCADE
); );