Exemple #1
0
def edit_list():
    list_id = request.json['list_id']

    form = ListForm()
    form['csrf_token'].data = request.cookies['csrf_token']

    lst = db.session.query(List) \
                    .filter(List.id == list_id) \
                    .first()
    lst_dict = lst.to_dict()

    title_changed = form.data['title'] != lst_dict['title']
    editorial_changed = form.data['editorial'] != lst_dict['editorial']
    published_changed = form.data['published'] != lst_dict['published']

    if (form.validate_on_submit()
            and (title_changed or editorial_changed or published_changed)):
        if title_changed:
            lst.title = form.data['title']
        if editorial_changed:
            lst.editorial = form.data['editorial']
        if published_changed:
            lst.published = form.data['published']
        db.session.commit()

    return lst.to_dict()
Exemple #2
0
 def get(self):
     form = ListForm(request.args)
     if not form.validate():
         return form.errors, 400
     data, page_token = for_user(form.user_id.data, True,
                                 form.page_token.data)
     rsp = NotificationResponse(page_token,
                                data=default_schema.dump(data, many=True))
     return rsp.to_response(), 200
Exemple #3
0
def new_list():
    form = ListForm()
    form['csrf_token'].data = request.cookies['csrf_token']
    if form.validate_on_submit():
        new_list = List(
            title=form.data['title'],
            editorial=form.data['editorial'],
            user_id=current_user.to_dict()['id'],
        )
        db.session.add(new_list)
        db.session.commit()
        return new_list.to_dict()
Exemple #4
0
def list(list_id=None):
    if list_id:
        obj = ItemList.query.filter_by(id=list_id).first()
        form = ListForm(obj=obj)
    else:
        form = ListForm()

    if form.add_item.data:
        form.items.append_entry()
        return render_template('list.html', form=form)

    if form.remove_item.data:
        form.items.pop_entry()
        return render_template('list.html', form=form)

    if form.validate_on_submit():
        if form.data['id'] != '':
            itemlist = ItemList.query.filter_by(
                id=int(form.data['id'])).first()
            itemlist.name = form.data['name']
        else:
            itemlist = ItemList(name=form.data['name'],
                                owner_id=current_user.id)
        db.session.add(itemlist)
        db.session.commit()

        Item.query.filter_by(list_id=itemlist.id).delete()
        db.session.commit()

        for item in form.data['items']:
            new_item = Item(name=item['name'],
                            quantity=item['quantity'],
                            category=item['category'],
                            bought=item['bought'],
                            list_id=itemlist.id)
            db.session.add(new_item)
        db.session.commit()
        return redirect(url_for('index'))

    return render_template('list.html', form=form)
Exemple #5
0
def index(request):
    list = List.objects.order_by('created_at')
    if request.method == 'POST':
        form = ListForm(request.POST)
        if form.is_valid():
            form.save()

    else:
        form = ListForm()

    return render(request, 'index.html', {'list': list, 'form': form})
