예제 #1
0
def index(request):
    context = RequestContext(request)
    success = None

    try:
        u = User.objects.get(username=request.user)
        flats_user = Flat_Member.objects.filter(user=u)

        for fu in flats_user:
            flat_members = Flat_Member.objects.filter(flat=fu.flat)
            fu.member_list = flat_members

            if fu.flat.active:
                flat_members = Flat_Member.objects.filter(flat=fu.flat)
                fu.member_list = flat_members
            else:
                fu.delete()

        #Get all flats to check if on invite lists
        invited_flats = []
        flats = Flat.objects.all()
        for flat in flats:
            invites = Invitation.objects.filter(flat = flat)
            for invite in invites:
                if invite.email == u.email:
                    invited_flats.append(flat)

        # Create a new flat form
        new_flat_form = NewFlatForm()

        # Accept a new invite to join a flat
        if "acceptInvite" in request.POST :
            flat_id = request.POST.get('flat_id')
            flat = Flat.objects.get(id = flat_id)
            already_member = False
            for m in Flat_Member.objects.filter(flat = flat):
                if m.user == u:
                    already_member = True
            if not already_member:
                member = Flat_Member.objects.create_flat_member(u, flat)
                member.save()
                success = True

            invites = Invitation.objects.filter(flat = flat)
            for invite in invites:
                if invite.email == u.email:
                    invite.delete()
                    invited_flats.remove(flat)
            return HttpResponseRedirect("/flats")

        # Ask people to join your flat
        if "sendInvite" in request.POST :
            flat_id = request.POST.get('flat_id')
            email = request.POST.get('email')
            flat = Flat.objects.get(id = flat_id)
            already_member = False
            already_invited = False
            members = Flat_Member.objects.filter(flat = flat)
            for member in members:
                if member.user.email == email:
                    already_member = True
            invites = Invitation.objects.filter(flat = flat)
            for invite in invites:
                if invite.email == email:
                    already_invited = True

            if not already_member and not already_invited:
                newInvite = Invitation()
                newInvite.flat = flat
                newInvite.email = email
                newInvite.save()
                #If email and password is set in the settings
                #then send an email
                if EMAIL_HOST_PASSWORD and EMAIL_HOST_USER:
                    send_email(email)
                success = True
            return HttpResponseRedirect("/flats")

        # Create a new flat
        if "createNewFlat" in request.POST:
            new_flat_form = NewFlatForm(request.POST)
            if new_flat_form.is_valid():
                flat = new_flat_form.save(commit=False)
                flat.save()
                success = True

                # Link a created flat to current user
                flat_member = Flat_Member.objects.create_flat_member(u, flat)
                flat_member.save()
                return HttpResponseRedirect("/flats")
            else:
                print (new_flat_form.errors)

        # Remove a flat
        
        if "deleteFlat" in request.POST :
            flat_member_id = request.POST.get('flat_id')
            print(flat_member_id)
            flat_member = Flat_Member.objects.get(id=flat_member_id)
            flat_member.delete()
            return HttpResponseRedirect("/flats")

        edit_flat_form = EditFlatInfoForm()

        if "editFlatInfo" in request.POST:
            edit_flat_form = EditFlatInfoForm(request.POST)
            if edit_flat_form.is_valid():	
                flat_id = request.POST.get('flat_flat_id')
                flat = Flat.objects.get(id = flat_id)
                name = edit_flat_form.cleaned_data['name']
                description = edit_flat_form.cleaned_data['description']
                flat.name = name
                flat.description = description
                flat.save()
                return HttpResponseRedirect("/flats")
            else:
                print (edit_flat_form.errors)

        return render_to_response('flats/index.html', { 'flats' : flats_user, 'flat_form' : new_flat_form, 'edit_form' : edit_flat_form, 'invited_flats' : invited_flats}, context)

    except:
        context = RequestContext(request)
        return render_to_response('flats/login.html', {}, context)
예제 #2
0
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()

invite2 = Invitation(flat=flat2, email = "*****@*****.**")
invite2.save()

invite3 = Invitation(flat=flat2, email = "*****@*****.**")
invite3.save()

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