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 }))
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, }, ), )
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 }))
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, }, ), )