Пример #1
0
def flat(request, flatid=None):
    context = RequestContext(request)
    u = User.objects.get(username=request.user)
    flat = Flat.objects.filter(id = flatid)
    full_list = Task.objects.filter(flat = flat)
    try:
        userFlatMember = Flat_Member.objects.get(user=u, flat=flat)
    except:
        #If user do not belong to flat
        raise PermissionDenied
    link = "/flats/" + flatid
    success = None
    user_lives_in_flat = livesInFlat(u, flat)

    if "deleteFlat" in request.POST and user_lives_in_flat:
        flat_id = request.POST.get('flat_id')
        flat = Flat.objects.get(id = flat_id)
        flat.delete()
        return HttpResponseRedirect("/flats")

    task_list = []
    shopping_list = []
    summaryList = []
    members_in_flat = Flat_Member.objects.filter(flat = flat)
    for member in members_in_flat:
        #Member
        row = []
        row.append(member.user.first_name)
        #Last task completed and date completed
        try:
            last_task = Assigned_Task.objects.filter(member = member).latest()
            last_task_name = last_task.task.name
            last_task_date = last_task.completion_date
        except:
            last_task_name = "N/A"
            last_task_date = "N/A"

        row.append(last_task_name)
        row.append(last_task_date)

        #total score
        total_score = 0
        for a_task in Assigned_Task.objects.filter(member = member):
            total_score = total_score + a_task.task.credits
        row.append(total_score)

        summaryList.append(row)

    for list_item in full_list:
        if list_item.category.name != "Shopping":
            task_list.append(list_item)
        else:
            shopping_list.append(list_item)

    flat_members = Flat_Member.objects.filter(flat=flat)

    new_task_form = NewTaskForm()

    if "setTaskDone" in request.POST and user_lives_in_flat:
        task_id = request.POST.get('task_id')
        task = Task.objects.get(id = task_id)
        assigned_task = Assigned_Task()
        assigned_task.task = task
        assigned_task.member = userFlatMember
        assigned_task.save()
        return HttpResponseRedirect(link)

    if "setShoppingItemDone" in request.POST and user_lives_in_flat:
        task_id = request.POST.get('task_id')
        task = Task.objects.get(id = task_id)
        assigned_task = Assigned_Task()
        assigned_task.task = task
        assigned_task.member = userFlatMember
        assigned_task.save()
        task.delete()

        full_list = Task.objects.filter(flat = flat )
        shopping_list = []

        for list_item in full_list:
            if list_item.category.name == "Shopping":
                shopping_list.append(list_item)
        return HttpResponseRedirect(link)

    if "deleteTaskItem" in request.POST and user_lives_in_flat:
        task_id = request.POST.get('task_id')
        print (task_id)
        task = Task.objects.get(id = task_id)
        task.delete()
        return HttpResponseRedirect(link)

    if "createNewItem" in request.POST and user_lives_in_flat:
        new_task_form = NewTaskForm(request.POST)

        if new_task_form.is_valid():
            task = new_task_form.save(commit=False)
            task.flat = flat[0]
            task.save()
            full_list = Task.objects.filter(flat = flat )
            task_list = []
            shopping_list = []

            for list_item in full_list:
                if list_item.category.name != "Shopping":
                    task_list.append(list_item)
                else:
                    shopping_list.append(list_item)
            
            return HttpResponseRedirect(link)
        else:
            print (new_task_form.errors)

    if user_lives_in_flat:
        return render_to_response('flats/flat.html', {'flat_info': flat[0], 'task_list' : task_list, 'shopping_list' : shopping_list, 'flat_members' : flat_members, 'task_form':new_task_form, 'summaryList':summaryList} , context)
    else:
        raise PermissionDenied
Пример #2
0
task8.save()

task9 = Task(name ="Take out garbage", description = "From kitchen and bathroom", credits = 4, flat = flat3, category = cat1)
task9.save()

task10 = Task(name ="Clean Toilet", description = "I know its bad..", credits = 25, flat = flat3, category = cat1)
task10.save()

task11 = Task(name ="Toiletpaper", description = "As Soon As Possible", credits = 5, flat = flat3, category = cat2)
task11.save()

task12 = Task(name ="Vaccum cleaner bags", description = "Size 30x25", credits = 5, flat = flat4, category = cat2)
task12.save()

assigned1 = Assigned_Task(task = task1, member = flatmember1)
assigned1.save()

assigned2 = Assigned_Task(task = task2, member = flatmember1)
assigned2.save()

assigned4 = Assigned_Task(task = task5, member = flatmember1)
assigned4.save()

assigned5 = Assigned_Task(task = task7, member = flatmember3)
assigned5.save()

assigned6 = Assigned_Task(task = task8, member = flatmember3)
assigned6.save()

invite1 = Invitation(flat=flat1, email = "*****@*****.**")
invite1.save()