Beispiel #1
0
 def post(self):
     try:
         data = json.loads(self.request.body.decode())
         code = data['eventcode']
         openid = data['openid']
         star = None
         try:
             star = yield dbutil.do(
                 Star.select().where((Star.eventcode == code)
                                     & (Star.openid == openid)).get)
         except:
             pass
         if star:
             if star.status == 1:  # 已经关注
                 raise RuntimeError
             else:  # 已经取消关注
                 star.status = 1
                 star.updatetime = datetime.datetime.now()
         else:  # 首次关注
             results = yield [
                 dbutil.do(
                     Event.select(
                         Event.id,
                         Event.code).where(Event.code == code).get),
                 dbutil.do(
                     User.select(
                         User.id,
                         User.openid).where(User.openid == openid).get)
             ]
             event = results[0]
             user = results[1]
             star = Star(eventid=event.get_id(),
                         eventcode=event.code,
                         userid=user.get_id(),
                         openid=user.openid,
                         createtime=datetime.datetime.now(),
                         status=1)
         yield dbutil.do(star.save)
         self.write(Response(status=1, msg='ok', result={}).json())
     except RuntimeError as e:
         self.write(Response(msg='sorry,亲,用户已经关注该活动').json())
         logging.exception('StarEventHandler error: {0}'.format(str(e)))
     except Exception as e:
         self.write(Response(msg='sorry,亲,关注活动失败').json())
         logging.exception('StarEventHandler error: {0}'.format(str(e)))
Beispiel #2
0
 def post(self):
     try:
         data = json.loads(self.request.body.decode())
         code = data['eventcode']
         openid = data['openid']
         star = yield dbutil.do(Star.select().where(
             (Star.eventcode == code) & (Star.openid == openid)
         ).get)
         star.status = -1
         star.updatetime = datetime.datetime.now()
         yield dbutil.do(star.save)
         self.write(Response(status=1, msg='ok', result={}).json())
     except Exception as e:
         self.write(Response(msg='sorry,亲,取消关注活动失败').json())
         logging.exception('UnStarEventHandler error: {0}'.format(str(e)))
Beispiel #3
0
def click_brightness_curve(file, center, star):
    """Simple program that prints the input variables"""
    click.echo(file.name)
    file_data = open(file.name, "rb").read()

    click.echo('(x: %d, y: %d) - radius: %d' % center)
    click.echo('(x: %d, y: %d) - length: %d, width: %d' % star)

    center_model = Point(center[0], center[1], center[2])
    star_model = Star(star[0], star[1], star[2], star[3])

    brightness_curve = BrightnessCurve(io.BytesIO(file_data), True,
                                       center_model, star_model)
    brightness_curve.calculate()
    click.echo(brightness_curve.json())
Beispiel #4
0
 def post(self):
     try:
         data = json.loads(self.request.body.decode())
         code = data['eventcode']
         openid = data['openid']
         star = yield dbutil.do(
             Star.select().where((Star.eventcode == code)
                                 & (Star.openid == openid)).get)
         star.status = -1
         star.updatetime = datetime.datetime.now()
         yield dbutil.do(star.save)
         self.write(Response(status=1, msg='ok', result={}).json())
     except Exception as e:
         self.write(Response(msg='sorry,亲,取消关注活动失败').json())
         logging.exception('UnStarEventHandler error: {0}'.format(str(e)))
Beispiel #5
0
    def __init__(self, db, x, y, z):
        cursor = db.cursor()
        cursor.execute(QUERY, (x, y, z))
        all_stars = [Star(*row) for row in cursor.fetchall()]
        # all_tree = Tree(all_stars)

        neutron_stars = [
            star for star in all_stars if star.distance_to_neutron is not None
        ]
        # neutron_star_scoopable_neighbors = list(
        #     filter(
        #         lambda star: star.distance_to_scoopable is not None,
        #         itertools.chain.from_iterable(
        #             [all_tree.get_neighbors(star, 15) for star in neutron_stars])
        #     )
        # )

        # stars = list(set(neutron_stars + neutron_star_scoopable_neighbors))
        stars = neutron_stars
        self._tree = Tree(stars)
