Ejemplo n.º 1
0
    def post(self):
        """Accepts a URL argument and saves it to the database
        """
        title = self.get_argument('title')
        url = self.get_argument('url')
        tags = self.get_argument('tags')

        # TODO The URLField should probably handle this somehow
        if not url.startswith('http'):
            url = 'http://%s' % (url)

        tag_list = tags.split(',')

        link_item = {
            'owner_id': self.current_user.id,
            'owner_username': self.current_user.username,
            'created_at': self.current_time,
            'updated_at': self.current_time,

            'title': title,
            'url': url,
            'tags': tag_list,
        }

        item = ListItem(**link_item)
        try:
            item.validate()
        except Exception, e:
            logging.error('Item validatiom failed')
            logging.error(e)
            return self.render_error(500)
Ejemplo n.º 2
0
    def post(self):
        """Accepts a URL argument and saves it to the database
        """
        title = self.get_argument("title")
        url = self.get_argument("url")
        tags = self.get_argument("tags")

        # TODO The URLField should probably handle this somehow
        if not url.startswith("http"):
            url = "http://%s" % (url)

        tag_list = None
        if tags:
            tag_list = tags.split(",")

        link_item = {
            "owner_id": self.current_user.id,
            "owner_username": self.current_user.username,
            "created_at": self.current_time,
            "updated_at": self.current_time,
            "title": title,
            "url": url,
            "tags": tag_list,
        }

        item = ListItem(**link_item)

        try:
            item.validate()
        except Exception, e:
            logging.error("Item validatiom failed")
            logging.error(e)
            return self.render_error(500)
Ejemplo n.º 3
0
    def post(self):
        """Accepts a URL argument and saves it to the database
        """
        title = self.get_argument('title')
        url = self.get_argument('url')
        tags = self.get_argument('tags')

        # TODO The URLField should probably handle this somehow
        if not url.startswith('http'):
            url = 'http://%s' % (url)

        tag_list = None
        if tags:
            tag_list = tags.split(',')

        link_item = {
            'owner_id': self.current_user.id,
            'owner_username': self.current_user.username,
            'created_at': self.current_time,
            'updated_at': self.current_time,
            'title': title,
            'url': url,
            'tags': tag_list,
        }

        item = ListItem(**link_item)

        try:
            item.validate()
        except Exception, e:
            logging.error('Item validatiom failed')
            logging.error(e)
            return self.render_error(500)
Ejemplo n.º 4
0
def before_request_handler():
    # if not IndustryFirstCategory.table_exists():
    #     IndustryFirstCategory.create_table()
    if not IndustryCategory.table_exists():
        IndustryCategory.create_table()
    if not IndustryThirdCategory.table_exists():
        IndustryThirdCategory.create_table()
    if not Industrys.table_exists():
        Industrys.create_table()
    if not ListItem.table_exists():
        ListItem.create_table()
    if not ListItemTrend.table_exists():
        ListItemTrend.create_table()
Ejemplo n.º 5
0
    def post(self, item_id):
        """Accepts a URL argument and saves it to the database
        """
        item_data = self._load_item(self.current_user.id, item_id)
        item = ListItem(**item_data)

        title = self.get_argument('title')
        url = self.get_argument('url')
        tags = self.get_argument('tags')

        # TODO The URLField should probably handle this somehow
        if not url.startswith('http'):
            url = 'http://%s' % (url)

        tag_list = tags.split(',')

        item.updated_at = self.current_time
        item.title = title
        item.url = url
        item.tags = tag_list

        try:
            item.validate()
        except Exception, e:
            logging.error('Item validatiom failed')
            logging.error(e)
            return self.render_error(500)
Ejemplo n.º 6
0
    def test_adding_list_items(self):
        tl = TrickleList(name='Test List')
        tl.save()
        li = ListItem(name='Test Item', trickle_list=tl)
        li.save()
        self.assertEqual(li.trickle_list.id, tl.id)

        li = tl.listitem_set.create(name='Another test item')
        li.save()
        self.assertEqual(li.trickle_list.id, tl.id)

        self.assertEqual(
            map(lambda i: i.name, tl.listitem_set.all()),
            [u'Test Item', u'Another test item']
        )
