Beispiel #1
0
def resource_edit(request, object_id, template='depot/resource_edit.html'):
    """ edits an existing resource. Uses a wizard-like approach, so checks resource.collection_status
        and hands off to resource_* function handler
    """
    UPDATE_LOCS = 'Update locations'
    UPDATE_TAGS = 'Update tags'
    
    resource = get_one_or_404(id=ObjectId(object_id), user=request.user, perm='can_edit')

    # doc = ''
    # places = None
    template_info = _template_info(request.REQUEST.get('popup', ''))

    if request.method == 'POST':
        result = request.POST.get('result', '') # or request.POST.get('result', '')
        if result == 'Cancel':
            return resource_edit_complete(request, resource, template_info)
        resourceform = ShortResourceForm(request.POST, instance=resource)
        eventform = EventForm(request.POST, instance=resource.calendar_event)
        locationform = LocationUpdateForm(request.POST, instance=resource)
        # shelflifeform = ShelflifeForm(request.POST, instance=resource)
        
        if resourceform.is_valid() and locationform.is_valid() and eventform.is_valid():
            acct = get_account(request.user.id)
            new_loc = locationform.cleaned_data['new_location']
            if new_loc: 
                resource.add_location_from_name(locationform.cleaned_data['new_location'])
                resource.save(author=acct, reindex=True)
            else:
                event_start = eventform.cleaned_data['start']
                if event_start:
                    resource.calendar_event = CalendarEvent(start=event_start, end=eventform.cleaned_data['end'])
                    # print 'event_start', event_start
                    # print 'event_finish', eventform.cleaned_data['end']
                else:
                    resource.calendar_event = None
                resource = resourceform.save()
                
                try:
                    resource.save(author=acct, reindex=True)
                    return resource_edit_complete(request, resource, template_info)
                except OperationError:
                    pass

    else:
        resourceform = ShortResourceForm(instance=resource)
        locationform = LocationUpdateForm(instance=resource)
        eventform = EventForm(instance=resource.calendar_event)
        # if not resource.locations:
        #     doc = resource.uri
        # shelflifeform = ShelflifeForm(instance=resource)
    
    return render_to_response(template,
        RequestContext( request, { 'template_info': template_info, 'object': resource,
            'resourceform': resourceform, 'locationform': locationform, 'eventform': eventform, #'places': places,
            # 'tagsform': tagsform, #'shelflifeform': shelflifeform,
            'UPDATE_LOCS': UPDATE_LOCS, 'UPDATE_TAGS': UPDATE_TAGS  }))
Beispiel #2
0
def resource_add(request, template='depot/resource_edit.html'):
    """adds a new resource"""

    template_info = _template_info(request.REQUEST.get('popup', ''))
    # formclass = ShortResourceForm

    if request.method == 'POST':
        if request.POST.get('result', '') == 'Cancel':
            return resource_edit_complete(request, None, template_info)
        form = ShortResourceForm(request.POST)
        if form.is_valid():
            resource = Resource(**form.cleaned_data)
            # resource.metadata.author = str(request.user.id)
            try:
                # resource.collection_status = COLL_STATUS_LOC_CONF
                user = get_account(request.user.id)
                resource.owner = user
                # save will create default moderation and curation using owner acct
                resource.save(author=user, reindex=True)
                # resource.index()
                # if popup:
                #     return HttpResponseRedirect(reverse('resource-popup-close'))
                return HttpResponseRedirect(
                    '%s?popup=%s' %
                    (reverse('resource-edit',
                             args=[resource.id]), template_info['popup']))
            except OperationError:
                pass

    else:
        description = request.GET.get('t', '').replace('||', '\n')
        initial = {
            'uri': request.GET.get('page', ''),
            'title': request.GET.get('title', ''),
            'description': description[:1250]
        }
        form = ShortResourceForm(initial=initial)

    return render_to_response(
        template,
        RequestContext(request, {
            'resourceform': form,
            'template_info': template_info
        }))
