From 17cdf2942cff01828e0f26ad90e288e9c277f319 Mon Sep 17 00:00:00 2001 From: Sanj Date: Sat, 26 May 2012 02:01:04 +0530 Subject: [PATCH] add is_empty option --- manifests/settings.py | 2 +- manifests/ships/convert.py | 43 ++++++++++++++++++++++--------- manifests/ships/models.py | 4 +++ manifests/ships/views.py | 6 ++++- manifests/templates/shipList.html | 5 ++++ 5 files changed, 46 insertions(+), 14 deletions(-) diff --git a/manifests/settings.py b/manifests/settings.py index 2fab57d..e64c013 100644 --- a/manifests/settings.py +++ b/manifests/settings.py @@ -2,7 +2,7 @@ import os from os.path import join -DEBUG = True +DEBUG = False TEMPLATE_DEBUG = DEBUG JSON_DEBUG = DEBUG LOCAL_DEVELOPMENT = True diff --git a/manifests/ships/convert.py b/manifests/ships/convert.py index 17168e9..82760df 100755 --- a/manifests/ships/convert.py +++ b/manifests/ships/convert.py @@ -120,6 +120,13 @@ def addType(inFile, outFile): outFile.write(outJSON) outFile.close() +def cleanDates(filename): + data = json.load(open(filename)) + for d in data: + d['filename'] = d['filename'].replace("csv/02042012/Outgoing/", "") + #date = d['filename'].replace("creekmanifest", "") + + #prefix is a silly quick-fix for date like creekmanifest1201 def importJSON(filename, prefix=""): data = json.loads(open(filename).read()) @@ -131,34 +138,46 @@ def importJSON(filename, prefix=""): m.filename = manifest['filename'] if prefix != '': manifest['date'] = manifest['date'].replace(prefix, "") - month = manifest['date'][0:3] - yr = manifest['date'][3:5] - m.month = month + dateStr = manifest['date'].replace(".csv", "") + yr = dateStr[-2:] + month = dateStr.replace(yr, "") + print yr + print month +# month = manifest['date'][0:3] +# yr = manifest['date'][3:5] + m.month = month[0:3].lower() + #print yr m.year = int("20" + yr) + matchedManifests = Manifest.objects.filter(month=month).filter(year=m.year) + if matchedManifests.count() > 0: + print "Manifest file exists with id %d" % matchedManifests[0].id + for match in matchedManifests: + match.delete() # datestring = "20%s-%s" % (yr, month,) # m.date = datestring try: m.save() except: manifests_errors.append(manifest['filename']) + exit() for ship in manifest['ships']: s = Ship() row = ship['row'] s.manifest_file = m - s.bill_type = row[0] - s.number = row[1] - shipDate = row[2] + s.bill_type = 'Export' + s.number = row[0] + shipDate = row[1] s.date = "%s-%s-%s" % (shipDate[6:], shipDate[3:5], shipDate[0:2]) - s.ship_name = row[3] - s.captain = row[4] - s.flag = row[5] - s.owner = row[6] + s.ship_name = row[2] + s.captain = row[3] + s.flag = row[4] + s.owner = row[5] try: - s.port = row[7] + s.port = row[6] except: s.port = '' try: - s.country = row[8] + s.country = row[7] except: s.country = '' try: diff --git a/manifests/ships/models.py b/manifests/ships/models.py index 74554d8..c9100ce 100644 --- a/manifests/ships/models.py +++ b/manifests/ships/models.py @@ -110,4 +110,8 @@ class Translation(models.Model): class Meta: ordering = ('id',) +''' +class NewTranslation(Translation): + pass +''' databrowse.site.register(Ship, Good, Manifest) diff --git a/manifests/ships/views.py b/manifests/ships/views.py index 9d5f629..24604e3 100644 --- a/manifests/ships/views.py +++ b/manifests/ships/views.py @@ -4,7 +4,7 @@ from django.http import HttpResponse from ships import models import json from django.shortcuts import render_to_response -from django.db.models import Q +from django.db.models import Q, Count from django.template import RequestContext from ox.django.shortcuts import render_to_json_response from django.views.decorators.csrf import csrf_exempt @@ -180,6 +180,7 @@ def shipList(request): port = request.GET.get('port', '') goods = request.GET.get('goods', '') sort = request.GET.get('sort', 'date') + is_empty = request.GET.get('is_empty', False) # country = request.GET.get('country', None) qset = Ship.objects.all() if page == '': @@ -207,6 +208,9 @@ def shipList(request): qset = qset.filter(port__icontains=port) if goods != '': qset = qset.filter(good__description_string_trans__icontains=goods).distinct() + if is_empty != False: + qset = qset.annotate(num_goods=Count('good')).filter(num_goods=0) + # if country: # qset = qset.filter(country__icontains=country) qset = qset.order_by(sort) diff --git a/manifests/templates/shipList.html b/manifests/templates/shipList.html index d74fd09..cef2a19 100644 --- a/manifests/templates/shipList.html +++ b/manifests/templates/shipList.html @@ -90,6 +90,11 @@ No of results: {{ no_of_results }}   Displaying page {{ page }} of {{ no_of + + +
+ +