Ejemplo n.º 7
0
    def post(self):
        """Renders a JSON list of link data
        """
        ### Stream offset
        updated_offset = self.get_stream_offset()

        ### Load the owner_id's list of items, sorted by `updated_at`.
        items_qs = load_listitems(self.db_conn, owner_id=self.current_user.id, updated_after=updated_offset)
        items_qs.sort("updated_at", direction=pymongo.DESCENDING)
        num_items = items_qs.count()

        ### Page list
        (page, count, skip) = self.get_paging_arguments()

        items_qs.skip(skip)
        items_qs.limit(count)

        ### Generate safe list out of loaded items
        items = [ListItem.make_ownersafe(i) for i in items_qs]

        data = {"num_items": num_items, "items": items}

        self.add_to_payload("data", data)

        return self.render(status_code=200)
Ejemplo n.º 8
0
    def render_list_items(self, user):

        memcache_key = user.nickname() +  '_list_items'        
        template_content = memcache.get(memcache_key)
        
        if template_content is not None:
            return template_content

        query = UserListItems.gql('WHERE user = :user', user=user)
        user_list_items = query.get()
        
        # Use multi-get to pull all list items for this user
        list_items = ListItem.get(user_list_items.list_items)
        
        template_values = {
            'user': user.nickname(),
            'list_items': list_items,
            'view_javascript': 'view'
        }
        path = os.path.join(os.path.dirname(__file__), 'view.html')
        template_content = template.render(path, template_values)
        
        if not memcache.add(memcache_key, template_content):
            logging.error('Memcache: Could not set ' + memcache_key)
            
        return template_content                    
Ejemplo n.º 9
0
    def post(self):
        """Renders a JSON list of link data
        """
        ### Stream offset
        updated_offset = self.get_stream_offset()

        ### Load the owner_id's list of items, sorted by `updated_at`.
        items_qs = load_listitems(self.db_conn,
                                  owner_id=self.current_user.id,
                                  updated_after=updated_offset)
        items_qs.sort('updated_at', direction=pymongo.DESCENDING)
        num_items = items_qs.count()

        ### Page list
        (page, count, skip) = self.get_paging_arguments()

        items_qs.skip(skip)
        items_qs.limit(count)

        ### Generate safe list out of loaded items
        items = [ListItem.make_ownersafe(i) for i in items_qs]

        data = {
            'num_items': num_items,
            'items': items,
        }

        self.add_to_payload('data', data)

        return self.render(status_code=200)
Ejemplo n.º 10
0
    def post(self, item_id):
        """Accepts a URL argument and saves it to the database
        """
        item_data = self._load_item(self.current_user.id, item_id)
        item = ListItem(**item_data)

        title = self.get_argument("title")
        url = self.get_argument("url")
        tags = self.get_argument("tags")

        # TODO The URLField should probably handle this somehow
        if not url.startswith("http"):
            url = "http://%s" % (url)

        tag_list = tags.split(",")

        item.updated_at = self.current_time
        item.title = title
        item.url = url
        item.tags = tag_list

        try:
            item.validate()
        except Exception, e:
            logging.error("Item validatiom failed")
            logging.error(e)
            return self.render_error(500)
Ejemplo n.º 11
0
def api_new():
    title = request.form.get('item', None)
    list_id = request.form.get('list-id', None)
    if title and list_id:
        list = db.session.query(List).filter_by(id=list_id).first()
        if list:
            newitem = ListItem(title=title, list_id=list.id)
            db.session.add(newitem)
            db.session.commit()
            return jsonify(list_id=list_id, title=title, id=newitem.id)
Ejemplo n.º 12
0
    def post(self):
        user = users.get_current_user()
        
        # We require a valid user
        if not user:
            self.redirect(users.create_login_url(self.request.uri))
    
        list_item = ListItem()
        
        list_item.author = user
        list_item.title = self.request.get('title')
        list_item.details = self.request.get('details')

        # If we were able to save, get rid of the template cache
        if list_item.put():
            memcache_key = user.nickname() +  '_list_items'            
            memcache.delete(memcache_key)

        self.redirect('/view')