Beispiel #3
0
def resource_add(request, template='depot/resource_edit.html'):
    """adds a new resource"""
    
    template_info = _template_info(request.REQUEST.get('popup', ''))
    # formclass = ShortResourceForm
    

    if request.method == 'POST':
        if request.POST.get('result', '') == 'Cancel':
            return resource_edit_complete(request, None, template_info)
        form = ShortResourceForm(request.POST)
        if form.is_valid():
            resource = Resource(**form.cleaned_data)
            # resource.metadata.author = str(request.user.id)
            try:
                # resource.collection_status = COLL_STATUS_LOC_CONF
                user = get_account(request.user.id)
                resource.owner = user
                # save will create default moderation and curation using owner acct
                resource.save(author=user, reindex=True)
                # resource.index()
                # if popup:
                #     return HttpResponseRedirect(reverse('resource-popup-close'))
                return HttpResponseRedirect('%s?popup=%s' % (reverse('resource-edit', args=[resource.id]), template_info['popup']))
            except OperationError:
                pass
            
    else:
        description= request.GET.get('t', '').replace('||', '\n')
        initial = {
            'uri': request.GET.get('page', ''),
            'title': request.GET.get('title', ''),
            'description': description[:1250]
            }
        form = ShortResourceForm(initial=initial)
    
    return render_to_response(template,
        RequestContext( request, {'resourceform': form, 'template_info': template_info }))
Beispiel #4
0
def resource_add(request, template="depot/resource_edit.html"):
    """adds a new resource"""

    template_info = _template_info(request.REQUEST.get("popup", ""))
    # formclass = ShortResourceForm

    if request.method == "POST":
        if request.POST.get("result", "") == "Cancel":
            return resource_edit_complete(request, None, template_info)
        form = ShortResourceForm(request.POST)
        if form.is_valid():
            resource = Resource(**form.cleaned_data)
            # resource.metadata.author = str(request.user.id)
            try:
                # resource.collection_status = COLL_STATUS_LOC_CONF
                user = get_account(request.user.id)
                resource.owner = user
                # save will create default moderation and curation using owner acct
                resource.save(author=user, reindex=True)
                # resource.index()
                # if popup:
                #     return HttpResponseRedirect(reverse('resource-popup-close'))
                return HttpResponseRedirect(
                    "%s?popup=%s" % (reverse("resource-edit", args=[resource.id]), template_info["popup"])
                )
            except OperationError:
                pass

    else:
        description = request.GET.get("t", "").replace("||", "\n")
        initial = {
            "uri": request.GET.get("page", ""),
            "title": request.GET.get("title", ""),
            "description": description[:1250],
        }
        form = ShortResourceForm(initial=initial)

    return render_to_response(template, RequestContext(request, {"resourceform": form, "template_info": template_info}))
