Exemplo n.º 1
0
    def get(self, play_store_id):
        start_time = time.time()
        app = App.for_play_store_id(play_store_id).first()
        if not app:
            try:
                res = requests.get(
                    'https://play.google.com/store/apps/details?id={}'.format(
                        play_store_id))
                if (res.status_code != 200):
                    return Response.error(
                        'No app found for app id {}'.format(play_store_id),
                        404)
            except requests.exceptions.ConnectionError:
                return Response.error(
                    'Unable to fetch app from playstore, please try again after sometime',
                    500)

            app = App()
            app.parse_html_and_save(res.text, play_store_id)

        if not app.reviews.count() > 0:
            app.parse_and_save_reviews()

        args = reqparse.RequestParser().add_argument(
            'page_num', type=int, required=False).parse_args()
        return Response.pagination(
            'Reviews Delivered in {} seconds'.format(
                round(time.time() - start_time, 2)),
            Review.for_play_store_id(play_store_id).with_author(),
            args.get('page_num') or 1, 40)
Exemplo n.º 2
0
	def post(self):
		try:
			args = self.reqparse.parse_args()

			email = args['email']
			query = db_session.query(models.User).filter(models.User.email == email)
			user = query.one_or_none()

			if user is not None:
				current_app.logger.warning("Already register id : " + email)
				return Response.error(error_code.Login.ALREADY_REGISTER)

			new_user = models.User(code=generate_uuid(), email=email, password_hash=sha256_crypt.encrypt(str(args['password'])), user_name=args['user_name'], role_id=args['role'],
								   room=args['room'], phone=args['phone'])
			db_session.add(new_user)
			db_session.commit()

			current_app.logger.info("Register new user : "******"/auth/confirm/" + generate_uuid()

			confirm = models.Confirm(email=email, type=models.Confirm_type.CONFIRM_REGISTER.name, url=url)
			db_session.add(confirm)
			db_session.commit()

			mail.make_confirm_email(email, url)

			return Response.ok()

		except Exception as e:
			current_app.logger.error(str(e))
			return Response.error(error_code.Global.UNKOWN)
Exemplo n.º 3
0
	def post(self):
		try:
			args = self.reqparse.parse_args()

			email = args['id']
			query = db_session.query(models.User).filter(models.User.email == email)
			user = query.one_or_none()

			if user is None:
				# return {'status': 'error', 'code': error_code.Login.NOT_FOUND_ID, 'message': error_code.Login.NOT_FOUND_ID.message()}
				return Response.error(error_code.Login.NOT_FOUND_ID)

			if not sha256_crypt.verify(str(args['password']), user.password_hash):
				# return {'status': 'error', 'code': error_code.Login.WRONG_PASSWORD, 'message': error_code.Login.WRONG_PASSWORD.message()}
				return Response.error(error_code.Login.WRONG_PASSWORD)

			if user.confirmed is False:
				return Response.error(error_code.Login.NOT_CONFORMED)

			user.last_seen = datetime.now()
			db_session.commit()

			session.clear()
			session[Session.LOGIN_SESSION] = sha256_crypt.encrypt(str(user.code + user.password_hash))
			session[Session.USER_SESSION] = user.code
			session[Session.ADMIN_SESSION] = decorators.is_admin(user.code)

			current_app.logger.info("Login - " + email)

			return Response.ok()
		except Exception as e:
			current_app.logger.error(str(e))
			return Response.error(error_code.Global.UNKOWN)
Exemplo n.º 4
0
def runWithErrorHandling(fn):
    try:
        return Response.ok(fn())
    except BaseError as error:
        raise Response.error(error.code, error.message)
    except AzureError as error:
        raise Response.error(400, 'Get Azure error: {}'.format(error))
    except BaseException as error:
        raise Response.error(500, 'Unhandled exception: {}.'.format(error))
Exemplo n.º 5
0
	def delete(self, index):
		try:
			db_session.query(Notice).filter(Notice.index == index).delete()

			return Response.ok()
		except Exception as e:
			current_app.logger.error(e)
			return Response.error()
Exemplo n.º 6
0
	def delete(self, code):
		try:
			db_session.query(models.User).filter(models.User.code == code).delete()

			return Response.ok()
		except Exception as e:
			current_app.logger.error(str(e))
			return Response.error(error_code.Global.UNKOWN)
Exemplo n.º 7
0
	def get(self):
		room_list = []
		try:
			for room, is_living in db_session.query(models.Room.room, models.Room.is_living).order_by(desc(models.Room.room)):
				room_list.append({'room': room, 'is_living': is_living})

			return Response.ok(room_list)
		except Exception as e:
			current_app.logger.error(str(e))
			return Response.error(error_code.Global.UNKOWN)
Exemplo n.º 8
0
	def get(self):
		role_list = []
		try:
			for id, name in db_session.query(models.Role.id, models.Role.name).order_by(desc(models.Role.id)):
				role_list.append({'id': id, 'name': name})

			# return role_list
			return Response.ok(role_list)
		except Exception as e:
			current_app.logger.error(str(e))
			return Response.error(error_code.Global.UNKOWN)
Exemplo n.º 9
0
def nmap(host, num=0):
    '''
    This function uses xmltodict

    <mydocument has="an attribute">
    <and>
        <many>elements</many>
        <many>more elements</many>
    </and>
    <plus a="complex">
        element as well
    </plus>
    </mydocument>

    doc['mydocument']['@has'] # == u'an attribute'
    doc['mydocument']['and']['many'] # == [u'elements', u'more elements']
    doc['mydocument']['plus']['@a'] # == u'complex'
    doc['mydocument']['plus']['#text'] # == u'element as well'
    '''
    response = Response()
    p = multiprocessing.current_process()
    print(p.name, p.pid)
    a = 'nmap -O {h} -oX -'.format(h=host)
    print(host)
    try:
        r = subprocess.check_output(a)
    except Exception as error:
        response.success = False
        response.error = str(error)
        return response

    try:
        doc = xmltodict.parse(r)
        response.result = doc
    except Exception as error:
        response.success = False
        response.error = str(error)
        return response
    time.sleep(num)

    return response
Exemplo n.º 10
0
    def get(self, play_store_id):
        app_found = App.for_play_store_id(play_store_id).first()
        if (app_found):
            return Response.model('Delivered', app_found)

        try:
            res = requests.get(
                'https://play.google.com/store/apps/details?id={}'.format(
                    play_store_id))
            if (res.status_code != 200):
                return Response.error(
                    'No app found for app id {}'.format(play_store_id), 404)
        except requests.exceptions.ConnectionError:
            return Response.error(
                'Unable to fetch app from playstore, please try again after sometime',
                500)

        new_app = App()
        new_app.parse_html_and_save(res.text, play_store_id)

        return Response.model('Delivered', new_app)
Exemplo n.º 11
0
	def put(self, code):
		try:
			args = self.reqparse.parse_args()

			room = db_session.query(models.Room).options(joinedload(models.Room.user)).filter(models.Room.room == args['room'] and models.User.code == code).one()

			room.is_living = args['is_living']
			db_session.commit()

			return Response.ok(room)
		except Exception as e:
			current_app.logger.error(str(e))
			return Response.error(error_code.Global.UNKOWN)
Exemplo n.º 12
0
	def get(self, code=None):
		try:
			if code is None:
				#check admin auth
				list = db_session.query(models.User).options(joinedload(models.User.role)).options(joinedload(models.User.room_info)).order_by(asc(models.User.email)).all()

				return Response.ok(list)
			else:
				user = db_session.query(models.User).options(joinedload(models.User.role)).options(joinedload(models.User.room_info)).filter(models.User.code == code).one()

				return Response.ok(user)
		except Exception as e:
			current_app.logger.error(str(e))
			return Response.error(error_code.Global.UNKOWN)
Exemplo n.º 13
0
	def put(self, index):
		try:
			# parser = fields.get_notice_parser()
			# args = parser.parse_args()
			args = self.reqparse.parse_args()

			notice = db_session.query(Notice).filter(Notice.index == index).one()
			notice.author_id = db_session.query(User.email).filter(User.email == session.get('email')).one()
			notice.title = args['title']
			notice.content = args['content']

			db_session.commit()

			return Response.ok()
		except Exception as e:
			current_app.logger.error(e)
			return Response.error()
Exemplo n.º 14
0
	def get(self):
		try:
			user = session.get(Session.USER_SESSION)

			bill = []
			for target_date, electric_usage, electric_amount, gas_usage, gas_amount, description in db_session.query(models.Bill.target_date, models.Bill.electric_usage,
																													 models.Bill.electric_amount,
																													 models.Bill.gas_usage, models.Bill.gas_amount,
																													 models.Bill.description).filter(
						models.Bill.author_id == user).all():
				date = '{:%Y %m}'.format(target_date)
				bill.append({'date': date, 'electric': {'usage': electric_usage, 'amount': electric_amount}, 'gas': {'usage': gas_usage, 'amount': gas_amount},
							 'description': description})

			return Response.ok(bill)
		except Exception as e:
			current_app.logger.error(str(e))
			return Response.error(error_code.Global.UNKOWN)
Exemplo n.º 15
0
	def get(self, year=None, month=None):
		try:
			if year is None or month is None:
				date = db_session.query(models.Bill.target_date).order_by(desc(models.Bill.target_date)).first()
			else:
				date = datetime.datetime(year, month, 1)

			bill = []
			for month_bill in db_session.query(models.Bill).filter(models.Bill.target_date == date).all():
				bill.append(
					{'date': '{:%Y %m}'.format(date), 'room': month_bill.room, 'electric': {'usage': month_bill.electric_usage, 'amount': month_bill.electric_amount},
					 'gas': {'usage': month_bill.gas_usage, 'amount': month_bill.gas_amount},
					 'description': month_bill.description})

			return Response.ok(bill)
		except Exception as e:
			current_app.logger.error(str(e))
			return Response.error(error_code.Global.UNKOWN)
Exemplo n.º 16
0
    def nmap_str(cls):
        start = datetime.now()
        response = Response()

        a = 'nmap -O {h}'.format(h=cls.host)
        try:
            r = subprocess.check_output(a)
            response.result = r
        except Exception as error:
            response.success = False
            response.error = str(error)
            return response

        end = datetime.now()
        response.execution_time = end - start

        if response.success:
            return response.result
        return response.error
Exemplo n.º 17
0
	def post(self, year, month):
		try:
			args = self.reqparse.parse_args()

			for bill in args['bill']:
				user = db_session.query(models.User.code).filter(models.User.room == bill['room']).one_or_none()

				user_bill = models.Bill(target_date=datetime.datetime(year, month, 1), room=bill['room'], author_id=user, electric_usage=bill['electric']['usage'],
										electric_amount=bill['electric']['amount'], gas_usage=bill['gas']['usage'], gas_amount=bill['gas']['amount'],
										description=bill['description'])
				db_session.add(user_bill)

			db_session.commit()
			return Response.ok()

		except Exception as e:
			db_session.rollback()
			current_app.logger.error(str(e))
			return Response.error(error_code.Global.UNKOWN)
Exemplo n.º 18
0
	def put(self, code):
		try:
			args = self.reqparse.parse_args()

			user = db_session.query(models.User).options(joinedload(models.User.role)).options(joinedload(models.User.room_info)).filter(models.User.code == code).one()

			if args['password'] is not None:
				user.password_hash = sha256_crypt.encrypt(str(args['password']))
			if args['confirmed'] is not None:
				user.confirmed = args['confirmed']
			if args['user_name'] is not None:
				user.user_name = args['user_name']
			if args['phone'] is not None:
				user.phone = args['phone']

			db_session.commit()

			return Response.ok(user)
		except Exception as e:
			current_app.logger.error(str(e))
			return Response.error(error_code.Global.UNKOWN)
Exemplo n.º 19
0
	def post(self):
		try:
			# parser = fields.get_notice_parser().copy()
			args = self.reqparse.parse_args()

			title = args['title']
			content = args['content']
			# email = session.get('email')
			code = session.get(Session.USER_SESSION)

			# get json from request BUT title and content is null when they was inserted
			user = db_session.query(User.code).filter(User.code == code).one()
			new_notice = Notice(title=title, content=content, author_id=user)

			db_session.add(new_notice)
			db_session.commit()

			return Response.ok()
		except Exception as e:
			current_app.logger.error(e)
			return Response.error()