def activity_update(request, pk, group_pk):
    activity = get_object_or_404(Activity, pk=pk)
    previous_status = activity.definite
    group = get_object_or_404(Group, pk=group_pk)
    if not request.user.id == group.creator.id:
        msg = "You don't have permission to edit this activity."
        return HttpResponse(msg, mimetype='text/html')
    if request.method != "POST":
        form = ActivityForm(instance=activity)
    else:
        form = ActivityForm(request.POST, instance=activity)
        if form.is_valid():
            form.save()
            if not previous_status and activity.definite:
                tweet(request, group, activity)
            return redirect('meetup:activity_view', pk=pk, group_pk=group_pk)
    return render(request, tmpl('update.html'),
                  {'form': form, 'group': group, 'activity': activity})
def activity_create(request, group_pk):
    group = get_object_or_404(Group, pk=group_pk)
    if request.user == group.creator.id:
        msg = "You don't have permission to create an activity in this group."
        return HttpResponse(msg, mimetype='text/html')
    if request.method != "POST":
        form = ActivityForm()
    else:
        form = ActivityForm(request.POST)
        if form.is_valid():
            activity = form.save(commit=False)
            activity.group = group
            activity.save()
            form.save_m2m()  # Required because of previous commit=False.

            if activity.definite:
                tweet(request, group, activity)

            return redirect('meetup:group_view', pk=group_pk)
        # Validation errors will automatically be propagated to the view.
    return render(request, tmpl('create.html'), {'form': form, 'group': group})
def activity_update(request, pk, group_pk):
    activity = get_object_or_404(Activity, pk=pk)
    previous_status = activity.definite
    group = get_object_or_404(Group, pk=group_pk)
    if not request.user.id == group.creator.id:
        msg = "You don't have permission to edit this activity."
        return HttpResponse(msg, mimetype='text/html')
    if request.method != "POST":
        form = ActivityForm(instance=activity)
    else:
        form = ActivityForm(request.POST, instance=activity)
        if form.is_valid():
            form.save()
            if not previous_status and activity.definite:
                tweet(request, group, activity)
            return redirect('meetup:activity_view', pk=pk, group_pk=group_pk)
    return render(request, tmpl('update.html'), {
        'form': form,
        'group': group,
        'activity': activity
    })
def activity_create(request, group_pk):
    group = get_object_or_404(Group, pk=group_pk)
    if request.user  == group.creator.id:
        msg = "You don't have permission to create an activity in this group."
        return HttpResponse(msg, mimetype='text/html')
    if request.method != "POST":
        form = ActivityForm()
    else:
        form = ActivityForm(request.POST)
        if form.is_valid():
            activity = form.save(commit=False)
            activity.group = group
            activity.save()
            form.save_m2m()  # Required because of previous commit=False.

            if activity.definite:
                    tweet(request, group, activity)

            return redirect('meetup:group_view', pk=group_pk)
        # Validation errors will automatically be propagated to the view.
    return render(request, tmpl('create.html'), {'form': form, 'group': group})