Beispiel #5
0
def resource_edit(request, object_id, template="depot/resource_edit.html"):
    """ edits an existing resource. Uses a wizard-like approach, so checks resource.collection_status
        and hands off to resource_* function handler
    """
    UPDATE_LOCS = "Update locations"
    UPDATE_TAGS = "Update tags"

    resource = get_one_or_404(id=ObjectId(object_id), user=request.user, perm="can_edit")

    # doc = ''
    # places = None
    template_info = _template_info(request.REQUEST.get("popup", ""))

    if request.method == "POST":
        result = request.POST.get("result", "")  # or request.POST.get('result', '')
        if result == "Cancel":
            return resource_edit_complete(request, resource, template_info)
        resourceform = ShortResourceForm(request.POST, instance=resource)
        eventform = EventForm(request.POST, instance=resource.calendar_event)
        locationform = LocationUpdateForm(request.POST, instance=resource)
        # shelflifeform = ShelflifeForm(request.POST, instance=resource)

        if resourceform.is_valid() and locationform.is_valid() and eventform.is_valid():
            acct = get_account(request.user.id)

            # REPLACE FROM HERE
            new_loc = locationform.cleaned_data["new_location"]
            if new_loc:
                resource.add_location_from_name(locationform.cleaned_data["new_location"])
                resource.save(author=acct, reindex=True)
            else:
                event_start = eventform.cleaned_data["start"]
                if event_start:
                    resource.calendar_event = CalendarEvent(start=event_start, end=eventform.cleaned_data["end"])
                    # print 'event_start', event_start
                    # print 'event_finish', eventform.cleaned_data['end']
                else:
                    resource.calendar_event = None
                resource = resourceform.save()

                try:
                    resource.save(author=acct, reindex=True)
                    return resource_edit_complete(request, resource, template_info)
                except OperationError:
                    pass

            # NEW SNEEU
            # # Location
            # new_loc = locationform.cleaned_data['new_location'].split(',')
            # print new_loc
            # resource.locations = Location.objects(id__in=new_loc)
            # #resource.add_location_from_name(locationform.cleaned_data['new_location'])
            # #resource.save(author=acct, reindex=True)

            # # Dates
            # event_start = eventform.cleaned_data['start']
            # if event_start:
            #     resource.calendar_event = CalendarEvent(start=event_start, end=eventform.cleaned_data['end'])
            #     # print 'event_start', event_start
            #     # print 'event_finish', eventform.cleaned_data['end']
            # else:
            #     resource.calendar_event = None
            # resource = resourceform.save()

            # try:
            #     resource.save(author=acct, reindex=True)
            #     return resource_edit_complete(request, resource, template_info)
            # except OperationError:
            #     pass

    else:
        resourceform = ShortResourceForm(instance=resource)
        locationform = LocationUpdateForm(instance=resource)
        eventform = EventForm(instance=resource.calendar_event)
        # if not resource.locations:
        #     doc = resource.uri
        # shelflifeform = ShelflifeForm(instance=resource)

    return render_to_response(
        template,
        RequestContext(
            request,
            {
                "template_info": template_info,
                "object": resource,
                "resourceform": resourceform,
                "locationform": locationform,
                "eventform": eventform,  #'places': places,
                # 'tagsform': tagsform, #'shelflifeform': shelflifeform,
                "UPDATE_LOCS": UPDATE_LOCS,
                "UPDATE_TAGS": UPDATE_TAGS,
            },
        ),
    )
Beispiel #6
0
def resource_edit(request, object_id, template='depot/resource_edit.html'):
    """ edits an existing resource. Uses a wizard-like approach, so checks resource.collection_status
        and hands off to resource_* function handler
    """
    UPDATE_LOCS = 'Update locations'
    UPDATE_TAGS = 'Update tags'

    resource = get_one_or_404(id=ObjectId(object_id),
                              user=request.user,
                              perm='can_edit')

    # doc = ''
    # places = None
    template_info = _template_info(request.REQUEST.get('popup', ''))

    if request.method == 'POST':
        result = request.POST.get('result',
                                  '')  # or request.POST.get('result', '')
        if result == 'Cancel':
            return resource_edit_complete(request, resource, template_info)
        resourceform = ShortResourceForm(request.POST, instance=resource)
        eventform = EventForm(request.POST, instance=resource.calendar_event)
        locationform = LocationUpdateForm(request.POST, instance=resource)
        # shelflifeform = ShelflifeForm(request.POST, instance=resource)

        if resourceform.is_valid() and locationform.is_valid(
        ) and eventform.is_valid():
            acct = get_account(request.user.id)

            # Location
            new_loc = locationform.cleaned_data['new_location'].split(',')
            print new_loc
            resource.locations = Location.objects(id__in=new_loc)
            #resource.add_location_from_name(locationform.cleaned_data['new_location'])
            #resource.save(author=acct, reindex=True)

            # Dates
            event_start = eventform.cleaned_data['start']
            if event_start:
                resource.calendar_event = CalendarEvent(
                    start=event_start, end=eventform.cleaned_data['end'])
                # print 'event_start', event_start
                # print 'event_finish', eventform.cleaned_data['end']
            else:
                resource.calendar_event = None
            resource = resourceform.save()

            try:
                resource.save(author=acct, reindex=True)
                return resource_edit_complete(request, resource, template_info)
            except OperationError:
                pass

    else:
        resourceform = ShortResourceForm(instance=resource)
        locationform = LocationUpdateForm(instance=resource)
        eventform = EventForm(instance=resource.calendar_event)
        # if not resource.locations:
        #     doc = resource.uri
        # shelflifeform = ShelflifeForm(instance=resource)

    return render_to_response(
        template,
        RequestContext(
            request,
            {
                'template_info': template_info,
                'object': resource,
                'resourceform': resourceform,
                'locationform': locationform,
                'eventform': eventform,  #'places': places,
                # 'tagsform': tagsform, #'shelflifeform': shelflifeform,
                'UPDATE_LOCS': UPDATE_LOCS,
                'UPDATE_TAGS': UPDATE_TAGS
            }))
