Ejemplo n.º 1
0
def org_edit(request, org_id):
    org = get_object_or_404(Organization, id=org_id)
    if not (request.user.is_staff or org.can_edit(request.user)):
        return HttpResponseForbidden("Forbidden")

    OrgFormSet = formset_factory(LinkForm, formset=BaseLinkFormSet)

    org_links = org.websites.all()
    link_data = [{
        'url_type': l.url_type,
        'url': l.url,
        'org_id': org.id
    } for l in org_links]

    if request.method == "POST":
        form = OrganizationForm(request.POST, request.FILES)
        org_link_formset = OrgFormSet(request.POST)
        form.public = request.POST['public']
        try:
            if form.is_valid():
                if org_link_formset.is_valid():
                    for link in link_data:
                        del_url = link.get('url')
                        org.websites.filter(url=del_url).delete()
                    for link_form in org_link_formset:
                        if not link_form.cleaned_data.get('org_id'):
                            link_form.cleaned_data['org_id'] = org.id
                        try:
                            if link_form.is_valid():
                                url_type = link_form.cleaned_data.get(
                                    'url_type')
                                url = link_form.cleaned_data.get('url')

                                if url_type and url:
                                    link_form.save()
                        except Exception as e:
                            print(("Could not save website: %s" % str(e)))

                    form.save()
                    return HttpResponseRedirect(
                        reverse('member:org:view', kwargs={'org_id': org.id}))
                else:
                    messages.error(
                        request,
                        'Please make sure the websites have a valid URL and URL type.'
                    )
        except Exception as e:
            messages.add_message(request, messages.ERROR,
                                 "Could not save: %s" % str(e))
    else:
        form = OrganizationForm(instance=org)
        org_link_formset = OrgFormSet(initial=link_data)

    context = {
        'organization': org,
        'form': form,
        'org_link_formset': org_link_formset,
    }
    return render(request, 'member/organization/org_edit.html', context)
Ejemplo n.º 2
0
def org_edit_photo(request, org_id):
    org = get_object_or_404(Organization, id=org_id)
    if not (request.user.is_staff or org.can_edit(request.user)):
        return HttpResponseForbidden("Forbidden")
    if request.method == 'POST':
        form = OrganizationForm(request.POST, request.FILES)
        org.photo = request.FILES.get('photo', None)

        org.save()

        return HttpResponseRedirect(
            reverse('member:org:view', kwargs={'org_id': org.id}))

    else:
        form = OrganizationForm(request.POST, request.FILES)

    context = {'organization': org}
    return render(request, 'members/organization/org_edit_photo.html', context)
Ejemplo n.º 3
0
def org_edit(request, org_id):
    org = get_object_or_404(Organization, id=org_id)
    if not (request.user.is_staff or org.can_edit(request.user)):
        return HttpResponseForbidden("Forbidden")

    OrgFormSet = formset_factory(LinkForm, formset=BaseLinkFormSet)

    org_links = org.websites.all()
    link_data = [{'url_type': l.url_type, 'url': l.url, 'org_id': org.id} for l in org_links]

    if request.method == "POST":
        form = OrganizationForm(request.POST, request.FILES)
        org_link_formset = OrgFormSet(request.POST)
        form.public = request.POST['public']
        try:
            if form.is_valid():
                if org_link_formset.is_valid():
                    for link in link_data:
                        del_url = link.get('url')
                        org.websites.filter(url=del_url).delete()
                    for link_form in org_link_formset:
                        if not link_form.cleaned_data.get('org_id'):
                            link_form.cleaned_data['org_id'] = org.id
                        try:
                            if link_form.is_valid():
                                url_type = link_form.cleaned_data.get('url_type')
                                url = link_form.cleaned_data.get('url')

                                if url_type and url:
                                    link_form.save()
                        except Exception as e:
                            print(("Could not save website: %s" % str(e)))

                    form.save()
                    return HttpResponseRedirect(reverse('member:org:view', kwargs={'org_id': org.id}))
                else:
                    messages.error(request, 'Please make sure the websites have a valid URL and URL type.')
        except Exception as e:
            messages.add_message(request, messages.ERROR, "Could not save: %s" % str(e))
    else:
        form = OrganizationForm(instance=org)
        org_link_formset = OrgFormSet(initial=link_data)

    context = {'organization': org,
               'form': form,
               'org_link_formset': org_link_formset,
               }
    return render(request, 'member/organization/org_edit.html', context)
Ejemplo n.º 4
0
def org_edit(request, org_id):
    org = get_object_or_404(Organization, id=org_id)
    if not (request.user.is_staff or org.can_edit(request.user)):
        return HttpResponseForbidden("Forbidden")

    if request.method == "POST":
        form = OrganizationForm(request.POST, request.FILES)
        form.public = request.POST['public']
        try:
            if form.is_valid():
                form.save()
                return HttpResponseRedirect(reverse('member_org_view', kwargs={'org_id': org.id}))
        except Exception as e:
            messages.add_message(request, messages.ERROR, "Could not save: %s" % str(e))
    else:
        form = OrganizationForm(instance=org)

    context = {'organization': org, 'form':form,}
    return render(request, 'members/org_edit.html', context)