Example #1
0
def create_travel(request):
    if request.POST:
        form = TravelForm(request.POST, request.FILES)

        if form.is_valid():
            print("Form valid")
            travel = form.save(commit=False)
            wu_user = WuProfil.objects.get(user=request.user)
            travel.author = wu_user
            stage_formset = StageFormSet(request.POST, instance=travel)
            if stage_formset.is_valid():
                travel.save()
                stage_formset.save()
                form.save_m2m()

                Historique.newTravelFact(
                    actor=request.user.wuprofil, 
                    action_type="TC",
                    object_travel=travel
                )

                return travel_list(request)
            else :
                print("Stage formset invalid")
                print(form.errors)
        else :
            print("Travel formset invalid")
            print(form.errors)
    else:
        form = TravelForm()
        stage_formset = StageFormSet(instance=Travel())

    return render(request, "travel/add_travel.html", {"form": form, "stage_formset": stage_formset})
Example #2
0
def register(request):

    # Used later if registration is valid
    registered = False

    if request.method == 'POST':
        # Attempt to grab information from the raw form information.
        # Note that we make use of both UserForm and UserProfileForm.
        user_form = UserForm(data=request.POST)
        profile_form = UserProfileForm(data=request.POST)

        # If the two forms are valid...
        if user_form.is_valid() and profile_form.is_valid():
            # create regular user in database + set password
            user = user_form.save()
            user.set_password(user.password)
            user.save()

            # commit= false delays the transaction for WuProfil
            profile = profile_form.save(commit=False)
            profile.user = user

            if 'image' in request.FILES:
                profile.image = request.FILES['image']

            # Now we save the UserProfile model instance.
            profile.save()

            # Update our variable to tell the template registration was successful.
            registered = True

            Historique.newUserFact(
                actor=user.wuprofil, 
                action_type="UJ",
                object_user=user.wuprofil
            )

        # Invalid form or forms - mistakes or something else?
        # Print problems to the terminal.
        # They'll also be shown to the user.
        else:
            print(user_form.errors, profile_form.errors)

    # Not a HTTP POST, so we render our form using two ModelForm instances.
    # These forms will be blank, ready for user input.
    else:
        user_form = UserForm()
        profile_form = UserProfileForm()

    # Render the template depending on the context.
    return render(request,
            'wu/register.html',
            {'user_form': user_form, 'profile_form': profile_form, 'registered': registered})
Example #3
0
def travel_subscribe(request, pk):
    travel = get_object_or_404(Travel, pk=pk)
    if len(travel.participants.filter(user_id=request.user.id)) < 1:
        participation=Participate(person=request.user.wuprofil, travel=travel, motivation=5)
        participation.save()

        Historique.newTravelFact(
            actor=request.user.wuprofil,
            action_type="TS",
            object_travel=travel
        )


    return HttpResponseRedirect('/travel/%s/' % pk)
Example #4
0
def travel_unsubscribe(request, pk):
    travel = get_object_or_404(Travel, pk=pk)
    participations=Participate.objects.filter(person__user__id=request.user.id, travel_id=pk)
    if len(participations) > 0:
        for participation in participations:
            participation.delete()

        Historique.newTravelFact(
            actor=request.user.wuprofil,
            action_type="TU",
            object_travel=travel
        )


    return HttpResponseRedirect('/travel/%s/' % pk)