def get(self): car_type = self.lookup_type() dates = self.lookup_dates() account = self.lookup_account() if not account and car_type and dates: self.redirect('/') else: checkin = dates['checkin'] checkout = dates['checkout'] #make sure there are cars in the DB before querying if not Cars.all().count(1): self.response.out.write("there are no cars in the DB") else: availability = self.get_available_cars(checkin, checkout) sporty_instock = availability['sporty'] economy_instock = availability['economy'] luxury_instock = availability['luxury'] if car_type is 'sporty' and not sporty_instock: sold_out = True car = None elif car_type is 'economy' and not economy_instock: sold_out = True car = None elif car_type is 'luxury' and not luxury_instock: sold_out = True car = None else: sold_out = True car = None for x in availability['cars']: if str(x.car_type).lower() == car_type: car = x sold_out = False break #get a unique database id that will be used as single-use token and reservation # res_instance = Reservations.all().get() if not res_instance: handmade_key = db.Key.from_path('Reservations', 1) id_tuple = db.allocate_ids(handmade_key, 1) else: id_tuple = db.allocate_ids(res_instance.key(), 1) id_val = id_tuple[0] id_token = self.mk_cookie(str(id_val)) self.render('checkout.html', checkin=checkin, checkout=checkout, car=car, sold_out=sold_out, id_token=id_token)
def get(self): car_type = self.lookup_type() dates = self.lookup_dates() account = self.lookup_account() if not account and car_type and dates: self.redirect('/') else: checkin=dates['checkin'] checkout=dates['checkout'] #make sure there are cars in the DB before querying if not Cars.all().count(1): self.response.out.write("there are no cars in the DB") else: availability=self.get_available_cars(checkin,checkout) sporty_instock = availability['sporty'] economy_instock = availability['economy'] luxury_instock = availability['luxury'] if car_type is 'sporty' and not sporty_instock: sold_out=True car = None elif car_type is 'economy' and not economy_instock: sold_out=True car = None elif car_type is 'luxury' and not luxury_instock: sold_out=True car = None else: sold_out=True car=None for x in availability['cars']: if str(x.car_type).lower() == car_type: car = x sold_out = False break #get a unique database id that will be used as single-use token and reservation # res_instance = Reservations.all().get() if not res_instance: handmade_key = db.Key.from_path('Reservations', 1) id_tuple = db.allocate_ids(handmade_key, 1) else: id_tuple = db.allocate_ids(res_instance.key(), 1) id_val = id_tuple[0] id_token = self.mk_cookie(str(id_val)) self.render('checkout.html',checkin=checkin,checkout=checkout,car=car, sold_out=sold_out,id_token=id_token)
def new(cls, title, artist, tracks, add_date=None, asin=None, cover_small=None, cover_large= None, is_new=True, key=None, parent=None, key_name=None, **kwds): if add_date is None: add_date = datetime.datetime.now() if key is None: proto_key = db.Key.from_path("Album", 1) batch = db.allocate_ids(proto_key, 1) key = db.Key.from_path('Album', batch[0]) # Instantiate the tracks, put them (Model.put() returns keys) if tracks: tracks = [Song(title=trackname, artist=artist, album=key,).put() for trackname in tracks] album = cls(parent=parent, key_name=key_name, key=key, title=title, lower_title=title.lower(), artist=artist, add_date=add_date, isNew=is_new, songList=tracks, **kwds) if cover_small is not None: album.cover_small = cover_small if cover_large is not None: album.cover_large = cover_large if asin is not None: # Amazon isn't working still as of time of writing album.asin = asin return album
def set_url(url_string, request): encoded = urllib.quote_plus(urllib.unquote(url_string)) url = Url.all().filter('url =', encoded).get() if url is not None: new_id_num = url.key().id() else: handmade_key = db.Key.from_path('Url', 1) new_ids = db.allocate_ids(handmade_key, 1) new_id_num = int(new_ids[0]) new_key = db.Key.from_path('Url', new_id_num) url = Url(key = new_key) url.url = encoded url.put() __hit(url, request) code = base62.encode(new_id_num) if memcache.get('cacheurls:%s' % code) is None: memcache.add('cacheurls:%s' % code, url, 60*60*12) return code
def allocate_ids(): # Create an employee using key name. employee = Employee(key_name='asalieri', first_name='Antonio', last_name='Salieri', hire_date=datetime.datetime.now().date(), attended_hr_training=True) employee.put() employee_key = employee.key() logging.info('Employee key: %s' % employee_key) logging.info('Employee id: %s' % employee_key.id()) logging.info('Employee key name: %s' % employee_key.name()) logging.info('Employee key to_path: %s' % employee_key.to_path()) # Now call allocate_ids, these ids will be reserved and will not be used # by datastore automatically. ids = db.allocate_ids(employee.key(), 10) ids_range = range(ids[0], ids[1] + 1) logging.info('Allocated IDs: %s' % (ids, )) # Create an instance using our reserved id. one_id = ids[0] logging.info('Create entity using ID: %s' % one_id) new_key = db.Key.from_path('Employee', one_id) new_instance = Employee(key=new_key) new_instance.put() assert new_instance.key().id() == one_id # When creating ones using automatical id, datastore will use other ids. another_instance = Employee() another_instance.put() another_id = another_instance.key().id() logging.info('Create entity using automatic id: %s' % another_id) assert another_id not in ids_range
def allocate_ids(): # Create an employee using key name. employee = Employee( key_name='asalieri', first_name='Antonio', last_name='Salieri', hire_date=datetime.datetime.now().date(), attended_hr_training = True) employee.put() employee_key = employee.key() logging.info('Employee key: %s' % employee_key) logging.info('Employee id: %s' % employee_key.id()) logging.info('Employee key name: %s' % employee_key.name()) logging.info('Employee key to_path: %s' % employee_key.to_path()) # Now call allocate_ids, these ids will be reserved and will not be used # by datastore automatically. ids = db.allocate_ids(employee.key(), 10) ids_range = range(ids[0], ids[1] + 1) logging.info('Allocated IDs: %s' % (ids,)) # Create an instance using our reserved id. one_id = ids[0] logging.info('Create entity using ID: %s' % one_id) new_key = db.Key.from_path('Employee', one_id) new_instance = Employee(key=new_key) new_instance.put() assert new_instance.key().id() == one_id # When creating ones using automatical id, datastore will use other ids. another_instance = Employee() another_instance.put() another_id = another_instance.key().id() logging.info('Create entity using automatic id: %s' % another_id) assert another_id not in ids_range
def newpost(web,args=None): """ /home/newpost?sub=subject&cnt=text New post: subject length no longer than 120 chars, content not longer than 2000. """ if not web.logged_in: web.fail('Not logged in') return frm,sub,cnt = web.get_params(['frm','sub','cnt']) if not frm: logging.warning('/home/newpost?frm not found') web.fail('Invalid parameter') return if not sub: logging.warning('/home/newpost?sub not found') web.fail('Invalid parameter') return if not cnt: logging.warning('/home/newpost?cnt not found') web.fail('Invalid parameter') return if len(sub)>120: sub = sub[:121] if len(cnt)>2000: cnt = cnt[:2001] madekey = db.Key.from_path('SuiPost',1) pid = db.allocate_ids(madekey,1)[0] p = SuiPost(key=db.Key.from_path('SuiPost',pid),forum=int(frm),subject=sub) p.author = '%s:%s'%(web.user.key().name(),web.user.name) c = SuiContent(post=pid,author=p.author,content=cnt) db.put([p,c]) SuiPost.decache_by_forum(frm) web.succeed('{"id":%d,"author":"%s","time":"%s","subject":"%s"}'%(p.key().id(),p.author,datetime.strftime(p.postime,'%Y-%m-%d %H:%M:%S'),p.subject))
def createIssue(summary, type, priority, description, user): ids = db.allocate_ids(db.Key.from_path(model.issues.Issue.__name__, 1), 1) key = db.Key.from_path(model.issues.Issue.__name__, ids[0]) issue = model.issues.Issue(key=key, summary=summary, description=description, creator_user=user, type=type, priority=priority, state=model.issues.State.New, resolution=None) issue.put() indexIssue(issue, []) return ids[0]
def post(self): find_key = db.Key.from_path('Spec', 1) key_batch = db.allocate_ids(find_key, 1) spec = Spec(id=key_batch[0]) new_id = spec.put().id() if self.request.body != '': self.put(new_id) self.response.headers['Content-Type'] = 'application/json' self.response.write(json.dumps({'id': new_id}))
def create(cls, tenant, user=None, uid=None, text='', email=None): """Creates a credential Object generating a random secret and a random uid if needed.""" secret = guid128(salt=str(tenant))[:12] if not uid: handmade_key = db.Key.from_path('Credential', 1) uid = "u%s" % (db.allocate_ids(handmade_key, 1)[0]) instance = cls.get_or_insert(key_name=uid, uid=uid, secret=secret, tenant=tenant, user=user, text=text, email=email) return instance
def get_new_key(model, model_str): mckey = "%s_ids"%(model_str) model_ids = memcache.get(mckey) if ((model_ids is not None) and (len(model_ids) > 0)): logging.info("get_new_key from memcache") else: try: model_ids_batch = db.allocate_ids(model.all().get().key(), 10) logging.info("get_new_key alloc new from existing model") except AttributeError: model_ids_batch = db.allocate_ids(db.Key.from_path(model_str, 1), 10) logging.info("get_new_key alloc new") model_ids = range(model_ids_batch[0], model_ids_batch[1] + 1) model_key = db.Key.from_path(model_str, model_ids.pop(0)) if not memcache.set(mckey,model_ids,KEY_CACHE_SECONDS): logging.error('get_new_key memcache set failure') logging.info("get_new_key: model=%s id=%s"%(model_str, model_key.id())) return model_key
def post(self): source = db.Key(self.request.get('source')) dest = db.Key(self.request.get('dest')) key_name = models.Migration.make_key_name(source.kind(), source.name(), dest.kind(), dest.name()) id = db.allocate_ids(db.Key.from_path('Migration', 1), 1)[0] migration = models.Migration.get_or_insert(key_name, id=id) taskqueue.add(queue_name='scan', params={'migration': key_name}) self.redirect('/migration/%d' % migration.id)
def create(cls, tenant='_unknown', user=None, uid=None, text='', email=None, admin=False): """Creates a credential Object generating a random secret and a random uid if needed.""" # secret hopfully contains about 64 bits of entropy - more than most passwords data = "%s%s%s%s%s" % (user, uuid.uuid1(), uid, text, email) secret = str(base64.b32encode(hashlib.md5(data).digest()).rstrip('='))[1:15] if not uid: handmade_key = db.Key.from_path('Credential', 1) uid = "u%s" % (db.allocate_ids(handmade_key, 1)[0]) return cls.get_or_insert(key_name=uid, uid=uid, secret=secret, tenant=tenant, user=user, text=text, email=email, admin=admin)
def generateKeys(self, count): result = [] if count > 0: baseKey = db.Key.from_path("FeedbackComment", 1) ids = db.allocate_ids(baseKey, count) idsRange = range(ids[0], ids[1] + 1) for item in range(0, count): result.append(str(db.Key.from_path("FeedbackComment", idsRange[item]))) return result
def get(self): ids = db.allocate_ids(db.Key.from_path('Entity', 1), 1) e1_key = db.Key.from_path('Entity', ids[0]) e1 = Entity(key=e1_key) e2 = Entity() e2.reference = e1_key db.put([e1, e2]) self.response.write('<p>The time is: %s</p>' % str(datetime.datetime.now()))
def generateKeys(self, data): model = self.all() model.filter("name = ", data[self.JSON_STATUS_NAME] ) model.filter("userId = ", self.userId) entity = model.fetch(1) if(len(entity) > 0): data[self.JSON_STATUS_KEY] = str(entity[0].key()) else: baseKey = db.Key.from_path('DouiTodoStatus', 1) ids = db.allocate_ids(baseKey, 1) idsRange = (ids[0], ids[1] + 1) data[self.JSON_STATUS_KEY] = str(db.Key.from_path('DouiTodoStatus', idsRange[0]))
def generateKeys(cls, data): logging.debug("generateKeys() started") data["itemsKeys"] = [] baseKey = db.Key.from_path('DouiTodoCategory', 1) ids = db.allocate_ids(baseKey, data["itemsCount"]) idsRange = range(ids[0], ids[1] + 1) for item in range(0, data["itemsCount"]): strNewKey = str(db.Key.from_path('DouiTodoItem', idsRange[item])) logging.debug("Created key: "+strNewKey+", for id: "+str(idsRange[item])) data["itemsKeys"].append(strNewKey) logging.debug("generateKeys() finished")
def get(self): template_values = { 'main_url': MAIN_URL, } if self.request.cookies.get('key') is None: handmade_key = db.Key.from_path('Cart', 1) first_batch = db.allocate_ids(handmade_key, 1) cart_id = first_batch[0] new_key = db.Key.from_path('Cart', cart_id) self.response.headers.add_header('Set-Cookie', 'key='+str(new_key)) logging.info(self.request.cookies.get('key')) template = JINJA_ENVIRONMENT.get_template('index.html') self.response.write(template.render())
def testAllocateIds(self): """Tests allocation of id ranges.""" class EmptyModel(db.Model): pass for i in xrange(0, 1000): key = EmptyModel().put() query = db.GqlQuery("SELECT * FROM EmptyModel") self.assertEqual(1000, query.count()) start, end = db.allocate_ids(key, 2000) self.assertEqual(start, 1001) self.assertEqual(end, 3000)
def get(self): handmade_key = db.Key.from_path('Product', 1) new_ids = db.allocate_ids(handmade_key, 1) new_id_num = int(new_ids[0]) new_key = db.Key.from_path('Product', new_id_num) product = Product(key=new_key, id = new_key.id(), desc = self.request.get('desc'), price = int(self.request.get('price')), qty = int(self.request.get('qty'))) product.pdate = datetime.datetime.now().date() product.mdate = datetime.datetime.now().date() product.put(); self.redirect('/product')
def StartOperation(description): """Start datastore admin operation. Args: description: operation description to be displayed to user. Returns: an instance of DatastoreAdminOperation. """ operation = DatastoreAdminOperation( description=description, id=db.allocate_ids(db.Key.from_path(DatastoreAdminOperation.kind(), 1), 1)[0] ) operation.put(config=_CreateDatastoreConfig()) return operation
def create(cls, tenant='_unknown', user=None, uid=None, text='', email=None, admin=False): """Creates a credential Object generating a random secret and a random uid if needed.""" # secret hopfully contains about 64 bits of entropy - more than most passwords data = u'%s%s%s%s%s%f%s' % (user, uuid.uuid1(), uid, text, email, time.time(), os.environ.get('CURRENT_VERSION_ID', '?')) digest = hashlib.md5(data.encode('utf-8')).digest() secret = str(base64.b32encode(digest).rstrip('='))[1:15] if not uid: handmade_key = db.Key.from_path('Credential', 1) uid = "u%s" % (db.allocate_ids(handmade_key, 1)[0]) if email: return cls.get_or_insert(key_name=uid, uid=uid, secret=secret, tenant=tenant, user=user, text=text, email=email, admin=admin) else: return cls.get_or_insert(key_name=uid, uid=uid, secret=secret, tenant=tenant, user=user, text=text, admin=admin)
def StartOperation(description): """Start datastore admin operation. Args: description: operation description to be displayed to user. Returns: an instance of DatastoreAdminOperation. """ operation = DatastoreAdminOperation( description=description, id=db.allocate_ids(db.Key.from_path(DatastoreAdminOperation.kind(), 1), 1)[0]) operation.put(config=_CreateDatastoreConfig()) return operation
def StartOperation(description): """Start datastore admin operation. Args: description: operation description to be displayed to user. Returns: an instance of DatastoreAdminOperation. """ operation = DatastoreAdminOperation( description=description, status=DatastoreAdminOperation.STATUS_ACTIVE, id=db.allocate_ids( db.Key.from_path(DatastoreAdminOperation.kind(), 1), 1)[0]) operation.put() return operation
def store_request(self): # store the request string GET. #self.response.out.write('store') handmade_key = db.Key.from_path('Record', 1) k=db.allocate_ids(handmade_key,1)[0] dg=dict(self.request.GET) if 'user' in dg or 'addr' in dg: return self.serve_default() r=Record(content=str(dict(dg)), key=db.Key.from_path('Record',k) ) r.verify=''.join([chr(randint(65,89)) for dummy in range(4)]) r.user=self.request.remote_user r.addr=self.request.remote_addr p=r.put() #self.response.write(repr(r.content)+ " "+" !@#!@# "+ repr(p.id()) + "<hr>") return r.verify+str(k)
def upload(web, args=None): """ Upload a gift image, or flash movie swf. Space used not counted into SuiUser.spaceused. """ web.require_author() me = web.user gname, gcat, gsex, gpic, myown = web.get_params( ['gname', 'gcat', 'gsex', 'gpic', 'myown']) if not gpic: web.redirect_with_msg('No picture uploaded') return if not gname: gname = gcat imgfile = web.request.POST.get('gpic').filename x = imgfile.rfind('.') if x < 0: web.redirect_with_msg('Unknown image file') return ext = imgfile[x + 1:].lower() if ext not in ['jpg', 'png', 'gif', 'swf']: web.redirect_with_msg( 'Image format not supported, only .jpg,.gif,.png', 'gift') return if not gsex: gsex = 'B' gsex = gsex[0].upper() if not gsex in ['M', 'F', 'B']: gsex = 'B' akey = db.Key.from_path('SuiBirthdayGift', 1) gid = db.allocate_ids(akey, 1)[0] gkey = '%s%s' % (gsex, gid) g = SuiBirthdayGift(key_name=gkey, creator=me.key().name(), name=gname, category=gcat, gender=gsex) u = SuiBirthdayUser.get_by_key_name(me.key().name()) u.usemyown = (myown == 'myown') ms = MediaStore(key_name='bdg_%s' % gkey) ms.stream = db.Blob(gpic) ms.format = ext ms.usage = 'BDG' u.add_mygift(gkey) db.put([ms, u, g]) # ms.put() helper.update_gift_cache('add', gkey, me.key().name()) web.redirect('/gift')
def post(self, something): soundboard = Soundboard() name = cgi.escape(self.request.get('name')) if name is None or len(name) == 0: name = 'Unnamed Soundboard' soundboard.name = name session_id = self.request.cookies.get('id') if session_id is None: handmade_key = db.Key.from_path(name, random.randint(0, 99999999)) id = db.allocate_ids(handmade_key, 1) session_id = id[0] self.response.headers.add_header('Set-Cookie', 'id=%d' % session_id) else: session_id = long(session_id) soundboard.session_id = session_id db.put(soundboard) self.redirect('/board/%d' % soundboard.key().id())
def get(self): # doesn't make sense if the user is already logged in session = get_current_session() if session.is_active(): return self.redirect('/tracker?err=You%20are%20already%20logged%20in.') # get a unique ID for the anonymous user str_id = str(db.allocate_ids(db.Key.from_path('User', 1), 1)[0]) hashed_id = User.make_key_name(str_id, anon=True) user = User.get_or_insert(key_name=hashed_id, display_name='anonymous', email='') # start a session for the user session['my_dname'] = user.display_name session['my_id'] = hashed_id session['my_last_seen'] = int(time.mktime(user.last_seen.timetuple())) session['my_email'] = user.email self.redirect('/tracker')
def __init__(self, source_url): self.images_hostname_proxies = feed_config_connector.get_images_hostname_proxies() self.source_url = source_url for _ in range(3): try: self.img_data = urlfetch.fetch( self._url_for_urlfetch(self.source_url), deadline=20 ).content break except ConnectionClosedError: pass except DeadlineExceededError: pass self.db_image = None handmade_key = db.Key.from_path("Image", 1) self._allocated_image_key = \ db.Key.from_path('Image', list(db.allocate_ids(handmade_key, 1))[0])
def addpage(web,args=None): """ Add a page to the book, at the end. """ bk = web.get_param('bk') book, p = check_get_book_page(web,bk) if book.pagecount >= 500: web.fail('Sorry, but you reached maximum number of pages allowed for a book.') return madekey = db.Key.from_path('SuiPage',1) pid = db.allocate_ids(madekey,1)[0] p = SuiPage(key=db.Key.from_path('SuiPage',pid),book=int(bk)) if web.user.key().name() != book.authors[0]: p.author = web.user.key().name() book.add_page(pid) try: db.put([p, book]) book.recache_updates() web.succeed({'id':pid,'bk':bk,'sc':'','ls':[],'rq':{},'rw':{},'v':p.version}) taskqueue.add(url='/task/feed/add_page',params={'uname':web.user.name,'bkid':bk,'title':book.title}) except Exception,e: logging.exception(e) web.fail('Failed to add page, try later')
def get(self): # doesn't make sense if the user is already logged in session = get_current_session() if session.is_active(): return self.redirect( '/tracker?err=You%20are%20already%20logged%20in.') # get a unique ID for the anonymous user str_id = str(db.allocate_ids(db.Key.from_path('User', 1), 1)[0]) hashed_id = User.make_key_name(str_id, anon=True) user = User.get_or_insert(key_name=hashed_id, display_name='anonymous', email='') # start a session for the user session['my_dname'] = user.display_name session['my_id'] = hashed_id session['my_last_seen'] = int(time.mktime(user.last_seen.timetuple())) session['my_email'] = user.email self.redirect('/tracker')
def putAlbum(title, artist, tracks, add_date=None, asin=None, cover_small=None, cover_large=None, isNew=True): if add_date is None: add_date = datetime.datetime.now() album_fake_key = db.Key.from_path("Album", 1) batch = db.allocate_ids(album_fake_key, 1) album_key = db.Key.from_path('Album', batch[0]) song_keys = [putSong(title=trackname, artist=artist, album=album_key, ).key() for trackname in tracks] tryPutArtist(artist) album = models.Album( key=album_key, title=title, lower_title=title.lower(), artist=artist, add_date=add_date, isNew=isNew, songList=song_keys, ) if cover_small is not None: album.cover_small = cover_small if cover_large is not None: album.cover_large = cover_large if asin is not None: album.asin = asin album.put() if isNew: pass return mcset(album, ALBUM_ENTRY, album.key())
def upload(web, args=None): """ Upload a gift image, or flash movie swf. Space used not counted into SuiUser.spaceused. """ web.require_author() me = web.user gname,gcat,gsex,gpic,myown = web.get_params(['gname','gcat','gsex','gpic','myown']) if not gpic: web.redirect_with_msg('No picture uploaded') return if not gname: gname = gcat imgfile = web.request.POST.get('gpic').filename x = imgfile.rfind('.') if x < 0: web.redirect_with_msg('Unknown image file') return ext = imgfile[x+1:].lower() if ext not in ['jpg','png','gif','swf']: web.redirect_with_msg('Image format not supported, only .jpg,.gif,.png','gift') return if not gsex: gsex = 'B' gsex = gsex[0].upper() if not gsex in ['M','F','B']: gsex = 'B' akey = db.Key.from_path('SuiBirthdayGift',1) gid = db.allocate_ids(akey,1)[0] gkey = '%s%s'%(gsex,gid) g = SuiBirthdayGift(key_name=gkey,creator=me.key().name(),name=gname,category=gcat,gender=gsex) u = SuiBirthdayUser.get_by_key_name(me.key().name()) u.usemyown = (myown == 'myown') ms = MediaStore(key_name='bdg_%s'%gkey) ms.stream = db.Blob(gpic) ms.format = ext ms.usage = 'BDG' u.add_mygift(gkey) db.put([ms,u,g]) # ms.put() helper.update_gift_cache('add',gkey, me.key().name()) web.redirect('/gift')
def ParsePatchSet(patchset): """Patch a patch set into individual patches. Args: patchset: a models.PatchSet instance. Returns: A list of models.Patch instances. """ patches = [] ps_key = patchset.key() splitted = SplitPatch(patchset.data) if not splitted: return [] first_id, last_id = db.allocate_ids( db.Key.from_path(models.Patch.kind(), 1, parent=ps_key), len(splitted)) ids = range(first_id, last_id + 1) for filename, text in splitted: key = db.Key.from_path(models.Patch.kind(), ids.pop(0), parent=ps_key) patches.append(models.Patch(patchset=patchset, text=utils.to_dbtext(text), filename=filename, key=key)) return patches
def addpage(web, args=None): """ Add a page to the book, at the end. """ bk = web.get_param('bk') book, p = check_get_book_page(web, bk) if book.pagecount >= 500: web.fail( 'Sorry, but you reached maximum number of pages allowed for a book.' ) return madekey = db.Key.from_path('SuiPage', 1) pid = db.allocate_ids(madekey, 1)[0] p = SuiPage(key=db.Key.from_path('SuiPage', pid), book=int(bk)) if web.user.key().name() != book.authors[0]: p.author = web.user.key().name() book.add_page(pid) try: db.put([p, book]) book.recache_updates() web.succeed({ 'id': pid, 'bk': bk, 'sc': '', 'ls': [], 'rq': {}, 'rw': {}, 'v': p.version }) taskqueue.add(url='/task/feed/add_page', params={ 'uname': web.user.name, 'bkid': bk, 'title': book.title }) except Exception, e: logging.exception(e) web.fail('Failed to add page, try later')
def upload(web, args=None): """ Upload a new virtual goods item. A VG has a 50x50 logo image and a large image suitable for display in gallery. Some VGs do not have a big image because they never used in galleries. """ #if not web.logged_in: # raise Exception('Not login') web.require_author() paramkeys = [ 'id', 'itm', 'bkid', 'price', 'note', 'display', 'logofile', 'imgfile' ] itemid, itemname, bkid, price, note, display, logo, img = web.get_params( paramkeys) newitem = False if itemid: itm = from_cache(itemid) if not itm: itm = SuiGoods.get_by_id(int(itemid)) else: madekey = db.Key.from_path('SuiGoods', 1) #why 1? itemid = db.allocate_ids(madekey, 1)[0] itmkey = db.Key.from_path('SuiGoods', itemid) itm = SuiGoods(key=itmkey) newitem = True if itemname: itm.name = itemname if display == 'True': itm.display = True else: itm.display = False if bkid: itm.book = bkid itm.author = web.user.key().name() if price: itm.price = float(price) if itm.price <= 0: itm.display = False else: itm.returnable = True itm.giftable = itm.display if note: itm.note = note dbs = [itm] ver = 0 au = web.user.get_author() if img: imgfile = web.request.POST.get('imgfile').filename ms = save_img(imgfile, img, 740, 500, 'vgb', itemid, bkid, newitem) if ms: ver = 1 dbs.append(ms) au.spaceused += len(db.model_to_protobuf(ms).Encode()) else: web.response.out.write( '<html>Only .jpg or .png supported. <a href="/market">Go Back</a></html>' ) return if logo: logofile = web.request.POST.get('logofile').filename mst = save_img(logofile, logo, 50, 50, 'vg', itemid, bkid, newitem) if mst: ver = 1 dbs.append(mst) au.spaceused += len(db.model_to_protobuf(mst).Encode()) else: web.response.out.write( '<html>Only .jpg or .png supported. <a href="/market">Go Back</a></html>' ) return elif img: mst = save_img(imgfile, img, 50, 50, 'vg', itemid, bkid, newitem) if mst: ver = 1 dbs.append(mst) au.spaceused += len(db.model_to_protobuf(mst).Encode()) if ver > 0: itm.version += ver dbs.append(au) web.user.recache() try: db.put(dbs) helper.decaches([ 'VG_%s' % itm.author, 'VG_%s' % au.key().name(), '%s' % itm.key().id() ]) web.redirect('/author') except Exception, e: logging.error(e) web.response.out.write( '<html>Error saving your item, retry later. <a href="/market">Go Back</a></html>' )
def allocate_ids(): return db.allocate_ids(db.Key.from_path(ShortURL.kind(), 1), amount) # (start, end)
def allocate_ids(model_class, count, parent=None): id_range = db.allocate_ids(db.Key.from_path(model_class.kind(), 1, parent=parent), count) # (start, end) return range(id_range[0], id_range[1] + 1)
def post(self, tenant): user = self.login_required() tenant = user.tenant if not hasattr(self.request.POST['pdfdata'], 'file'): logging.debug(dir(self.request)) logging.debug(vars(self.request)) HTTP400_BadRequest('pdfdata not a file') pdfdata = self.request.POST['pdfdata'].file.read() if len(pdfdata) > 900000: raise HTTP413_TooLarge('PDF bigger than 900k') ref = self.request.POST['ref'] typ = self.request.POST['type'] refs = ref.split() if refs: akte_designator = refs[0] else: handmade_key = db.Key.from_path('Akte', 1) akte_designator = "ablage%s" % (db.allocate_ids(handmade_key, 1)[0]) pdf_id = str( base64.b32encode(hashlib.sha1(pdfdata).digest()).rstrip('=')) key_name = "%s-%s" % (tenant, pdf_id) # do we already have that document - ignore designator given by uploader doc = Dokument.get_by_key_name(key_name) if doc and doc.akte.designator != akte_designator: ref = ' '.join(list(set([akte_designator] + ref.split()))) akte_designator = doc.akte.designator akte_key = "%s-%s" % (tenant, akte_designator) akteargs = dict(type=typ, designator=akte_designator, datum=convert_to_date(self.request.POST.get('datum'))) for key in ('name1', 'name2', 'name3', 'strasse', 'land', 'plz', 'ort', 'email', 'ref_url', 'datum'): if self.request.POST.get(key): akteargs[key] = self.request.POST.get(key) if self.request.POST.get('akte_%s' % key): akteargs[key] = self.request.POST.get('akte_%s' % key) if key == 'datum' and 'datum' in akteargs: akteargs[key] = convert_to_date(akteargs[key]) akte = Akte.get_or_insert(akte_key, tenant=tenant, **akteargs) oldref = akte.ref newref = list(set(oldref + self.request.POST.get('ref', '').split())) newseit = oldseit = akte.seit if self.request.POST.get('datum'): postseit = convert_to_date(self.request.POST.get('datum')) if postseit and postseit and (postseit < postseit): newseit = postseit if (newref != oldref) or (newseit != oldseit): akte.seit = newseit akte.ref = newref akte.put() docargs = dict(type=typ, datum=datetime.date.today(), file_length=len(pdfdata)) for key in ('name1', 'name2', 'name3', 'strasse', 'land', 'plz', 'ort', 'email', 'ref_url', 'datum' 'quelle'): if self.request.POST.get(key): docargs[key] = self.request.POST.get(key) if 'datum' in docargs: docargs['datum'] = convert_to_date(docargs['datum']) dokument = Dokument.get_or_insert(key_name, designator=pdf_id, akte=akte, tenant=tenant, **docargs) # resave if it existed but something had changed for key in docargs.keys(): if getattr(dokument, key) != docargs[key]: logging.debug('%s: key %s has changed', pdf_id, key) dokument.put() break DokumentFile.get_or_insert( key_name, dokument=dokument, akte=akte, data=pdfdata, tenant=tenant, mimetype='application/pdf', filename=self.request.POST['pdfdata'].filename) oldref = dokument.ref newref = list(set(oldref + self.request.POST.get('ref', '').split())) if newref != oldref: dokument.ref = newref dokument.put() self.redirect(dokument.get_url()) self.response.set_status(201) self.response.headers["Content-Type"] = 'text/plain' self.response.out.write('ok:%s\n' % dokument.designator)
def upload(web, args=None): """ Upload a new book or update an old book info. If it's a new book, all followers are notified by email. Note: only first author can modify an existing book (owner). """ web.require_author() # if not web.logged_in: # raise Exception('Not login') paramkeys = [ 'bkid', 'title', 'status', 'logofile', 'intro', 'toc', 'notes', 'authors', 'zine', 'height', 'quests' ] bkid, title, status, logo, intro, toc, notes, authors, zine, height, quests = web.get_params( paramkeys) newbook = False if bkid: bk = SuiBook.seek_by_id(bkid) if not bk: logging.warning('book %s not found' % bkid) web.redirect_with_msg('Invalid Book ID', 'book') return if web.user.key().name() != bk.authors[0]: web.redirect_with_msg('No permission', 'book') return else: madekey = db.Key.from_path('SuiBook', 1) bkid = db.allocate_ids(madekey, 1)[0] bk = SuiBook(key=db.Key.from_path('SuiBook', bkid)) bk.authors = [web.user.key().name()] if zine and zine != 'none': bk.zine = zine bk newbook = True if title: bk.title = title if status: bk.status = status genres = web.request.get('genre', allow_multiple=True) if genres: bk.genre = list(set(genres)) if intro: bk.intro = join_lines(intro) if toc: bk.toc = join_lines(toc, 'ul') if notes: bk.notes = join_lines(notes) if quests: bk.quests = validate_quests(quests) if height: try: bk.height = int(height) except: pass # if authors: # import re # aus = [s for s in re.split(r'[ ,;]',authors) if s] # aus = [s.key().name() for s in SuiAuthor.get_by_key_name(aus) if s] # if aus: # bk.authors = list(set(bk.authors + aus)) dbs = [bk] if logo: logofile = web.request.POST.get('logofile').filename #logging.debug('-=------- save logofile %s'%logofile) x = logofile.rfind('.') if x < 0: web.fail('Unknown image file') return ext = logofile[x + 1:].lower() if ext in ['jpg', 'png']: from google.appengine.api import images m = images.Image(logo) w = h = 240 if m.width > 240 or m.height > 240: m.resize(240, 240) w = m.width h = m.height if ext == 'png': oenc = images.PNG else: oenc = images.JPEG m = m.execute_transforms(oenc) else: m = logo if newbook: ms = MediaStore(key_name='bk_%s' % bkid) else: ms = MediaStore.get_by_key_name('bk_%s' % bkid) if ms: bk.version += 1 ms.decache() else: ms = MediaStore(key_name='bk_%s' % bkid) ms.book = '%s' % bkid ms.width = w ms.height = h ms.usage = 'BK' ms.stream = db.Blob(m) ms.format = ext dbs.append(ms) bk.version += 1 me = SuiAuthor.get_by_key_name(web.user.key().name()) if not me: logging.warning('Book editor is not in SuiAuthor %s' % web.user.key().name()) web.redirect_with_msg('You are not a registered author', 'book') return me.add_book(bk) dbs.append(me) try: db.put(dbs) me.recache() bk.decache_for_newbook() helper.to_cache('%s' % bkid, bk) if newbook: SuiLog.log_newbook(me, bkid) taskqueue.add(url='/task/genre/add_book', params={ 'genre': ','.join(bk.genre), 'bkid': bkid }) taskqueue.add(url='/task/feed/add_book', params={ 'uid': me.key().name(), 'uname': me.name, 'bkid': bkid, 'title': bk.title, 'intro': intro or '' }) #bdy='Good news! %s published a new book "%s". Visit <a href="http://suicomics.appspot.com">Suinova Comics</a> to check it out.'%(me.name,bk.title) #TODO: notify my fans about the new book #helper.notify_followers(me,'New book by %s'%me.name,bdy) #logging.info('followers notified if any') web.redirect('/book/' + bkid) except Exception, e: logging.error(e) web.redirect_with_msg('Error saving your item, retry later.', 'book')