Exemple #6
0
def home():
    valid_form = False
    is_section1 = False
    is_section2 = False
    is_section3 = False
    overlap = False
    form = ClassesForm()
    list_form = ListForm()

    if form.validate_on_submit():
        # Used to activate the if valid_form clause in base.html
        valid_form = True

        # Get the data of the form submitted by the user
        year_data = form.year.data
        semester_data = form.semester.data
        section1_data = form.section1.data
        section2_data = form.section2.data
        section3_data = form.section3.data
        print(year_data, semester_data, section1_data, section2_data)

        # Filter the list of classes according to the choices of the user in the form
        list_classes_filtered1 = choice_user_controller(
            list_classes, year_data, semester_data, section1_data)
        list_classes_filtered2 = choice_user_controller(
            list_classes, year_data, semester_data, section2_data)
        list_classes_filtered3 = choice_user_controller(
            list_classes, year_data, semester_data, section3_data)

        # Create the list of classes the user can chose from for the section 1 and 2
        if section1_data:
            is_section1 = True
            list_form.selection1.label = "Choisir les cours en " + section1_data
            list_form.selection1.choices = [
                (item['id'],
                 "<span class=\"time-of-class\">" + item['Day'] + " | " +
                 item['TimeStart'] + "-" + item['TimeEnd'] + "</span><br>" +
                 "<span class=\"name-of-class\">" + item['Name'] +
                 "</span><br>" + "<span class=\"teacher-of-class\">" +
                 item['Teacher'] + "</span>")
                for item in list_classes_filtered1
            ]
            print(list_form.selection1.choices)
        if section2_data:
            is_section2 = True
            list_form.selection2.label = "Choisir les cours en " + section2_data
            list_form.selection2.choices = [
                (item['id'],
                 "<span class=\"time-of-class\">" + item['Day'] + " | " +
                 item['TimeStart'] + "-" + item['TimeEnd'] + "</span><br>" +
                 "<span class=\"name-of-class\">" + item['Name'] +
                 "</span><br>" + "<span class=\"teacher-of-class\">" +
                 item['Teacher'] + "</span>")
                for item in list_classes_filtered2
            ]
        if section3_data:
            is_section3 = True
            list_form.selection3.label = "Choisir les cours en " + section3_data
            list_form.selection3.choices = [
                (item['id'],
                 "<span class=\"time-of-class\">" + item['Day'] + " | " +
                 item['TimeStart'] + "-" + item['TimeEnd'] + "</span><br>" +
                 "<span class=\"name-of-class\">" + item['Name'] +
                 "</span><br>" + "<span class=\"teacher-of-class\">" +
                 item['Teacher'] + "</span>")
                for item in list_classes_filtered3
            ]

    else:
        print(form.errors)
        print("error in myform")

    # If the user has selected classes and submit it
    if list_form.validate_on_submit():
        print(list_form.selection1.data)
        print("list_form validate")
        list_classes_selected = []
        list_overlap = []

        # Puts the classes selected by the user in a list
        for item in list_classes:
            # The data retrieved from the list of classes is just the id of the class
            for classe_id in list_form.selection1.data:
                if str(item['id']) == str(classe_id):
                    # event-1 used to determine the category of the event in schedule.html
                    item['event'] = 'event-1'
                    list_classes_selected.append(item)
            for classe_id in list_form.selection2.data:
                if str(item['id']) == str(classe_id):
                    # event-2 used to determine the category of the event in schedule.html
                    item['event'] = 'event-2'
                    list_classes_selected.append(item)
            for classe_id in list_form.selection3.data:
                if str(item['id']) == str(classe_id):
                    # event-3 used to determine the category of the event in schedule.html
                    item['event'] = 'event-3'
                    list_classes_selected.append(item)

        # Checks if there is an overlap in the classes selected by the user
        if list_classes_selected:
            nbr_classes = len(list_classes_selected)
            idx1 = 0
            idx2 = 1

            while idx1 < nbr_classes - 1:
                chevauchement = False
                classe1 = list_classes_selected[idx1]
                while idx2 < nbr_classes:
                    classe2 = list_classes_selected[idx2]
                    # Checks if classes are on the same day and start at the same time or during one of the classes
                    if classe1['Day'] == classe2['Day'] and \
                            (classe1['TimeStart'] <= classe2['TimeStart'] < classe1['TimeEnd'] or
                            classe2['TimeStart'] <= classe1['TimeStart'] < classe2['TimeEnd']):
                        print("overlap True")
                        overlap = True
                        chevauchement = True
                    idx2 += 1
                # list-overlap used to display day and time of overlap in schedule.html
                if chevauchement:
                    list_overlap.append([classe1['Day'], classe1['TimeStart']])
                idx1 += 1
                idx2 = idx1 + 1

        print(list_classes_selected)

        if list_form.selection1.data or list_form.selection2.data or list_form.selection3.data:
            """
            return render_template(
                'schedule.html',
                list_classes=list_classes_selected,
                overlap_checker=overlap,
                overlap_info=list_overlap,
            )
            """
            print("list selected", list_classes_selected)

            i = 0
            while i < len(list_classes_selected):
                list_classes_selected[i]['id'] = "i+1"
                list_classes_selected[i]['HourStart'] = list_classes_selected[
                    i]['TimeStart'][:2]
                list_classes_selected[i][
                    'MinuteStart'] = list_classes_selected[i]['TimeStart'][-2:]
                list_classes_selected[i]['HourEnd'] = list_classes_selected[i][
                    'TimeEnd'][:2]
                list_classes_selected[i]['MinuteEnd'] = list_classes_selected[
                    i]['TimeEnd'][-2:]
                i += 1

            print("list selected update", list_classes_selected)

            return render_template(
                'weekcalendar.html',
                list_classes=json.dumps(list_classes_selected))

    else:
        print(list_form.errors)
        print("error in list_form")

    return render_template(
        'index.html',
        myform=form,
        list_form=list_form,
        valid_form=valid_form,
        is_section1=is_section1,
        is_section2=is_section2,
        is_section3=is_section3,
    )
def views(request):

    data = {'title': 'To Do Application - My Lists'}

    if request.method == 'POST':
        if 'action' in request.POST and request.POST['action'] != '':
            action = request.POST['action']

            if action == 'create':
                try:
                    with transaction.atomic():
                        f = ListForm(request.POST)
                        if f.is_valid():
                            name = f.cleaned_data['name']
                            priority = f.cleaned_data['priority']
                            assigned_to = f.cleaned_data['assigned_to']
                            due_date = f.cleaned_data['due_date']

                            if not name:
                                return bad_json(
                                    message='Please enter a list name.')

                            if not priority:
                                return bad_json(
                                    message='Please select a priority.')

                            if not assigned_to:
                                return bad_json(
                                    message=
                                    'Please select a user to assign this list.'
                                )

                            if not due_date:
                                return bad_json(
                                    message=
                                    'Please select a due date for this list.')

                            list = List(name=name,
                                        priority=priority,
                                        assigned_to=assigned_to,
                                        created_by=request.user,
                                        due_date=due_date)
                            list.save()

                            return ok_json(
                                data={
                                    'message':
                                    'You have successfully created a list',
                                    'redirect_url': '/lists'
                                })
                        else:
                            # for field in f.errors:
                            #     error = field + ": " + field.data[0]
                            #     break
                            return bad_json(extradata={'errors': f._errors})
                except Exception as ex:
                    return bad_json(message=ex.__str__())

            if action == 'edit':
                try:
                    with transaction.atomic():
                        f = ListForm(request.POST)
                        if f.is_valid():
                            name = f.cleaned_data['name']
                            priority = f.cleaned_data['priority']
                            assigned_to = f.cleaned_data['assigned_to']
                            due_date = f.cleaned_data['due_date']
                            list = None

                            if 'id' in request.POST and request.POST[
                                    'id'] != '':
                                if List.objects.filter(
                                        id=int(request.POST['id'])).exists():
                                    list = List.objects.get(
                                        id=int(request.POST['id']))

                            if not list:
                                return bad_json(
                                    message='Please select a list to edit.')

                            if not name:
                                return bad_json(
                                    message='Please enter a list name.')

                            if not priority:
                                return bad_json(
                                    message='Please select a priority.')

                            if not assigned_to:
                                return bad_json(
                                    message=
                                    'Please select a user to assign this list.'
                                )

                            if not due_date:
                                return bad_json(
                                    message=
                                    'Please select a due date for this list.')

                            list.name = name
                            list.priority = priority
                            list.assigned_to = assigned_to
                            list.due_date = due_date
                            list.save()

                            return ok_json(
                                data={
                                    'message':
                                    'You have successfully edited a list',
                                    'redirect_url': '/lists'
                                })
                        else:
                            return bad_json(extradata={'errors': f._errors})
                except Exception as ex:
                    return bad_json(message=ex.__str__())

            if action == 'add_task':
                try:
                    with transaction.atomic():
                        f = TaskForm(request.POST)
                        if f.is_valid():
                            title = f.cleaned_data['title']
                            priority = f.cleaned_data['priority']
                            assigned_to = f.cleaned_data['assigned_to']
                            due_date = f.cleaned_data['due_date']
                            note = f.cleaned_data['note']
                            list = None

                            if 'id' in request.POST and request.POST[
                                    'id'] != '':
                                if List.objects.filter(
                                        id=int(request.POST['id'])).exists():
                                    list = List.objects.get(
                                        id=int(request.POST['id']))

                            if not list:
                                return bad_json(
                                    message=
                                    'Please select a list to add this task to.'
                                )

                            if not title:
                                return bad_json(
                                    message='Please enter a task title.')

                            if not priority:
                                return bad_json(
                                    message='Please select a priority.')

                            if not assigned_to:
                                return bad_json(
                                    message=
                                    'Please select a user to assign this task to.'
                                )

                            if not due_date:
                                return bad_json(
                                    message=
                                    'Please select a due date for this task.')

                            if not note:
                                return bad_json(
                                    message='Please enter a note for this task.'
                                )

                            task = Task(list=list,
                                        title=title,
                                        priority=priority,
                                        assigned_to=assigned_to,
                                        created_by=request.user,
                                        due_date=due_date,
                                        note=note)
                            task.save()

                            return ok_json(
                                data={
                                    'message':
                                    'You have successfully added a task',
                                    'redirect_url':
                                    '/lists?action=view&id={}'.format(list.id)
                                })
                        else:
                            # for field in f.errors:
                            #     error = field + ": " + field.data[0]
                            #     break
                            return bad_json(extradata={'errors': f._errors})
                except Exception as ex:
                    return bad_json(message=ex.__str__())

            if action == 'edit_task':
                try:
                    with transaction.atomic():
                        f = TaskForm(request.POST)
                        if f.is_valid():
                            title = f.cleaned_data['title']
                            priority = f.cleaned_data['priority']
                            assigned_to = f.cleaned_data['assigned_to']
                            due_date = f.cleaned_data['due_date']
                            note = f.cleaned_data['note']
                            task = None

                            if 'id' in request.POST and request.POST[
                                    'id'] != '':
                                if Task.objects.filter(
                                        id=int(request.POST['id'])).exists():
                                    task = Task.objects.get(
                                        id=int(request.POST['id']))

                            if not task:
                                return bad_json(
                                    message='Please select a task to edit.')

                            if not title:
                                return bad_json(
                                    message='Please enter a task title.')

                            if not priority:
                                return bad_json(
                                    message='Please select a priority.')

                            if not assigned_to:
                                return bad_json(
                                    message=
                                    'Please select a user to assign this task to.'
                                )

                            if not due_date:
                                return bad_json(
                                    message=
                                    'Please select a due date for this task.')

                            if not note:
                                return bad_json(
                                    message='Please enter a note for this task.'
                                )

                            task.title = title
                            task.priority = priority
                            task.assigned_to = assigned_to
                            task.due_date = due_date
                            task.note = note
                            task.save()

                            return ok_json(
                                data={
                                    'message':
                                    'You have successfully edited a task',
                                    'redirect_url':
                                    '/lists?action=view&id={}'.format(
                                        task.list.id)
                                })
                        else:
                            # for field in f.errors:
                            #     error = field + ": " + field.data[0]
                            #     break
                            return bad_json(extradata={'errors': f._errors})
                except Exception as ex:
                    return bad_json(message=ex.__str__())

            if action == 'add_comment':
                try:
                    with transaction.atomic():
                        f = CommentForm(request.POST)
                        if f.is_valid():
                            body = f.cleaned_data['body']
                            task = None

                            if 'id' in request.POST and request.POST[
                                    'id'] != '':
                                if Task.objects.filter(
                                        id=int(request.POST['id'])).exists():
                                    task = Task.objects.get(
                                        id=int(request.POST['id']))

                            if not task:
                                return bad_json(
                                    message=
                                    'Please select a task to add this comment to.'
                                )

                            if not body:
                                return bad_json(
                                    message=
                                    'Please enter a comment for this task.')

                            comment = Comment(
                                task=task,
                                author=User.objects.get(id=request.user.id),
                                body=body)
                            comment.save()

                            return ok_json(
                                data={
                                    'message':
                                    'You have successfully added a comment',
                                    'redirect_url':
                                    '/lists?action=view_task&id={}'.format(
                                        task.id)
                                })
                        else:
                            return bad_json(extradata={'errors': f._errors})
                except Exception as ex:
                    return bad_json(message=ex.__str__())

    else:
        if 'action' in request.GET:
            if 'action' in request.GET and request.GET['action'] != '':
                action = request.GET['action']

                if action == 'create':
                    data['title'] = 'Create a new List'
                    data['form'] = ListForm()
                    return render(request, 'lists/create.html', data)

                if action == 'edit':
                    if 'id' in request.GET and request.GET['id'] != '':
                        if List.objects.filter(
                                id=int(request.GET['id'])).exists():
                            data['list'] = list = List.objects.get(
                                id=int(request.GET['id']))
                            data['title'] = 'Edit List: ' + list.name
                            data['form'] = ListForm(
                                initial={
                                    'name': list.name,
                                    'priority': list.priority,
                                    'assigned_to': list.assigned_to,
                                    'due_date': list.due_date
                                })

                            return render(request, 'lists/edit.html', data)

                if action == 'view':
                    if 'id' in request.GET and request.GET['id'] != '':
                        if List.objects.filter(
                                id=int(request.GET['id'])).exists():
                            data['list'] = list = List.objects.get(
                                id=int(request.GET['id']))

                            data['title'] = 'Viewing List: ' + list.name
                            return render(request, 'lists/view.html', data)

                if action == 'add_task':
                    if 'id' in request.GET and request.GET['id'] != '':
                        if List.objects.filter(
                                id=int(request.GET['id'])).exists():
                            data['list'] = list = List.objects.get(
                                id=int(request.GET['id']))
                            data['title'] = 'Add Task to: ' + list.name
                            data['form'] = TaskForm()
                    return render(request, 'tasks/create.html', data)

                if action == 'edit_task':
                    if 'id' in request.GET and request.GET['id'] != '':
                        if Task.objects.filter(
                                id=int(request.GET['id'])).exists():
                            data['task'] = task = Task.objects.get(
                                id=int(request.GET['id']))
                            data['title'] = 'Edit Task for: ' + task.list.name
                            data['form'] = TaskForm(
                                initial={
                                    'title': task.title,
                                    'due_date': task.due_date,
                                    'assigned_to': task.assigned_to,
                                    'note': task.note,
                                    'priority': task.priority
                                })

                    return render(request, 'tasks/edit.html', data)

                if action == 'mark_complete_task':
                    if 'id' in request.GET and request.GET['id'] != '':
                        if Task.objects.filter(
                                id=int(request.GET['id'])).exists():
                            task = Task.objects.get(id=int(request.GET['id']))
                            task.completed = True
                            task.save()
                            return HttpResponseRedirect(
                                '/lists?action=view&id={}'.format(
                                    task.list.id))
                    return HttpResponseRedirect('/lists')

                if action == 'mark_incomplete_task':
                    if 'id' in request.GET and request.GET['id'] != '':
                        if Task.objects.filter(
                                id=int(request.GET['id'])).exists():
                            task = Task.objects.get(id=int(request.GET['id']))
                            task.completed = False
                            task.save()
                            return HttpResponseRedirect(
                                '/lists?action=view&id={}'.format(
                                    task.list.id))
                    return HttpResponseRedirect('/lists')

                if action == 'delete_task':
                    if 'id' in request.GET and request.GET['id'] != '':
                        if Task.objects.filter(
                                id=int(request.GET['id'])).exists():
                            task = Task.objects.get(id=int(request.GET['id']))
                            list = task.list
                            task.delete()
                            return HttpResponseRedirect(
                                '/lists?action=view&id={}'.format(list.id))
                    return HttpResponseRedirect('/lists')

                if action == 'delete':
                    if 'id' in request.GET and request.GET['id'] != '':
                        if List.objects.filter(
                                id=int(request.GET['id'])).exists():
                            list = List.objects.get(id=int(request.GET['id']))
                            list.delete()
                    return HttpResponseRedirect('/lists')

                if action == 'view_task':
                    if 'id' in request.GET and request.GET['id'] != '':
                        if Task.objects.filter(
                                id=int(request.GET['id'])).exists():
                            data['task'] = task = Task.objects.get(
                                id=int(request.GET['id']))

                            data['title'] = 'Viewing Task: ' + task.title
                            return render(request, 'tasks/view.html', data)

                if action == 'add_comment':
                    if 'id' in request.GET and request.GET['id'] != '':
                        if Task.objects.filter(
                                id=int(request.GET['id'])).exists():
                            data['task'] = task = Task.objects.get(
                                id=int(request.GET['id']))
                            data['form'] = CommentForm()

                            data['title'] = 'Comment on Task: ' + task.title
                            return render(request, 'comment/create.html', data)

    data['lists'] = List.objects.all()
    return render(request, 'lists.html', data)