Beispiel #7
0
def resource_edit(request, object_id, template="depot/resource_edit.html"):
    """ edits an existing resource. Uses a wizard-like approach, so checks resource.collection_status
        and hands off to resource_* function handler
    """
    UPDATE_LOCS = "Update locations"
    UPDATE_TAGS = "Update tags"

    resource = get_one_or_404(id=ObjectId(object_id), user=request.user, perm="can_edit")

    doc = ""
    places = None
    template_info = _template_info(request.REQUEST.get("popup", ""))

    if request.method == "POST":
        result = request.POST.get("result", "")  # or request.POST.get('result', '')
        if result == "Cancel":
            return resource_edit_complete(request, resource, template_info)
        resourceform = ShortResourceForm(request.POST, instance=resource)
        locationform = LocationUpdateForm(request.POST, instance=resource)
        # tagsform = TagsForm(request.POST, instance=resource)
        # shelflifeform = ShelflifeForm(request.POST, instance=resource)

        if resourceform.is_valid() and locationform.is_valid():  # and tagsform.is_valid() and shelflifeform.is_valid():
            # del_loc = ''
            # for k in request.POST.keys():
            #     if k.startswith('del_loc:'):
            #         del_loc = k.split(':')[1]
            #         break
            acct = get_account(request.user.id)
            new_loc = locationform.cleaned_data["new_location"]
            if new_loc:
                resource.add_location_from_name(locationform.cleaned_data["new_location"])
                resource.save(author=acct, reindex=True)
            # elif del_loc:
            #     # print [l.id for l in resource.locations]
            #     # print Location.objects.get(id=ObjectId(del_loc)).id
            #
            #     # list.remove doesn't seem to work
            #     resource.locations = [loc for loc in resource.locations if str(loc.id) != del_loc]
            #     resource.save(author=get_account(request.user.id))
            #     # print resource.locations
            #     # places = fix_places(resource.locations, locationform.content() or resource.url)
            else:
                resource = resourceform.save()

                # read location checkboxes
                # cb_places = request.POST.getlist('cb_places')
                # locations = []
                # for loc in cb_places:
                #     locations.append(location_from_cb_value(loc).woeid)
                # if len(locations) > 0:
                # resource.locations = locations

                # resource = tagsform.save()
                # resource = shelflifeform.save()

                try:
                    resource.save(author=acct, reindex=True)
                    # resource.reindex()
                    # try:
                    #     keys = get_terms(resource.url)
                    # except:
                    #     keys = [] # need to fail silently here
                    # resource.make_keys(keys)
                    return resource_edit_complete(request, resource, template_info)
                    # return HttpResponseRedirect('%s?popup=%s' % (reverse('resource', args=[resource.id]), template_info['popup']))
                except OperationError:
                    pass

    else:
        resourceform = ShortResourceForm(instance=resource)
        locationform = LocationUpdateForm(instance=resource)
        if not resource.locations:
            doc = resource.uri
        # places = fix_places(resource.locations, doc)
        # tagsform = TagsForm(instance=resource)
        shelflifeform = ShelflifeForm(instance=resource)

    return render_to_response(
        template,
        RequestContext(
            request,
            {
                "template_info": template_info,
                "object": resource,
                "resourceform": resourceform,
                "locationform": locationform,  #'places': places,
                # 'tagsform': tagsform, #'shelflifeform': shelflifeform,
                "UPDATE_LOCS": UPDATE_LOCS,
                "UPDATE_TAGS": UPDATE_TAGS,
            },
        ),
    )