def get_activity(from_user, activity_id): activity = Activity.select().join(User).where( User.telegram_id == from_user.id, Activity.activity_id == activity_id) if not activity.exists(): return None return activity.get()
def changeActivity(urlPassed, name): #import the Activity from models.py from models import Activity #search for record based on activity name. Iterate through to get all values. Activity is a Foreign key so note special handling for that rawActivity = Activity.select().where( Activity.activityName == urlPassed).get() #modify values based on database and classes defined in models.py if name != "None" or "" or None: rawActivity.activityName = name rawActivity.save()
def getActivities(): from models import Activity #below is to create a new one # new_activity = Activity.create(name="Water Polo") # create a list. A dictionary isn't needed because we only have one type of values activitiesList = [] #iterate through activities in database for Activity in Activity.select(): #get activity name and add it to list activityName = Activity.activityName activitiesList.append(activityName) #return the activity list in a list format return activitiesList
def generate_xlsx(month): def as_text(value): if value is None: return "" return str(value) wb = Workbook() ws = wb.active alignment = Alignment(horizontal='general', vertical='top', text_rotation=0, wrap_text=False, shrink_to_fit=False, indent=0) ws.append([ '№', 'ПІБ', 'Організував, провів, зробив', 'Долучився', 'Відвідав', 'Чергове засідання', 'Позачергові засідання' ]) users = User.select().order_by(User.name) activities = Activity.select().where(Activity.date.month == month) for i, user in enumerate(users): organized = activities.join(User).where( (Activity.activity_type == 'organized') & (User.name == user.name)) collaborated = activities.join( User).where((Activity.activity_type == 'collaborated') & (User.name == user.name)) visited = activities.join(User).where( (Activity.activity_type == 'visited') & (User.name == user.name)) organized_str = "" for j, act in enumerate(organized): organized_str += f'{j+1}. {act.name} [{act.date.strftime("%d.%m.%Y")}]' if act.description: organized_str += f'\nОпис: {act.description}\n\n' else: organized_str += '\n' collaborated_str = "" for j, act in enumerate(collaborated): collaborated_str += f'{j+1}. {act.name} [{act.date.strftime("%d.%m.%Y")}]' if act.description: collaborated_str += f'\nОпис: {act.description}\n\n' else: collaborated_str += '\n' visited_str = "" for j, act in enumerate(visited): visited_str += f'{j+1}. {act.name} [{act.date.strftime("%d.%m.%Y")}]' if act.description: visited_str += f'\nОпис: {act.description}\n\n' else: visited_str += '\n' ws.append([ str(i + 1), user.name, organized_str.strip(), collaborated_str.strip(), visited_str.strip() ]) for column_cells in ws.columns: max_length = 0 for cell in column_cells: max_length_of_line_in_cell = max( len(line) for line in as_text(cell.value).strip().split('\n')) max_length = max(max_length_of_line_in_cell, max_length) ws.column_dimensions[get_column_letter( column_cells[0].column)].width = max_length + 2 for row_cells in ws.rows: height = max(as_text(cell.value).count('\n') for cell in row_cells) ws.row_dimensions[row_cells[0].row].height = 11 * (height + 2) max_length = 0 max_height = 0 for col in ws.columns: for cell in col: val = as_text(cell.value) cell.alignment = alignment # TODO: Add normal autosize max_length = max(len(val), max_length) max_height = max(val.count('\n'), max_height) tf = NamedTemporaryFile(suffix='.xlsx', delete=False) wb.save(tf.name) return tf.name.replace('/tmp/', '')
def archive(): for activity in Activity.select().execute(): manager.archive(activity)
def get_activities(from_user): activities = Activity.select().join(User).where( User.telegram_id == from_user.id) return activities.dicts()