Ejemplo n.º 13
0
    def get(self):
        """Renders a template with our links listed
        """
        items_qs = load_listitems(self.db_conn, self.current_user.username)
        items_qs.sort('updated_at', direction=pymongo.DESCENDING)

        items = [ListItem(i).to_primitive(role='owner') for i in items_qs]
        context = {
            'links': items,
        }
        return self.render_template('linklists/link_list.html', **context)
Ejemplo n.º 14
0
    def post(self):
        """Accepts a URL argument and saves it to the database
        """
        url = self.get_argument("url")

        if not url.startswith("http"):
            url = "http://%s" % (url)

        logging.info(self.current_user.to_primitive())
        link_item = {
            "owner": self.current_user.id,
            "username": self.current_user.username,
            "created_at": datetime.utcnow(),
            "updated_at": datetime.utcnow(),
            "url": url,
        }

        item = ListItem(link_item)
        item.validate()
        save_listitem(self.db_conn, item)
        return self.redirect("/list")
Ejemplo n.º 15
0
    def post(self):
        """Accepts a URL argument and saves it to the database
        """
        url = self.get_argument('url')

        if not url.startswith('http'):
            url = 'http://%s' % (url)
            
        link_item = {
            'owner': self.current_user.id,
            'username': self.current_user.username,
            'created_at': self.current_time,
            'updated_at': self.current_time,
            'url': url,
        }

        item = ListItem(**link_item)
        item.validate()
        save_listitem(self.db_conn, item)
            
        return self.redirect('/')
Ejemplo n.º 16
0
    def post(self):
        """Accepts a URL argument and saves it to the database
        """
        url = self.get_argument('url')

        if not url.startswith('http'):
            url = 'http://%s' % (url)

        logging.info(self.current_user.to_primitive())
        link_item = {
            'owner': self.current_user.id,
            'username': self.current_user.username,
            'created_at': datetime.utcnow(),
            'updated_at': datetime.utcnow(),
            'url': url,
        }

        item = ListItem(link_item)
        item.validate()
        save_listitem(self.db_conn, item)
        return self.redirect('/list')
Ejemplo n.º 17
0
def new():
    title = request.form.get('item', None)
    list_id = request.form.get('list-id', None)
    if title and list_id:
        list = db.session.query(List).filter_by(id=list_id).first()
        if list:
            newitem = ListItem(title=title, list_id=list.id)
            db.session.add(newitem)
            db.session.commit()
            flash('Success! Added \'%s\' to \'%s\'' % (title, list.title),
                  'success')
            return redirect(url_for('index'))
    flash('An error occurred', 'danger')
    return redirect(url_for('index'))
Ejemplo n.º 18
0
    def prepare_items(self, query_set, sort_field='updated_at'):
        query_set.sort(sort_field, direction=pymongo.DESCENDING)

        items = []
        for i in query_set:
            item_id = i['_id']
            item = ListItem.make_ownersafe(i)

            updated = millis_to_datetime(item['updated_at'])
            formatted_date = prettydate(updated)
            item['formatted_date'] = formatted_date

            item['id'] = item_id
            items.append(item)

        return items
Ejemplo n.º 19
0
    def prepare_items(self, query_set, sort_field="updated_at"):
        query_set.sort(sort_field, direction=pymongo.DESCENDING)

        items = []
        for i in query_set:
            item_id = i["_id"]
            item = ListItem.make_ownersafe(i)

            updated = millis_to_datetime(item["updated_at"])
            formatted_date = prettydate(updated)
            item["formatted_date"] = formatted_date

            item["id"] = item_id
            items.append(item)

        return items
Ejemplo n.º 20
0
    def get(self):
        """Renders a template with our links listed
        """
        items_qs = load_listitems(self.db_conn, self.current_user.id)
        items_qs.sort('updated_at', direction=pymongo.DESCENDING)
        num_items = items_qs.count()
        
        items = [ListItem.make_ownersafe(i) for i in items_qs]

        data = {
            'num_items': num_items,
            'items': items,
        }

        self.set_body(json.dumps(data))
        return self.render(status_code=200)
Ejemplo n.º 21
0
    def get(self):
        """Renders a template with our links listed
        """
        items_qs = load_listitems(self.db_conn, self.current_user.id)
        items_qs.sort('updated_at', direction=pymongo.DESCENDING)
        num_items = items_qs.count()
        
        items = [ListItem.make_ownersafe(i) for i in items_qs]

        data = {
            'num_items': num_items,
            'items': items,
        }

        self.set_body(json.dumps(data))
        return self.render(status_code=200)
Ejemplo n.º 22
0
def add_item(request):
	name_for_item = request.POST.get('name_for_item')
	item_datetime = datetime.datetime.utcnow()
	item_status = "incomplete"
	new_item_entry = ListItem()
	new_item_entry.item = name_for_item
	new_item_entry.datetime = item_datetime
	new_item_entry.status = item_status
	new_item_entry.save()
	current_list = ListItem.objects.order_by('item')
	return render(request, 'ordered_to_do_list_app/index.html', { 'current_list' : current_list })
Ejemplo n.º 23
0
 def get(self):
     """Renders a JSON response
     """
     paging_arguments = self.get_paging_arguments()
     total = load_listitems(self.db_conn,
                            self.current_user.username).count()
     items_qs = page_listitems(self.db_conn, self.current_user.username,
                               **paging_arguments)
     items_qs.sort('updated_at', direction=pymongo.DESCENDING)
     num_items = items_qs.count()
     response = {
         'paging': {
             'page': paging_arguments['page'],
             'count': paging_arguments['count'],
             'total': total
         },
         'items':
         [ListItem(i).to_primitive(role='owner') for i in items_qs]
     }
     self.set_body(json.dumps(response))
     return self.render(status_code=200)
Ejemplo n.º 24
0
def newListItem(category_id):
    if 'username' not in login_session:
        return redirect('/login')
    categories = session.query(Category)
    if request.method == 'POST':
        category_name = request.form['category']
        category = session.query(Category).filter_by(name=category_name).one()
        newItem = ListItem(name=bleach.clean(request.form['name']),
                           description=bleach.clean(
                               request.form['description']),
                           category_id=category.id,
                           user_id=login_session['user_id'])
        session.add(newItem)
        session.commit()
        flash('New Item %s Successfully Created' % (newItem.name))
        return redirect(
            url_for('showList', categories=categories,
                    category_id=category.id))
    else:
        return render_template('newlistitem.html',
                               categories=categories,
                               category_id=category_id)
Ejemplo n.º 25
0
    def create_list_item(self, list_id, item_name, raw=False):
        """
        Create an item associated with a list.

        :param list_id: list ID
        :type list_id: basestring
        :param item_name: item name
        :type item_name: basestring
        :param raw: whether to return raw response body
        :type raw: bool
        :return: raw item or model instance
        :rtype: dict | ListItem
        """
        url = "%s/%s/%s/%s" % (self.BASE_URL, 'lists', list_id, 'items')

        data = {"item": {"name": item_name}}

        serialized = self.make_request(requests.post,
                                       url,
                                       data=json.dumps(data)).json()
        if raw:
            return serialized
        return ListItem.from_dict(serialized)
Ejemplo n.º 26
0
    def get_lists(self, raw=False, include_items=False):
        """
        Get lists.

        :param raw: whether to return raw response body
        :type raw: bool
        :param include_items: whether to include items associated with list
        :type include_items: bool
        :return: the lists, either raw or model instances
        :rtype: [dict] | [List]
        """
        url = "%s/%s" % (self.BASE_URL, 'lists')
        serialized = self.make_request(requests.get, url).json()

        if raw:
            return serialized

        lists = [List.from_dict(l) for l in serialized['lists']]
        if include_items:
            for todo_list in lists:
                items = self.get_list(todo_list.id, raw=True)['items']
                todo_list.items = [ListItem.from_dict(i) for i in items]
        return lists
