def addColours(task, task_div): colour_container = DIV() colour_container.class_name = 'colour_container' task_div.append(colour_container) colours = task.get('colours', []) for colour_nr in colours: colour_div = DIV() colour_div.class_name = 'label-colour' + colour_nr colour_container.append(colour_div)
def appendCommentToDom(author, when, content): length = len(cell6.get(selector='.comment')) colour = '' if length % 2 == 0 else ' callout' #colour = '' panel = DIV() panel.class_name = 'panel comment' + colour panel <= B('{0} dnia {1}'.format(author, when)) panel <= P(content) cell6 <= panel
def on_complete(response): data = loads(response.text) container = DIV() container <= H2('Organizacje') form = FORM(method='POST', action='#') #input0 = INPUT(name='id', type='hidden', value=task_details['id'], id='id_field') #form <= input0 row1 = DIV() row1.class_name = 'row' cell11 = DIV() cell11.class_name = 'large-12 columns' label11 = LABEL('Nazwa') input11 = INPUT(placeholder='Nazwa organizacji', type='text', value='', name='name', id='org_name_field') label11 <= input11 cell11 <= label11 row1 <= cell11 row2 = DIV() row2.class_name = 'row' cell2 = DIV() cell2.class_name = 'large-12 columns' label2 = LABEL('Tablice') input2 = SELECT(name='boards', id='org_boards', multiple=True) for board in data['boards']: input2 <= OPTION(board[0], value=getApiLink('boards', board[1])) label2 <= input2 cell2 <= label2 row2 <= cell2 row3 = DIV() row3.class_name = 'row' cell3 = DIV() cell3.class_name = 'large-12 columns' label3 = LABEL('Użytkownicy') input3 = SELECT(name='users', id='org_users', multiple=True) for user in data['users']: input3 <= OPTION(user[0], value=getApiLink('users', user[1])) label3 <= input3 cell3 <= label3 row3 <= cell3 row4 = DIV() row4.class_name = 'row' cell4 = DIV() cell4.class_name = 'large-12 columns' submit_btn = A('Zapisz', type='submit', href='#') submit_btn.class_name = 'button tiny' cell4 <= submit_btn row4 <= cell4 form <= row1 form <= row2 form <= row3 form <= row4 container <= form def on_submit(event): event.preventDefault() name = document['org_name_field'].value boards = [o.value for o in input2 if hasattr(o, 'selected') and o.selected] users = [o.value for o in input3 if hasattr(o, 'selected') and o.selected] owner = getApiLink('users', document['body'].__getattribute__('data-userid')) if name and boards and users: data = { 'name': name, 'boards': boards, 'users': users, 'owner': owner } def on_complete(response): if response.status == 201: hide_modal() else: alert(response.text) req2 = ajax.ajax() req2.bind('complete', on_complete) req2.open('POST', '/api/organizations/?format=json', True) req2.set_header('content-type', 'application/x-www-form-urlencoded') req2.set_header('X-CSRFToken', document.get(name='csrfmiddlewaretoken')[0].value) req2.send(data) submit_btn.bind('click', on_submit) show_modal(container)
def usersLoaded(req): users = loads(req.text) container = DIV() container <= H2('Edycja zadania') form = FORM(method='PATCH', action='#') input0 = INPUT(name='id', type='hidden', value=task_details['id'], id='id_field') form <= input0 row1 = DIV() row1.class_name = 'row' cell11 = DIV() cell11.class_name = 'large-6 columns' label11 = LABEL('Nazwa') input11 = INPUT(placeholder='Nazwa zadania', type='text', value=task_details['name'], name='name', id='name_field') label11 <= input11 cell11 <= label11 error_panel1 = DIV(id='name_error') error_panel1.class_name = 'panel error' cell11 <= error_panel1 row1 <= cell11 row2 = DIV() row2.class_name = 'row' cell2 = DIV() cell2.class_name = 'large-6 columns' label2 = LABEL('Opis') input2 = INPUT(placeholder='Opis zadania', name='description', id='description_field', type='text') input2.value = task_details['description'] label2 <= input2 cell2 <= label2 row2 <= cell2 cell3 = DIV() cell3.class_name = 'large-6 columns' label3 = LABEL('Przypisane do') input3 = SELECT(name='assignee', id='assignee_field') input3 <= OPTION('---', value=None) for user in users: input3 <= OPTION(user['username'], value=getApiLink('users', user['id'])) input3.value = task_details['assignee'] label3 <= input3 cell3 <= label3 row1 <= cell3 cell7 = DIV() cell7.class_name = 'large-6 columns' label7 = LABEL('Termin wykonania') input7 = INPUT(name='deadline', id='deadline_field', placeholder='yyyy-mm-dd', type='text') input7.value = task_details['deadline'] if task_details['deadline'] else '' label7 <= input7 cell7 <= label7 error_panel7 = DIV(id='deadline_error') error_panel7.class_name = 'panel error' cell7 <= error_panel7 row2 <= cell7 row3 = DIV() row3.class_name = 'row' cell33 = DIV() cell33.class_name = 'large-6 columns' label33 = LABEL('Trudność') input33 = SELECT(name='difficulty', id='difficulty_field') input33 <= OPTION('---', value='None') input33 <= OPTION('łatwe', value='easy') input33 <= OPTION('średnie', value='intermediate') input33 <= OPTION('trudne', value='hard') if task_details['difficulty'] and task_details['difficulty'] in ('easy', 'intermediate', 'hard'): input33.value = task_details['difficulty'] label33 <= input33 cell33 <= label33 cell34 = DIV() cell34.class_name = 'large-6 columns' label34 = LABEL('') label34 <= BR() input34 = BUTTON('Archiwizuj', id='archive_task', data_task_id=task_details['id']) input34.class_name = 'button tiny' label34 <= input34 cell34 <= label34 row3 <= cell33 row3 <= cell34 input34.bind('click', archiveTask) row6 = DIV() row6.class_name = 'row' cell6 = DIV() cell6.class_name = 'large-12 columns' cell6.id = 'comments-container' cell6 <= H4('Komentarze') def appendCommentToDom(author, when, content): length = len(cell6.get(selector='.comment')) colour = '' if length % 2 == 0 else ' callout' #colour = '' panel = DIV() panel.class_name = 'panel comment' + colour panel <= B('{0} dnia {1}'.format(author, when)) panel <= P(content) cell6 <= panel if len(task_details['comments']) == 0: panel = DIV() panel.class_name = 'panel' panel <= P('No comments') cell6 <= panel else: i = 0 for comment in task_details['comments']: author = [user['username'] for user in users if getApiLink('users', user['id']) == comment['author']].pop() when = comment['created_at'] content = comment['content'] appendCommentToDom(author, when, content) row6 <= cell6 row5 = DIV() row5.class_name = 'row' cell5 = DIV() cell5.class_name = 'large-12 columns' label5 = LABEL('Skomentuj') input5 = TEXTAREA(placeholder='Treść komentarza', name='comment', id='comment_field') label5 <= input5 cell5 <= label5 comment_btn = A('Dodaj komentarz', href='#') comment_btn.class_name = 'button tiny' cell5 <= comment_btn row5 <= cell5 def addComment(event): event.preventDefault() content = document['comment_field'].value author = document['body'].__getattribute__('data-username') author_id = document['body'].__getattribute__('data-userid') when = window.Date() if content: appendCommentToDom(author, when, content) data = { 'author': getApiLink('users', author_id), 'content': content, 'task': getApiLink('tasks', task_details['id']) } a2 = ajax.ajax() a2.open('POST', '/api/comments2/?format=json') a2.set_header('content-type', 'application/x-www-form-urlencoded') a2.set_header('X-CSRFToken', document.get(name='csrfmiddlewaretoken')[0].value) a2.send(data) document['comment_field'].value = '' comment_btn.bind('click', addComment) row4 = DIV() row4.class_name = 'row' cell4 = DIV() cell4.class_name = 'large-12 columns' submit_btn = A('Zapisz zmiany', type='submit', href='#') submit_btn.class_name = 'button tiny' cell4 <= submit_btn row4 <= cell4 form <= row1 form <= row2 form <= row3 form <= row4 form <= row6 form <= row5 container <= form for i in range(1, 6): colour_name = 'colour{0}'.format(i) if str(i) in task_details['colours']: document[colour_name].class_name = 'colour-btn checked' else: document[colour_name].class_name = 'colour-btn' #/api/tasks/ def on_submit(event): assignee = None if document['assignee_field'].value == 'None' \ else document['assignee_field'].value deadline = '' if document['deadline_field'].value == '' \ else document['deadline_field'].value + 'T01:01' difficulty = None if document['difficulty_field'].value == '---' \ or document['difficulty_field'].value not in ('easy', 'intermediate', 'hard') \ else document['difficulty_field'].value colours = [] for checked_colour in document.get(selector='a.colour-btn.checked'): colours.append(checked_colour.id[-1]) data = { 'id': document['id_field'].value, 'name': document['name_field'].value, 'description': document['description_field'].value, 'assignee': assignee, 'deadline': deadline, 'difficulty': difficulty, 'colours': ','.join(colours) } def taskUpdated(req): data = loads(req.text) if 'id' in data: # zapisalo sie poprawnie document['task_' + str(data['id'])].clear() data['colours'] = data['colours'].split(',') if data['colours'] else [] addColours(data, document['task_' + str(data['id'])]) document['task_' + str(data['id'])] <= H3(data['name'], **{'data-task-id': data['id']}) hide_modal() else: for field, errors in data.items(): error_field_name = field + '_error' for error in errors: document[error_field_name] <= error for error_field in document.get(selector='.panel.error'): error_field.clear() a = ajax.ajax() a.open('PATCH', '/api/tasks/' + str(data['id']) + '/?format=json') a.bind('complete', taskUpdated) a.set_header('content-type', 'application/x-www-form-urlencoded') a.set_header('X-CSRFToken', document.get(name='csrfmiddlewaretoken')[0].value) a.send(data) return False submit_btn.bind('click', on_submit) show_modal(container)
def on_complete(req): response = loads(req.text) if response['success']: for l in response['lists']: column = DIV() column.class_name = 'large-3 columns listsColumns' tasks_container = DIV(id='tasks_for_list_' + str(l['id']), data_list_id=l['id']) tasks_container.class_name = 'tasksContainer' column <= tasks_container header = H4(l['name']) archive_icon = SPAN() archive_icon.class_name = 'fi-trash size-24 archive_list_icon' archive_icon.bind('click', archiveList) header <= archive_icon tasks_container <= header new_task_input_params = {'placeholder': 'Nowe zadanie', 'class': 'newTaskInput', 'type': 'text', 'data-lists-id': l['id']} new_task_input = INPUT(**new_task_input_params) tasks_container <= new_task_input def drag_over(ev): ev.dataTransfer.dropEffect = 'move' ev.preventDefault() def on_drop(ev): ev.preventDefault() task_element_id = ev.dataTransfer.getData('text') element = document[task_element_id] task_id = task_element_id.split('_')[-1] target = ev.target if not hasattr(ev.target, 'class_name') or ev.target.class_name != 'tasksContainer': if ev.target.class_name == 'taskDiv' or ev.target.class_name == 'newTaskInput': target = ev.target.parent elif ev.target.class_name == 'task_name_header': target = ev.target.parent.parent elif ev.target.class_name == 'newTaskInput': target = ev.target.parent else: return dragee_pos = ev.clientY positions_elements = {dragee_pos: element} new_elements_positions = [dragee_pos] for el in target.get(selector='.taskDiv'): pos = el.top if el.top != dragee_pos else el.top - 1 positions_elements[pos] = el new_elements_positions.append(pos) new_elements_positions.sort() tasks_positions = {} for i, pos in enumerate(new_elements_positions, 1): el = positions_elements[pos] target <= el tasks_positions[el.__getattribute__('data-task-id')] = i def set_new_positions(ev): a = ajax.ajax() a.open('POST', '/update_tasks_positions', True) a.set_header('content-type', 'application/x-www-form-urlencoded') a.set_header('X-CSRFToken', document.get(name='csrfmiddlewaretoken')[0].value) a.send({'updates': dumps(tasks_positions)}) a = ajax.ajax() a.open('PATCH', getApiLink('tasks', task_id) + '?format=json') a.bind('complete', set_new_positions) a.set_header('content-type', 'application/x-www-form-urlencoded') a.set_header('X-CSRFToken', document.get(name='csrfmiddlewaretoken')[0].value) a.send({'id': task_id, 'board_list': getApiLink('boardlists', target.data_list_id)}) tasks_container.bind('dragover', drag_over) tasks_container.bind('drop', on_drop) def addColours(task, task_div): colour_container = DIV() colour_container.class_name = 'colour_container' task_div.append(colour_container) colours = task.get('colours', []) for colour_nr in colours: colour_div = DIV() colour_div.class_name = 'label-colour' + colour_nr colour_container.append(colour_div) def appendTask(task, container): task_div_params = {'class': 'taskDiv', 'data-task-id': task['id'], 'id': 'task_' + str(task['id']), 'draggable': True} task_div = DIV(**task_div_params) task_name = H3(task['name'], **{'data-task-id': task['id']}) task_name.class_name = 'task_name_header' addColours(task, task_div) task_div <= task_name container <= task_div def drag_start(ev): ev.dataTransfer.setData('text', ev.target.id) ev.dataTransfer.effectAllowed = 'move' task_div.bind('dragstart', drag_start) def taskDetails(event): #Uwaga na niedziedziczone atrybuty przy dziedziczonych eventach! task_id = event.target.__getattribute__('data-task-id') def taskDetailsLoaded(req): task_details = loads(req.text) def usersLoaded(req): users = loads(req.text) container = DIV() container <= H2('Edycja zadania') form = FORM(method='PATCH', action='#') input0 = INPUT(name='id', type='hidden', value=task_details['id'], id='id_field') form <= input0 row1 = DIV() row1.class_name = 'row' cell11 = DIV() cell11.class_name = 'large-6 columns' label11 = LABEL('Nazwa') input11 = INPUT(placeholder='Nazwa zadania', type='text', value=task_details['name'], name='name', id='name_field') label11 <= input11 cell11 <= label11 error_panel1 = DIV(id='name_error') error_panel1.class_name = 'panel error' cell11 <= error_panel1 row1 <= cell11 row2 = DIV() row2.class_name = 'row' cell2 = DIV() cell2.class_name = 'large-6 columns' label2 = LABEL('Opis') input2 = INPUT(placeholder='Opis zadania', name='description', id='description_field', type='text') input2.value = task_details['description'] label2 <= input2 cell2 <= label2 row2 <= cell2 cell3 = DIV() cell3.class_name = 'large-6 columns' label3 = LABEL('Przypisane do') input3 = SELECT(name='assignee', id='assignee_field') input3 <= OPTION('---', value=None) for user in users: input3 <= OPTION(user['username'], value=getApiLink('users', user['id'])) input3.value = task_details['assignee'] label3 <= input3 cell3 <= label3 row1 <= cell3 cell7 = DIV() cell7.class_name = 'large-6 columns' label7 = LABEL('Termin wykonania') input7 = INPUT(name='deadline', id='deadline_field', placeholder='yyyy-mm-dd', type='text') input7.value = task_details['deadline'] if task_details['deadline'] else '' label7 <= input7 cell7 <= label7 error_panel7 = DIV(id='deadline_error') error_panel7.class_name = 'panel error' cell7 <= error_panel7 row2 <= cell7 row3 = DIV() row3.class_name = 'row' cell33 = DIV() cell33.class_name = 'large-6 columns' label33 = LABEL('Trudność') input33 = SELECT(name='difficulty', id='difficulty_field') input33 <= OPTION('---', value='None') input33 <= OPTION('łatwe', value='easy') input33 <= OPTION('średnie', value='intermediate') input33 <= OPTION('trudne', value='hard') if task_details['difficulty'] and task_details['difficulty'] in ('easy', 'intermediate', 'hard'): input33.value = task_details['difficulty'] label33 <= input33 cell33 <= label33 cell34 = DIV() cell34.class_name = 'large-6 columns' label34 = LABEL('') label34 <= BR() input34 = BUTTON('Archiwizuj', id='archive_task', data_task_id=task_details['id']) input34.class_name = 'button tiny' label34 <= input34 cell34 <= label34 row3 <= cell33 row3 <= cell34 input34.bind('click', archiveTask) row6 = DIV() row6.class_name = 'row' cell6 = DIV() cell6.class_name = 'large-12 columns' cell6.id = 'comments-container' cell6 <= H4('Komentarze') def appendCommentToDom(author, when, content): length = len(cell6.get(selector='.comment')) colour = '' if length % 2 == 0 else ' callout' #colour = '' panel = DIV() panel.class_name = 'panel comment' + colour panel <= B('{0} dnia {1}'.format(author, when)) panel <= P(content) cell6 <= panel if len(task_details['comments']) == 0: panel = DIV() panel.class_name = 'panel' panel <= P('No comments') cell6 <= panel else: i = 0 for comment in task_details['comments']: author = [user['username'] for user in users if getApiLink('users', user['id']) == comment['author']].pop() when = comment['created_at'] content = comment['content'] appendCommentToDom(author, when, content) row6 <= cell6 row5 = DIV() row5.class_name = 'row' cell5 = DIV() cell5.class_name = 'large-12 columns' label5 = LABEL('Skomentuj') input5 = TEXTAREA(placeholder='Treść komentarza', name='comment', id='comment_field') label5 <= input5 cell5 <= label5 comment_btn = A('Dodaj komentarz', href='#') comment_btn.class_name = 'button tiny' cell5 <= comment_btn row5 <= cell5 def addComment(event): event.preventDefault() content = document['comment_field'].value author = document['body'].__getattribute__('data-username') author_id = document['body'].__getattribute__('data-userid') when = window.Date() if content: appendCommentToDom(author, when, content) data = { 'author': getApiLink('users', author_id), 'content': content, 'task': getApiLink('tasks', task_details['id']) } a2 = ajax.ajax() a2.open('POST', '/api/comments2/?format=json') a2.set_header('content-type', 'application/x-www-form-urlencoded') a2.set_header('X-CSRFToken', document.get(name='csrfmiddlewaretoken')[0].value) a2.send(data) document['comment_field'].value = '' comment_btn.bind('click', addComment) row4 = DIV() row4.class_name = 'row' cell4 = DIV() cell4.class_name = 'large-12 columns' submit_btn = A('Zapisz zmiany', type='submit', href='#') submit_btn.class_name = 'button tiny' cell4 <= submit_btn row4 <= cell4 form <= row1 form <= row2 form <= row3 form <= row4 form <= row6 form <= row5 container <= form for i in range(1, 6): colour_name = 'colour{0}'.format(i) if str(i) in task_details['colours']: document[colour_name].class_name = 'colour-btn checked' else: document[colour_name].class_name = 'colour-btn' #/api/tasks/ def on_submit(event): assignee = None if document['assignee_field'].value == 'None' \ else document['assignee_field'].value deadline = '' if document['deadline_field'].value == '' \ else document['deadline_field'].value + 'T01:01' difficulty = None if document['difficulty_field'].value == '---' \ or document['difficulty_field'].value not in ('easy', 'intermediate', 'hard') \ else document['difficulty_field'].value colours = [] for checked_colour in document.get(selector='a.colour-btn.checked'): colours.append(checked_colour.id[-1]) data = { 'id': document['id_field'].value, 'name': document['name_field'].value, 'description': document['description_field'].value, 'assignee': assignee, 'deadline': deadline, 'difficulty': difficulty, 'colours': ','.join(colours) } def taskUpdated(req): data = loads(req.text) if 'id' in data: # zapisalo sie poprawnie document['task_' + str(data['id'])].clear() data['colours'] = data['colours'].split(',') if data['colours'] else [] addColours(data, document['task_' + str(data['id'])]) document['task_' + str(data['id'])] <= H3(data['name'], **{'data-task-id': data['id']}) hide_modal() else: for field, errors in data.items(): error_field_name = field + '_error' for error in errors: document[error_field_name] <= error for error_field in document.get(selector='.panel.error'): error_field.clear() a = ajax.ajax() a.open('PATCH', '/api/tasks/' + str(data['id']) + '/?format=json') a.bind('complete', taskUpdated) a.set_header('content-type', 'application/x-www-form-urlencoded') a.set_header('X-CSRFToken', document.get(name='csrfmiddlewaretoken')[0].value) a.send(data) return False submit_btn.bind('click', on_submit) show_modal(container) req = ajax.ajax() req.open('GET', '/api/users/?format=json', True) req.bind('complete', usersLoaded) req.send() req = ajax.ajax() req.open('GET', '/api/tasks/' + task_id + '/?format=json', True) req.bind('complete', taskDetailsLoaded) req.send() task_div.bind('click', taskDetails) l['tasks'].sort(key=itemgetter('order_in_list', 'id')) for task in l['tasks']: appendTask(task, tasks_container) document['listContainer'] <= column def saveTask(event): value = event.target.value lists_id = event.target.__getattribute__('data-lists-id') if value: def task_add_callback(req): if req.status == 201: new_task = loads(req.text) boards_id = new_task['board_list'].split('/')[-2] container = document['tasks_for_list_' + str(boards_id)] appendTask(new_task, container) event.target.value = '' else: alert('Dodanie zadania nie powiodło się') req = ajax.ajax() req.bind('complete', task_add_callback) req.open('POST', '/tasks/?format=json', True) req.set_header('content-type', 'application/x-www-form-urlencoded') req.set_header('X-CSRFToken', document.get(name='csrfmiddlewaretoken')[0].value) req.send({'lists_id': lists_id, 'description': '', 'name': value, 'comments': None}) new_task_input.bind('blur', saveTask) column = DIV() column.class_name = 'large-3 columns listsColumns' new_input = INPUT(id='newColumnInput', placeholder='Nowa lista', type='text') column <= new_input document['listContainer'] <= column new_input.bind('blur', saveNewList) #console.log(document.get(selector='.newTaskInput')[0].focus()) else: alert('Pobranie list nie powiodło się')