TheLegace

Untitled

Apr 20th, 2013
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.79 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2.  
  3. db = DAL('sqlite://storage.sqlite',pool_size=1,check_reserved=['all'])
  4.  
  5. def make_thumbnail(table, image_id, size=(150, 150)):
  6.     import os
  7.     from PIL import Image
  8.     this_image = table(image_id)
  9.     im = Image.open(os.path.join(request.folder), 'uploads',
  10.         this_image.image)
  11.     im.thumbnail(size, Image.ANTIALIAS)
  12.     thumbnail = 'document.thumbnail.%s.png' % this_image.image.split('.')[2]
  13.     im.save(os.path.join(request.folder, 'uploads', thumbnail), 'png')
  14.     this_image.update_record(thumbnail=thumbnail)
  15.     return
  16.  
  17. def resize_image(image, size, path):
  18.     from PIL import Image
  19.     import os.path
  20.     try:
  21.         img = Image.open('%sstatic/uploads/%s' % (request.folder, image))
  22.         img.thumbnail(size, Image.NEAREST)
  23.         root, ext = os.path.splitext(image)
  24.         filename = '%s_%s%s' %(root, path, ext)
  25.         img.save('%sstatic/uploads/%s' % (request.folder, filename))
  26.     except Exception, e:
  27.         return e
  28.     else:
  29.         return filename
  30.  
  31. def render_image(ids,row):
  32.     span = SPAN()
  33.     for id in ids:
  34.         img = db.restaurant_images(id)
  35.         if img:
  36.             span.append(A(img.filename,_href=URL('download',args=img.image)))
  37.     return span  
  38.  
  39. from gluon.tools import Auth,Crud, Service, prettydate
  40. from gluon.contrib.login_methods.basic_auth import basic_auth
  41. from gluon.tools import geocode
  42. import datetime, os
  43. crud, service = Crud(db), Service(),
  44. auth = Auth(db)
  45. auth.define_tables(username=True)
  46. auth.settings.allow_basic_login = True
  47. auth.settings.hmac_key = 'sha512:a-pass-phrase'
  48. auth.settings.password_min_length = 4
  49. auth.settings.login_after_registration = True
  50. auth.settings.create_user_groups = False
  51. auth.settings.login_email_validate = False
  52.  
  53. auth.settings.login_methods = [basic_auth('http://127.0.0.1:8000')]
  54.  
  55.  
  56. db.define_table('restaurant_images',
  57.     Field('image',
  58.         'upload',
  59.         uploadfolder=os.path.join(request.folder,'static'),
  60.         uploadseparate=True
  61.         ),
  62.     # Field('image_thumb', 'upload', uploadfolder=request.folder+'static/uploads',
  63.  #            compute=lambda r: resize_image(r['image'], (150,130), 'thumb'))) 
  64.     #Field('thumb', writeable=False, readable=False)
  65.     Field('uploaded_by', db.auth_user),
  66.     format='%(id)s')
  67.  
  68. db.define_table('item',
  69.     Field('name'),
  70.     Field('description', 'text'),
  71.     Field('category'),
  72.     Field('price', 'double'),
  73.     Field('images', 'list:reference restaurant_images'),
  74.     format='%(name)s')
  75.  
  76. db.define_table('menu',
  77.     Field('menuitems', 'list:reference item'),
  78.     format='%(id)s')
  79.  
  80. db.define_table('restaurant_table',
  81.     Field('user_id', 'reference auth_user'),
  82.     Field('table_active', 'boolean', default=False),
  83.     Field('restaurant', 'reference restaurant'),
  84.     Field('capacity', 'integer'),
  85.     Field('booked_on', 'datetime'))
  86.    
  87. db.define_table('restaurant_order',
  88.     Field('user_id', 'reference auth_user'),
  89.     Field('item', 'reference item'),
  90.     Field('refill', 'boolean', default=False),
  91.     Field('quantity', 'integer', default=1),
  92.     Field('restaurant_table', 'reference restaurant_table'),
  93.     Field('ordered_on', 'datetime'))
  94.  
  95. db.define_table('bill',
  96.     Field('restaurant_table', 'reference restaurant_table'),
  97.     Field('restaurant_order', 'list:reference order'),
  98.     Field('billed_on', 'datetime'),
  99.     Field('total_bill', 'double'))
  100.  
  101. db.define_table('restaurant',
  102.     Field('name'),
  103.     Field('address'),
  104.     Field('latitude',
  105.             compute=lambda r: geocode(r.address)[0]),
  106.     Field('longitude',
  107.             compute=lambda r: geocode(r.address)[1]),
  108.     Field('phone'),
  109.     Field('url'),
  110.     Field('menu', 'reference menu'),
  111.     Field('images', 'list:reference restaurant_images'),
  112.     format='%(latitude; longitude)s')
  113.  
  114. #db.auth_user.password.requires = [IS_STRONG(), CRYPT()]
  115. #db.item.images.represent = lambda row:  URL('download', args=row)
  116. #db.restaurant.images.represent = lambda row:  URL('download', args=row)
  117.  
  118. session.connect(request, response, db)
Advertisement
Add Comment
Please, Sign In to add comment