Commit 3efa66b4 authored by Christos Stavrakakis's avatar Christos Stavrakakis
Browse files

cyclades: Common filtering based on changes-since

Add 'filter_modified_since' function in snf_django api utils, for
parsing 'changes-since' HTTP parameter and filter the DB objects that
are updated after that date. Otherwise the deleted objects are returned.
parent ba922017
......@@ -92,8 +92,8 @@ def list_floating_ips(request):
userid = request.user_uniq
floating_ips = FloatingIP.objects.filter(userid=userid, deleted=False)\
floating_ips = FloatingIP.objects.filter(userid=userid).order_by("id")
floating_ips = utils.filter_modified_since(request, objects=floating_ips)
floating_ips = map(ip_to_dict, floating_ips)
......@@ -231,8 +231,8 @@ def release_floating_ip(request, floating_ip_id):
@api.api_method(http_method='GET', user_required=True, logger=log,
def list_floating_ip_pools(request):
networks = Network.objects.filter(public=True, deleted=False,
networks = Network.objects.filter(public=True, floating_ip_pool=True)
networks = utils.filter_modified_since(request, objects=networks)
pools = [{"name": str(} for net in networks]
request.serialization = "json"
data = json.dumps({"floating_ip_pools": pools})
......@@ -120,16 +120,9 @@ def list_networks(request, detail=False):
# overLimit (413)
log.debug('list_networks detail=%s', detail)
since = utils.isoparse(request.GET.get('changes-since'))
user_networks = Network.objects.filter(Q(userid=request.user_uniq) |
if since:
user_networks = user_networks.filter(updated__gte=since)
if not user_networks:
return HttpResponse(status=304)
user_networks = user_networks.filter(deleted=False)
user_networks = utils.filter_modified_since(request, objects=user_networks)
networks_dict = [network_to_dict(network, request.user_uniq, detail)
for network in user_networks.order_by('id')]
......@@ -242,14 +242,7 @@ def list_servers(request, detail=False):
log.debug('list_servers detail=%s', detail)
user_vms = VirtualMachine.objects.filter(userid=request.user_uniq)
since = utils.isoparse(request.GET.get('changes-since'))
if since:
user_vms = user_vms.filter(updated__gte=since)
if not user_vms:
return HttpResponse(status=304)
user_vms = user_vms.filter(deleted=False)
user_vms = utils.filter_modified_since(request, objects=user_vms)
servers_dict = [vm_to_dict(server, detail)
for server in user_vms.order_by('id')]
......@@ -34,6 +34,7 @@
import datetime
from dateutil.parser import parse as date_parse
from django.utils import simplejson as json
from django.http import HttpResponse
from django.conf import settings
from snf_django.lib.api import faults
......@@ -115,3 +116,21 @@ def prefix_pattern(prefix):
prefix += '/'
pattern = '^' + prefix
return pattern
def filter_modified_since(request, objects):
"""Filter DB objects based on 'changes-since' request parameter.
Parse request for 'changes-since' parameter and get only the DB objects
that have been updated after that time. Otherwise, return the non-deleted
since = isoparse(request.GET.get("changes-since"))
if since:
modified_objs = objects.filter(updated__gte=since)
if not modified_objs:
return HttpResponse(status=304)
return modified_objs
return objects.filter(deleted=False)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment