def add(cid: int, tel: str):
    coupon_res = coupon.get_by_id(cid)
    if not coupon_res:
        raise error.CouponNotFoundError(cid)
    telephone = {'tel': tel, 'coupon_id': cid}
    result = db.execute(Telephone.insert().values(**telephone))
    return result.inserted_primary_key
    async def post(self, *, cid: str, tel: str):
        validator.check_tel(tel)
        coupon_row = coupon.get_by_uuid(cid)
        if not coupon_row:
            raise error.CouponNotFoundError(cid)
        tel_id = telephone.add(coupon_row['id'], tel)
        coupon.inc_submit(cid)

        self.render('coupon_success.html')
 async def get(self, *, cid: str):
     coupon_row = coupon.get_by_uuid(cid)
     if not coupon_row:
         raise error.CouponNotFoundError(cid)
     coupon.inc_view(cid)
     img_url = coupon_row['img']
     self.render('coupon_detail.html',
                 cid=cid,
                 img_url=img_url,
                 page_title=coupon_row['title'],
                 receive_text=coupon_row['receive_text'],
                 custom_style=coupon_row['custom_style'])
    async def post(self, cid: str):
        coupon_row = coupon.get_by_uuid(cid)
        if not coupon_row:
            raise error.CouponNotFoundError(cid)
        data = {}
        async for part in await self.request.multipart():
            if part.name != 'img':
                content = (await part.read()).decode()
                if content != '':
                    data[part.name] = content
            elif part.filename != '':
                data['img'] = await self.handle_img_upload(part)

        if 'title' not in data:
            raise error.ValidationError('title')
        try:
            cid = coupon.edit(cid, **data)
        except TypeError:
            raise error.UnknownArgumentError()
        self.redirect(self.reverse_url('coupon_detail', cid=cid))
 async def get(self, *, cid: str):
     coupon_row = coupon.get_by_uuid(cid)
     if not coupon_row:
         raise error.CouponNotFoundError(cid)
     self.render('coupon_create.html', **coupon_row, edit=True)