def update_library(request): user = CoreUser.objects.get(username=request.user) if 'id' not in request.POST: raise HttpResponseBadRequest('id is a required parameter') else: id = request.POST['id'].strip() library = LinkLibrary.objects.get(pk=id) if not library: raise Http404 links = '' if 'links' in request.POST: links = request.POST['links'].strip() linkArray = links.split(',') library.links.clear() for link_object in linkArray: link_object = link_object.strip() if link_object.isdigit(): link = Link.objects.get(pk=int(link_object)) library.links.add(link) if 'name' not in request.POST: return HttpResponseBadRequest('name is a required parameter') name = request.POST['name'].strip() library.name = name if 'desc' in request.POST: desc = request.POST['desc'].strip() library.desc = desc tags = '' if 'tags' in request.POST: tags = request.POST['tags'].strip() tags = tags.split(',') library.tags.clear() for t in tags: t = t.strip() if len(t) > 0: retrievedtag = Tag.objects.get_or_create(name=t) library.tags.add(retrievedtag[0]) # if tags[-1] == ',': # length_of_tags = len(tags) # tags = tags[0:length_of_tags-1] library.save() # The below line shouldn't be needed because the library should # stay in the profile of the user even if it was updated. # user.libraries.add(library) if utils.accepts_json(request): jsonContent = utils.get_linklibrary_json(library) return HttpResponse(content=jsonContent, content_type=utils.JSON_CONTENT_TYPE) return HttpResponse(str(library.pk))
def create_library(request): """ This view when called will create a link library. It won't work properly unless you are already logged in to the webapp in a legitimate way. Parameters: ``links`` - a comma-delimited list of the primary keys of the links you want to add to the created link library. They are passed in from request object via POST. ``name`` - the name you wish to call the created link library. Passed in from the request object via POST. ``desc`` - The description you want to use for the link library. ``tags`` - Another comma-delimited list of the names of the tags you want to associate with the link library you are creating. If the tags are not found within the Tag table, they will be created. Returns: This view should return the same page that called it, which is testgrid. We may need to modify this when it is more smoothly integrated into our existing webapp. """ user = CoreUser.objects.get(username=request.user) if not user: logging.error('No user retrieved by the username of %s' % request.user) return HttpResponseBadRequest('No user identified in request.') if request.method == 'POST': links = '' if 'links' in request.POST: links = request.POST['links'].strip() if 'name' not in request.POST: return HttpResponseBadRequest('name is a required parameter') name = request.POST['name'].strip() desc = '' if 'desc' in request.POST: desc = request.POST['desc'].strip() tags = '' if 'tags' in request.POST: tags = request.POST['tags'].strip() # if tags[-1] == ',': # length_of_tags = len(tags) # tags = tags[0:length_of_tags-1] linkArray = links.split(',') tags = tags.split(',') library = LinkLibrary(name=name, desc=desc, creator=user) library.save() for t in tags: t = t.strip() if len(t) > 0: retrievedtag = Tag.objects.get_or_create(name=t) library.tags.add(retrievedtag[0]) for link_object in linkArray: link_object = link_object.strip() if link_object.isdigit(): link = Link.objects.get(pk=int(link_object)) library.links.add(link) library.save() user.libraries.add(library) if utils.accepts_json(request): jsonContent = utils.get_linklibrary_json(library) return HttpResponse(content=jsonContent, content_type=utils.JSON_CONTENT_TYPE) return HttpResponse(str(library.pk)) else: allLinks = Link.objects.all() allTags = Tag.objects.all() return render_to_response('createlib.html', { 'allLinks' : allLinks, 'allTags': allTags }, context_instance=RequestContext(request))