Пример #1
0
 def post(self):
     item_key = db.Key(self.request.get('id'))
     item = Item.get(item_key)
     image = None
     if (self.request.get("img")):
         image = images.Image(self.request.get("img"))
     description = self.request.get("description")
     n = int(self.request.get('n'))
     write_image(item, image, n, description)
Пример #2
0
 def get(self):
   #TODO: Change loop to redirect
   oldest_possible_date = datetime.datetime.now() - datetime.timedelta(days=30)  
   items = Item.all().filter('timestamp <', oldest_possible_date).fetch(1000)
   size = len(items)
   for item in items:
       delete.delete_item(item)
   if (size >= 1000):
       self.redirect('/delete/delete_expired_items', true)
       return
   self.response.out.write('All expired items have been deleted')
Пример #3
0
def add_operation_fifo():
    arr = json.loads(request.data)
    print('Request received')
    for data in arr:
        i = data['item']
        var = i['variable']
        k = i['kind']
        item = Item(k, var)
        operation = Operation(item.kind, item, data['tid'])
        time_taken = fifo.organise_operations(operation)
    print('TIME TAKEN FOR FIFO: ' + str(time_taken) + ' units')
    return str(time_taken)
Пример #4
0
def add_operation_ldsf():
    arr = json.loads(request.data)
    print('Request received')
    for data in arr:
        i = data['item']
        var = i['variable']
        k = i['kind']
        item = Item(k, var)
        operation = Operation(item.kind, item, data['tid'])
        ldsf.organise_operations(operation, len(arr))
    with open('./ldsf_time.txt', 'r') as f:
        return f.readline()
    return 'OK'
Пример #5
0
 def get(self):      
     id = self.request.get('id')
     item = Item.get(id)
     type = self.request.get('get')
     
     if type == 'default_thumbnail':
       self.response.headers['Content-Type'] = 'image/jpeg'
       thumbnail = Thumbnail.all().ancestor(item).filter('index', item.thumbnail_index).get()
       if not thumbnail:
           self.redirect('/images/x.jpg')
       else:    
           self.response.out.write(thumbnail.picture)
       return  
     elif type == 'thumbnail':
       self.response.headers['Content-Type'] = 'image/jpeg'  
       n = int(self.request.get('n'))
       thumbnail = Thumbnail.all().ancestor(item).filter('index', n).get()
       if not thumbnail:
           self.redirect('/images/x.jpg')
       else:    
           self.response.out.write(thumbnail.picture)
       return 
     elif type == 'default':
       self.response.headers['Content-Type'] = 'image/jpeg'  
       n = int(self.request.get('n'))
       picture = Picture.all().ancestor(item).filter('index', n).get()
       if not picture:
           self.redirect('/images/x.jpg')
       else:    
           self.response.out.write(picture.picture)
       return
     elif type == 'description':
       n = int(self.request.get('n'))
       picture = Picture.all().ancestor(item).filter('index', n).get()
       if not picture:
           self.response.set_status(204)
       else:
           self.response.out.write(picture.description)
       return
     elif type == 'descriptions':
       pictures = Picture.all().ancestor(item).fetch(6)
       descriptions = {}
       for picture in pictures:
           descriptions[picture.index] = picture.description
       self.response.out.write(simplejson.dumps(descriptions))
       return
     else:
       self.error(404)
       return  
Пример #6
0
 def post(self):
   id = self.request.get("id")
   if id != '':
     item_key = db.Key(id)
     item = db.get(item_key)
     item_parts_to_write = []
     user_id = users.get_current_user().user_id()
     user = User.get_by_key_name('key:' + user_id)
     if (item.parent().user == users.get_current_user()):
       item.title = self.request.get("title")
       item.price = float(self.request.get("price"))
       item.description = self.request.get("description")
       item.category = self.request.get("category")
       item.tags = self.request.get("tags").lower()
       item.location = user.home
       item.currency = user.currency
       
       item.timestamp = datetime.datetime.now()
       item_parts_to_write.append(item)
       
       category_string = pluralize(clean_word(self.request.get("category").lower()))
       
       if self.request.get("img"):
         image = images.Image(self.request.get("img"))
         image_post.write_image(item, image, 0, "")
       
       tags = remove_duplicates([clean_word(word) for word in self.request.get("tags").split(',')])
       extra_tags = ['']  
       for tag in tags:
           if len(tag.split()) > 1:
               tags.extend(tag.split())
               extra_tags.extend(tag.split())
       if category_string:
           tags.append(category_string)
           extra_tags.append(category_string)
       singulars = get_singulars(tags)
       tags += singulars   
       extra_tags += singulars    
       title_words = [clean_word(word) for word in item.title.split(' ')] 
       tags += title_words
       extra_tags += title_words
        
       # Delete old item light so that when sorted by __key__ ascending (default)
       # Oldest items (about to expire) show first.
       #TODO check if touching or re putting is enough?
       db.delete(item.itemlight_set[0])
       item_light = ItemLight(parent=user, title=item.title, item_key=item.key(), location=user.home, area=user.area, tags=tags, extra_tags=extra_tags)
       item_parts_to_write.append(item_light)
       
       db.put(item_parts_to_write)  
       logging.info('Item was updated: %s, by user: %s' % (item.title, item.parent().user.nickname()))
       
   else:
     user_id = users.get_current_user().user_id()
     user = User.get_by_key_name('key:' + user_id)
     item = Item(parent=user, location=user.home)
     item.currency = user.currency
     item.title = self.request.get("title")
     item.category = self.request.get("category")
     item.tags = self.request.get("tags").lower()
     item.price = float(self.request.get("price"))
     item.description = self.request.get("description")
     item_key = item.put()
     
     category_string = pluralize(clean_word(self.request.get("category").lower()))
     
     if self.request.get("img"):
         image = images.Image(self.request.get("img"))
         image_post.write_image(item, image, 0, "")
     
     tags = remove_duplicates([clean_word(word) for word in self.request.get("tags").split(',')])
     extra_tags = ['']  
     for tag in tags:
         if len(tag.split()) > 1:
             tags.extend(tag.split())
             extra_tags.extend(tag.split())
     if category_string:
         tags.append(category_string)
         extra_tags.append(category_string)
     singulars = get_singulars(tags)
     tags += singulars   
     extra_tags += singulars    
     title_words = [clean_word(word) for word in item.title.split(' ')] 
     tags += title_words
     extra_tags += title_words
       
     item_light = ItemLight(parent=user, title=item.title, item_key=item_key, location=user.home, area=user.area, tags=tags, extra_tags=extra_tags)
     item_light.put()
     
     logging.info('Item was created: %s, by user: %s' % (item.title, item.parent().user.nickname()))
     
   self.response.out.write(cgi.escape(MyEncoder.encode(MyEncoder(), item)))
   self.response.headers.add_header('Location', '/item/' + str(item.key()))
   self.response.set_status(201)
Пример #7
0
 def get(self):      
   if (self.request.get("action") == "map"):
     #TODO: Add results from categories with high edges 
     min_lat = float(self.request.get('min_lat'))
     min_lng = float(self.request.get('min_lng'))
     max_lat = float(self.request.get('max_lat'))
     max_lng = float(self.request.get('max_lng'))
     search_terms = [word.strip().lower() for word in self.request.get('search_term').split()]
     result = []
     tags = {}
     areas = geo.getInnerSquares(db.GeoPt(min_lat, min_lng), db.GeoPt(max_lat, max_lng))
     while len(areas) > 0:
         query = db.Query(ItemLight)
         query.filter("area =", areas.pop())
         for term in search_terms:
             query.filter("tags =", term)
         items = query.fetch(200)
         for item in items:
             result.append(item)
     
     self.response.out.write(cgi.escape('{Items: ' + MyEncoder.encode(MyEncoder(), result) + "}"))
     
   elif (self.request.get("action") == "user"):
     user_id = self.request.get("user_id")
     offset = int(self.request.get("offset"))
     user = User.get_by_key_name('key:' + user_id)
     items = []
     if user:
         items = ItemLight.all().ancestor(user).fetch(1000, offset=offset)
     self.response.out.write(cgi.escape('{Items: ' + MyEncoder.encode(MyEncoder(), items) + "}"))
     return
 
   elif (self.request.get("action") == "me"):
     items = []
     user = users.get_current_user()
     if user:
         user = User.get_by_key_name('key:' + user.user_id())
         query = db.Query(ItemLight)
         query.ancestor(user)
         items = query.fetch(1000)
     self.response.out.write(cgi.escape('{Items: ' + MyEncoder.encode(MyEncoder(), items) + "}"))
     
   elif (self.request.get("action") == "item"):
     key = self.request.get("key")
     item = Item.get(key)
     self.response.out.write(MyEncoder.encode(MyEncoder(), item))
     
   elif (self.request.get("action") == "comments"):
     offset = int(self.request.get("offset"))  
     key = self.request.get("id")
     item = Item.get(key)
     comments = Comment.all().ancestor(item).order('-__key__').fetch(5, offset=offset)
     self.response.out.write(cgi.escape('{Messages: ' + MyEncoder.encode(MyEncoder(), comments) + '}'))
     
   elif (self.request.get("action") == "good_ratings"):
     offset = int(self.request.get("offset"))  
     key = self.request.get("id")
     item = Item.get(key)
     ratings = GoodRating.all().ancestor(item.parent()).order('-__key__').fetch(5, offset=offset)
     logging.info(MyEncoder.encode(MyEncoder(), ratings))
     self.response.out.write(cgi.escape('{Messages: ' + MyEncoder.encode(MyEncoder(), ratings) + '}'))
     
   elif (self.request.get("action") == "bad_ratings"):
     offset = int(self.request.get("offset"))  
     key = self.request.get("id")
     item = Item.get(key)
     ratings = BadRating.all().ancestor(item.parent()).order('-__key__').fetch(5, offset=offset)
     self.response.out.write(cgi.escape('{Messages: ' + MyEncoder.encode(MyEncoder(), ratings) + '}'))