def edit_item(request, item_id):
	# get user from shard
	user = get_user_from_cache(request.user.id)
	# get item from shard
	#item = get_object_or_404(Item, item_id=item_id)
	# item_query = Item.objects
	# set_user_for_sharding(item_query, request.user.id)
	# item = item_query.get(item_id=item_id)
	item = get_item_from_cache(item_id, request.user.id) # pass in the user_id as a backup to get the item from the db if it does not exist

	if item is None: # this means the cache doesn't have it and (if we passed in the user_id) the db doesn't have it
		return flashHomeMessage(request, 'Sorry, we could\'t find an item by that specification')

	if item.owner != user:
		return flashHomeMessage(request, 'This isn\'t your item')

	if request.method == 'POST':
		form = ItemForm(request.POST, instance=item)

		if form.is_valid():
			new_item = form.save(commit=False)
			new_item.save()
			# add it to the cache
			# cache = caches['items']
			# single_item_cache_key = 'qr_item_' + str(item_id) # have single item cache as well so that
			# cache.set(single_item_cache_key, new_item)
			return HttpResponseRedirect(reverse('my_qrcode:profile', args=()))
	else:
		form = ItemForm(instance=item, initial={'mark_lost': (item.status == Item.ITEM_LOST),})

	context = {
		'user': user,
		'form': form,
		'item_id': item_id,
	}
	return render(request, 'my_qrcode/item_edit.html', context)
def add_item(request):
	user = get_user_from_cache(request.user.id)

	if request.method == 'POST':
		form = ItemForm(request.POST)

		if form.is_valid():
			new_item = form.save(commit=False)
			new_item.owner = user
			new_item.status = Item.ITEM_NOT_LOST

			# set item id
			item_id = generate_item_id(user.user_id)

			while len(Item.objects.filter(item_id=item_id)):
				item_id = generate_item_id(user.user_id)

			new_item.item_id = item_id

			new_item.save()

			# add it to the cache
			# cache = caches['items']
			# single_item_cache_key = 'qr_item_' + str(item_id) # have single item cache as well so that
			# cache.set(single_item_cache_key, new_item)

			return HttpResponseRedirect(reverse('my_qrcode:profile', args=()))
	else:
		form = ItemForm()
		form.fields['mark_lost'].widget = forms.HiddenInput() # hide mark_lost for adding

	context = {
		'user': user,
		'form': form,
	}
	return render(request, 'my_qrcode/item_add.html', context)
Example #3
0
def edit_item(request, item_id):
    # get user from shard
    user = get_user_from_cache(request.user.id)
    # get item from shard
    #item = get_object_or_404(Item, item_id=item_id)
    # item_query = Item.objects
    # set_user_for_sharding(item_query, request.user.id)
    # item = item_query.get(item_id=item_id)
    item = get_item_from_cache(
        item_id, request.user.id
    )  # pass in the user_id as a backup to get the item from the db if it does not exist

    if item is None:  # this means the cache doesn't have it and (if we passed in the user_id) the db doesn't have it
        return flashHomeMessage(
            request, 'Sorry, we could\'t find an item by that specification')

    if item.owner != user:
        return flashHomeMessage(request, 'This isn\'t your item')

    if request.method == 'POST':
        form = ItemForm(request.POST, instance=item)

        if form.is_valid():
            new_item = form.save(commit=False)
            new_item.save()
            # add it to the cache
            # cache = caches['items']
            # single_item_cache_key = 'qr_item_' + str(item_id) # have single item cache as well so that
            # cache.set(single_item_cache_key, new_item)
            return HttpResponseRedirect(reverse('my_qrcode:profile', args=()))
    else:
        form = ItemForm(instance=item,
                        initial={
                            'mark_lost': (item.status == Item.ITEM_LOST),
                        })

    context = {
        'user': user,
        'form': form,
        'item_id': item_id,
    }
    return render(request, 'my_qrcode/item_edit.html', context)
Example #4
0
def add_item(request):
    user = get_user_from_cache(request.user.id)

    if request.method == 'POST':
        form = ItemForm(request.POST)

        if form.is_valid():
            new_item = form.save(commit=False)
            new_item.owner = user
            new_item.status = Item.ITEM_NOT_LOST

            # set item id
            item_id = generate_item_id(user.user_id)

            while len(Item.objects.filter(item_id=item_id)):
                item_id = generate_item_id(user.user_id)

            new_item.item_id = item_id

            new_item.save()

            # add it to the cache
            # cache = caches['items']
            # single_item_cache_key = 'qr_item_' + str(item_id) # have single item cache as well so that
            # cache.set(single_item_cache_key, new_item)

            return HttpResponseRedirect(reverse('my_qrcode:profile', args=()))
    else:
        form = ItemForm()
        form.fields['mark_lost'].widget = forms.HiddenInput(
        )  # hide mark_lost for adding

    context = {
        'user': user,
        'form': form,
    }
    return render(request, 'my_qrcode/item_add.html', context)