Ejemplo n.º 27
0
 def post(self):
   user = users.get_current_user()
   if user:
     checklist = Checklist.get(self.request.get('checklist_key'))
     listitem = ListItem(
         title=self.request.get('title'),
         assigner=user,
         assignees=[user],
         priority=int(self.request.get('priority')),
         finished=bool(self.request.get('finished')),
         checklist=checklist)
     
     if self.request.get('link') != '':
       listitem.link = self.request.get('link')
     if self.request.get('deadline') != '':
       # TODO: replace this with deadline
       listitem.deadline=datetime.now()
     if self.request.get('details') != '':
       listitem.details=self.request.get('detail')
        
     listitem.put()
   self.redirect('/')
Ejemplo n.º 28
0
    def save_list_item_trend(self, items):
        if not isinstance(items, list):
            return
        else:
            for item in items:
                shop_name = item['shop_name']
                item_category = item['item_category']
                item_category_id = item['item_category_id']
                item_title = item['item_title']
                item_id = item['item_id']
                item_price = item['item_price']
                item_url = item['item_url']
                logger.debug(
                    '\033[92m 保存商品店铺信息: shop_name:{0}, item_title:{1} \033[0m'.
                    format(shop_name, item_title))
                # 这里还需要对这三组list数据做对应日期映射
                pay_item_qtylist = item['pay_item_qtylist']
                pay_ord_cntlist = item['pay_ord_cntlist']
                pay_byr_rate_index_list = item['pay_byr_rate_index_list']

                try:
                    list_item, created = ListItem.get_or_create(
                        item_title=item_title,
                        item_id=item_id,
                        shop_name=shop_name,
                        defaults={
                            'item_category': item_category,
                            'item_category_id': item_category_id,
                            'item_price': item_price,
                            'item_url': item_url,
                        })
                    if not created:
                        list_item.item_price = item_price
                        list_item.item_url = item_url
                        list_item.item_category = item_category
                        list_item.item_category_id = item_category_id
                        list_item.save()

                    #import pdb
                    #pdb.set_trace()
                    #cursor = mysql_db.execute_sql('select count(*) from list_item_trend')
                    #res = cursor.fetchone()
                    ## 第一次将数据写入到ListItemTrend表时应该写入所有pay_*数据:
                    #if res[0] == 0:
                    if not len(pay_item_qtylist) \
                            == len(pay_ord_cntlist) \
                            == len(pay_byr_rate_index_list) == 30:
                        logger.warning(
                            '\033[94m please check the list data !!!\033[0m')
                    else:
                        date_list = get_30_date_before_today()
                        for num, old_date in enumerate(date_list):
                            data_mapping_date = old_date
                            pay_item_qty = pay_item_qtylist[num]
                            pay_ord_cnt = pay_ord_cntlist[num]
                            pay_byr_rate_index = pay_byr_rate_index_list[num]
                            logger.debug(
                                '\033[96m 保存商品趋势信息: date:{0}, 所有终端-支付子订单数:{1} , 所有终端-支付转化率指数:{2} ,所有终端-支付件数:{3}\033[0m'
                                .format(data_mapping_date, pay_item_qty,
                                        pay_ord_cnt, pay_byr_rate_index))
                            list_item_trend, created = ListItemTrend.get_or_create(
                                list_item=list_item,
                                data_mapping_date=data_mapping_date,
                                defaults={
                                    'pay_item_qty':
                                    pay_item_qty if pay_item_qty else 0,
                                    'pay_ord_cnt':
                                    pay_ord_cnt if pay_ord_cnt else 0,
                                    'pay_byr_rate_index':
                                    pay_byr_rate_index
                                    if pay_byr_rate_index else 0,
                                })
                            if not created:
                                list_item_trend.pay_item_qty = pay_item_qty
                                list_item_trend.pay_ord_cnt = pay_ord_cnt
                                list_item_trend.pay_byr_rate_index = pay_byr_rate_index
                                list_item_trend.save()
                except Exception as e:
                    logger.error('\033[92m {} \033[0m'.format(e))
Ejemplo n.º 29
0
 def setUp(self):
     self.list_item = ListItem()