Beispiel #6
0
 def post(self):
     try:
         data = json.loads(self.request.body.decode())
         code = data['eventcode']
         openid = data['openid']
         star = None
         try:
             star = yield dbutil.do(Star.select().where(
                 (Star.eventcode == code) & (Star.openid == openid)
             ).get)
         except:
             pass
         if star:
             if star.status == 1:  # 已经关注
                 raise RuntimeError
             else:  # 已经取消关注
                 star.status = 1
                 star.updatetime = datetime.datetime.now()
         else:  # 首次关注
             results = yield [
                 dbutil.do(Event.select(Event.id, Event.code).where(Event.code == code).get),
                 dbutil.do(User.select(User.id, User.openid).where(User.openid == openid).get)
             ]
             event = results[0]
             user = results[1]
             star = Star(
                 eventid=event.get_id(),
                 eventcode=event.code,
                 userid=user.get_id(),
                 openid=user.openid,
                 createtime=datetime.datetime.now(),
                 status=1
             )
         yield dbutil.do(star.save)
         self.write(Response(status=1, msg='ok', result={}).json())
     except RuntimeError as e:
         self.write(Response(msg='sorry,亲,用户已经关注该活动').json())
         logging.exception('StarEventHandler error: {0}'.format(str(e)))
     except Exception as e:
         self.write(Response(msg='sorry,亲,关注活动失败').json())
         logging.exception('StarEventHandler error: {0}'.format(str(e)))
Beispiel #7
0
 def post(self):
     openid = None
     try:
         request = json.loads(self.request.body.decode())
         openid = request.get('openid', None)
         results = yield dbutil.do(
             Star.select().where((Star.openid == openid)
                                 & (Star.status == 1)).dicts)
         stars = [star for star in results]
         self.write(Response(status=1, msg='ok', result=stars).json())
     except AttributeError as e:
         self.write(Response(msg='sorry,亲,修改用户信息时必须包含openid').json())
         logging.exception('UpdateUserInfoHandler error: {0}'.format(
             str(e)))
     except DoesNotExist as e:
         self.write(
             Response(msg='sorry,亲,用户不存在 openid={0}'.format(openid)).json())
         logging.exception('UpdateUserInfoHandler error: {0}'.format(
             str(e)))
     except Exception as e:
         self.write(Response(msg='sorry,亲,修改用户信息失败').json())
         logging.exception('UpdateUserInfoHandler error: {0}'.format(
             str(e)))
Beispiel #8
0
 def get_by_name(self, name):
     cursor = self._db.cursor()
     cursor.execute(QUERY, (name, ))
     return [Star(*row) for row in cursor.fetchall()][0]
Beispiel #9
0
def astrograph_brightness_curve():
    if request.method == 'POST':
        # check if the post request has the file part
        if 'file' not in request.files:
            logging.error("brightness_curve: no file attached")
            flash('No file part')
            return redirect(request.url)
        file = request.files['file']
        # if user does not select file, browser also
        # submit an empty part without filename
        if file.filename == '':
            flash('No selected file')
            logging.error("brightness_curve: empty or invalid file attached")
            return redirect(request.url)
        if file and allowed_file(file.filename):
            info = json.loads(request.form.get('parameters'))

            reference = Point(info['reference']['x'], info['reference']['y'],
                              info['reference']['radius'])
            star = Star(info['star']['x'], info['star']['y'],
                        info['star']['line']['length'],
                        info['star']['line']['width'])

            brightness_curve = BrightnessCurve(
                io.BytesIO(file.read()),
                get_file_extension(file.filename) != 'tiff', reference, star)
            logging.info("calculating...")
            brightness_curve.calculate()
            logging.info("calculating done.")

            if "preview" in info and info["preview"] is True:
                logging.debug("brightness_curve: returning debug image")
                jpeg = io.BytesIO()
                imageio.imwrite(jpeg,
                                brightness_curve.raw_data,
                                format="JPEG",
                                quality=100,
                                optimize=True)

                proxy = io.BytesIO()
                proxy.write(jpeg.getvalue())
                # seeking was necessary. Python 3.5.2, Flask 0.12.2
                proxy.seek(0)
                jpeg.close()

                return send_file(proxy,
                                 mimetype='image/jpeg',
                                 as_attachment=True,
                                 attachment_filename='result.jpeg')

            if "csv" in info and info["csv"] is True:
                logging.debug("brightness_curve: returning .csv")
                # return send_file(brightness_curve.csv())
                return Response(brightness_curve.csv(),
                                mimetype="text/csv",
                                headers={
                                    "Content-Disposition":
                                    "attachment;filename=test.csv"
                                })
            else:
                logging.debug("brightness_curve: returning .json")
                return brightness_curve.json()
    return 'Invalid parameters.'