summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSmári McCarthy <smari@fabfolk.com>2009-10-20 19:35:13 +0000
committerSmári McCarthy <smari@fabfolk.com>2009-10-20 19:35:13 +0000
commit21d8bd3e42be360c60a6e607c7cd1a41e61da3d0 (patch)
tree8530d0df5a09e5e6e17b8ea4bac63fc9d3443155
parent51e740ac391fdc97135e3fa3195375f661fc030d (diff)
downloadtangiblebit-21d8bd3e42be360c60a6e607c7cd1a41e61da3d0.tar.gz
tangiblebit-21d8bd3e42be360c60a6e607c7cd1a41e61da3d0.zip
EAN lookup and locations over JSON.
-rw-r--r--server/locations/views.py27
-rw-r--r--server/locations/xmlrpc.py9
-rw-r--r--server/objects/models.py8
-rw-r--r--server/objects/views.py19
-rw-r--r--server/urls.py1
5 files changed, 60 insertions, 4 deletions
diff --git a/server/locations/views.py b/server/locations/views.py
index 7f5e730..f509c7a 100644
--- a/server/locations/views.py
+++ b/server/locations/views.py
@@ -4,6 +4,7 @@ from locations.models import *
from locations.xmlrpc import *
from django.http import HttpResponse
from django.contrib.auth.models import User
+from django.db.models import Q
import simplejson
import time
@@ -22,10 +23,30 @@ def search(request):
query = request.GET['q']
if not query or query == "":
- return HttpResonse(simplejson.encode(GetLocationList()))
+ return HttpResponse(simplejson.encode(GetLocationList()))
else:
- # FIXME: This isn't actually a real search function at the moment.
- return HttpResonse(simplejson.encode(GetLocationList()))
+ q = Q()
+ for qs in query.split(" "):
+ q &= Q(name__contains=qs) | Q(locname__contains=qs)
+
+ try:
+ locations = Location.objects.get(q)
+ except:
+ return HttpResponse("[]")
+
+ results = []
+
+ for location in locations:
+ results.append({
+ 'id': location.id,
+ 'lat': location.lat,
+ 'lon': location.lon,
+ 'name': location.name,
+ 'city': location.locname,
+ 'website': location.website
+ })
+
+ return HttpResponse(simplejson.encode(results))
def setlocation(request):
if not request.has_key('locationid'):
diff --git a/server/locations/xmlrpc.py b/server/locations/xmlrpc.py
index c2ec2f2..5d22bdf 100644
--- a/server/locations/xmlrpc.py
+++ b/server/locations/xmlrpc.py
@@ -12,7 +12,14 @@ def GetLocationList():
results = []
for location in locations:
- results.append({ 'id': location.id, 'lat': location.lat, 'lon': location.lon, 'name': location.name, 'locname': location.locname, 'weblocation': location.website})
+ results.append({
+ 'id': location.id,
+ 'lat': location.lat,
+ 'lon': location.lon,
+ 'name': location.name,
+ 'locname': location.locname,
+ 'website': location.website
+ })
return results
diff --git a/server/objects/models.py b/server/objects/models.py
index fdb0062..a9471a3 100644
--- a/server/objects/models.py
+++ b/server/objects/models.py
@@ -25,6 +25,13 @@ class Object(models.Model):
files = models.ManyToManyField(ObjectFile)
+class Product(models.Model):
+ # To map product codes (EAN, UPC,...) to objects.
+ code = models.CharField(max_length=18, primary_key=True)
+ name = models.CharField(max_length=200)
+ object = models.ForeignKey(Object, blank=True, null=True)
+
+
class Intangible(models.Model):
# TODO: Define me!
# Heat, pressure, electricity, manpower,...
@@ -40,4 +47,5 @@ class ObjectSearchForm(ModelForm):
model = Object
+
admin.site.register(Object)
diff --git a/server/objects/views.py b/server/objects/views.py
index 524ba08..cdf45d3 100644
--- a/server/objects/views.py
+++ b/server/objects/views.py
@@ -1,8 +1,27 @@
+from django.http import HttpResponse
from django.shortcuts import render_to_response
from objects.models import *
+import simplejson
def index(request):
objsearchform = ObjectSearchForm(auto_id="catalogsearch")
return render_to_response("objects/index.html", {"catalogsearch": objsearchform, "user": request.user})
+def ean_lookup(request):
+ query = request.GET["q"].split(",")
+
+ try:
+ results = Product.objects.filter(code__in=query)
+ products = []
+
+ for r in results:
+ products.append({
+ 'code': r.code,
+ 'name': r.name
+ })
+
+ return HttpResponse(simplejson.encode(products))
+ except Exception as e:
+ print e
+ return HttpResponse("[]")
diff --git a/server/urls.py b/server/urls.py
index 375b0fa..ab694ab 100644
--- a/server/urls.py
+++ b/server/urls.py
@@ -32,6 +32,7 @@ urlpatterns = patterns('',
(r'^materials/tradenames$', 'materials.views.manufacturers'),
(r'^objects/$', 'objects.views.index'),
+ (r'^objects/ean_lookup$', 'objects.views.ean_lookup'),
(r'^xml/location', 'locations.views.locationtoxml'),
(r'^yaml/location', 'locations.views.locationtoyaml'),