def curation_edit(request, object_id, index, template_name='depot/curation_edit.html'): """Curation is an EmbeddedDocument, so can't be saved, needs to be edited, then Resource saved.""" resource = get_one_or_404(id=ObjectId(object_id), user=request.user, perm='can_edit') object = resource.curations[int(index)] if request.method == 'POST': result = request.POST.get('result', '') if result == 'Cancel': return HttpResponseRedirect( reverse('curation', args=[resource.id, index])) form = CurationForm(request.POST, instance=object) if form.is_valid(): user = get_account(request.user.id) curation = form.save(do_save=False) curation.item_metadata.update(author=user) curation.save() resource.save(reindex=True) return HttpResponseRedirect( reverse('curation', args=[resource.id, index])) else: form = CurationForm(instance=object) template_context = {'form': form} return render_to_response(template_name, template_context, RequestContext(request))
def curation_add(request, object_id, template_name="depot/curation_edit.html"): """docstring for curation_add""" resource = get_one_or_404(id=ObjectId(object_id)) user = get_account(request.user.id) curation = get_curation_for_user_resource(user, resource) if curation: index, cur = curation messages.success(request, "You already have a curation for this resource.") return HttpResponseRedirect(reverse("curation", args=[resource.id, index])) if request.method == "POST": result = request.POST.get("result", "") if result == "Cancel": return HttpResponseRedirect(reverse("resource", args=[resource.id])) form = CurationForm(request.POST) if form.is_valid(): curation = Curation(**form.cleaned_data) curation.owner = user curation.item_metadata.update(author=user) curation.resource = resource curation.save() resource.curations.append(curation) resource.save(reindex=True) index = len(resource.curations) - 1 return HttpResponseRedirect(reverse("curation", args=[resource.id, index])) else: initial = {"outcome": STATUS_OK} form = CurationForm(initial=initial) template_context = {"form": form} return render_to_response(template_name, template_context, RequestContext(request))
def curation_edit(request, object_id, index, template_name='depot/curation_edit.html'): """Curation is an EmbeddedDocument, so can't be saved, needs to be edited, then Resource saved.""" resource = get_one_or_404(id=ObjectId(object_id), user=request.user, perm='can_edit') object = resource.curations[int(index)] if request.method == 'POST': result = request.POST.get('result', '') if result == 'Cancel': return HttpResponseRedirect(reverse('curation', args=[resource.id, index])) form = CurationForm(request.POST, instance=object) if form.is_valid(): user = get_account(request.user.id) curation = form.save(do_save=False) curation.item_metadata.update(author=user) curation.save() resource.save(reindex=True) return HttpResponseRedirect(reverse('curation', args=[resource.id, index])) else: form = CurationForm(instance=object) template_context = {'form': form} return render_to_response( template_name, template_context, RequestContext(request) )
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_find(request, template='depot/resource_find.html'): """docstring for resource_find""" results = locations = [] centre = None pins = [] # places = [] result = request.REQUEST.get('result', '') if request.method == 'POST' or result: # result = request.REQUEST.get('result', '') if result == 'Cancel': return HttpResponseRedirect(reverse('resource-list')) form = FindResourceForm(request.REQUEST) if form.is_valid(): user = get_account(request.user.id) for result in form.results: resource = get_one_or_404(id=ObjectId(result['res_id'])) try: curation_index, curation = get_curation_for_user_resource( user, resource) except TypeError: curation_index = curation = None curation_form = CurationForm(initial={'outcome': STATUS_OK}, instance=curation) results.append({ 'resource_result': result, 'curation': curation, 'curation_form': curation_form, 'curation_index': curation_index }) locations = form.locations centre = form.centre # pins = [loc['obj'] for loc in locations] else: form = FindResourceForm( initial={ 'post_code': 'aberdeen', 'boost_location': settings.SOLR_LOC_BOOST_DEFAULT }) # print places context = { 'form': form, 'results': results, 'locations': locations, 'centre': centre, 'pins': pins, 'yahoo_appid': settings.YAHOO_KEY, 'google_key': settings.GOOGLE_KEY, } return render_to_response(template, RequestContext(request, context))
def resource_find(request, template="depot/resource_find.html"): """docstring for resource_find""" results = locations = [] centre = None pins = [] # places = [] result = request.REQUEST.get("result", "") if request.method == "POST" or result: # result = request.REQUEST.get('result', '') if result == "Cancel": return HttpResponseRedirect(reverse("resource-list")) form = FindResourceForm(request.REQUEST) if form.is_valid(): user = get_account(request.user.id) for result in form.results: resource = get_one_or_404(id=ObjectId(result["res_id"])) try: curation_index, curation = get_curation_for_user_resource(user, resource) except TypeError: curation_index = curation = None curation_form = CurationForm(initial={"outcome": STATUS_OK}, instance=curation) results.append( { "resource_result": result, "curation": curation, "curation_form": curation_form, "curation_index": curation_index, } ) locations = form.locations centre = form.centre # pins = [loc['obj'] for loc in locations] else: form = FindResourceForm(initial={"post_code": "aberdeen", "boost_location": settings.SOLR_LOC_BOOST_DEFAULT}) context = { "form": form, "results": results, "locations": locations, "centre": centre, "pins": pins, "yahoo_appid": settings.YAHOO_KEY, "google_key": settings.GOOGLE_KEY, } return render_to_response(template, RequestContext(request, context))
def resource_find(request, template='depot/resource_find.html'): """docstring for resource_find""" results = locations = [] centre = None pins = [] # places = [] result = request.REQUEST.get('result', '') if request.method == 'POST' or result: # result = request.REQUEST.get('result', '') if result == 'Cancel': return HttpResponseRedirect(reverse('resource-list')) form = FindResourceForm(request.REQUEST) if form.is_valid(): user = get_account(request.user.id) for result in form.results: resource = get_one_or_404(id=ObjectId(result['res_id'])) try: curation_index, curation = get_curation_for_user_resource(user, resource) except TypeError: curation_index = curation = None curation_form = CurationForm( initial={'outcome': STATUS_OK}, instance=curation) results.append({ 'resource_result': result, 'curation': curation, 'curation_form': curation_form, 'curation_index': curation_index }) locations = form.locations centre = form.centre # pins = [loc['obj'] for loc in locations] else: form = FindResourceForm(initial={'post_code': 'aberdeen', 'boost_location': settings.SOLR_LOC_BOOST_DEFAULT}) # print places context = { 'form': form, 'results': results, 'locations': locations, 'centre': centre, 'pins': pins, 'yahoo_appid': settings.YAHOO_KEY, 'google_key': settings.GOOGLE_KEY, } return render_to_response(template, RequestContext(request, context))
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 }))
def curation_add(request, object_id, template_name='depot/curation_edit.html'): """docstring for curation_add""" resource = get_one_or_404(id=ObjectId(object_id)) user = get_account(request.user.id) # check if user already has a curation for this resource curation = get_curation_for_user_resource(user, resource) if curation: index, cur = curation messages.success(request, 'You already have a curation for this resource.') return HttpResponseRedirect( reverse('curation', args=[resource.id, index])) if request.method == 'POST': result = request.POST.get('result', '') if result == 'Cancel': return HttpResponseRedirect(reverse('resource', args=[resource.id])) form = CurationForm(request.POST) if form.is_valid(): curation = Curation(**form.cleaned_data) curation.owner = user curation.item_metadata.update(author=user) curation.resource = resource curation.save() resource.curations.append(curation) resource.save(reindex=True) index = len(resource.curations) - 1 return HttpResponseRedirect( reverse('curation', args=[resource.id, index])) else: initial = {'outcome': STATUS_OK} form = CurationForm(initial=initial) template_context = {'form': form} return render_to_response(template_name, template_context, RequestContext(request))
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 }))
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}))
def curation_add(request, object_id, template_name='depot/curation_edit.html'): """docstring for curation_add""" resource = get_one_or_404(id=ObjectId(object_id)) user = get_account(request.user.id) # check if user already has a curation for this resource for index, cur in enumerate(resource.curations): if cur.owner.id == user.id: messages.success(request, 'You already have a curation for this resource.') return HttpResponseRedirect(reverse('curation', args=[resource.id, index])) if request.method == 'POST': result = request.POST.get('result', '') if result == 'Cancel': return HttpResponseRedirect(reverse('resource', args=[resource.id])) form = CurationForm(request.POST) if form.is_valid(): curation = Curation(**form.cleaned_data) curation.owner = user curation.item_metadata.update(author=user) curation.resource = resource curation.save() resource.curations.append(curation) resource.save(reindex=True) index = len(resource.curations) - 1 return HttpResponseRedirect(reverse('curation', args=[resource.id, index])) else: initial = { 'outcome': STATUS_OK} form = CurationForm(initial=initial) template_context = {'form': form} return render_to_response( template_name, template_context, RequestContext(request) )
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, }, ), )
def location_remove(request, object_id, index): """docstring for location_remove""" resource = get_one_or_404(id=object_id, user=request.user, perm='can_edit') del resource.locations[int(index)] resource.save(author=get_account(request.user.id), reindex=True) return HttpResponseRedirect(reverse('resource-edit', args=[resource.id]))
def perm_can_delete(self, user): """docstring for perm_can_edit""" acct = get_account(user.id) # print self.owner, acct return self.owner == acct
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) 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, }, ), )