Beispiel #1
0
def new_experiment(url, name):
    raw = get_dataset_from_url(url)

    with database.atomic():
        experiment = Experiment.create(source_url=url, name=name)
        Data.bulk_create([Data(**r, experiment=experiment) for r in raw])
        return experiment
Beispiel #2
0
def save_data_to_db(rssi_file, position_file, session_file):
    rssi_data = list(csv.reader(open(rssi_file, 'r')))
    position_data = list(csv.reader(open(position_file, 'r')))

    with open(session_file, 'r') as f:
        session_name, session_date = [x.rstrip() for x in f]

    with database.atomic():
        sess = CaptureSession()
        sess.name = session_name
        sess.date = session_date
        sess.save()
        for row in rssi_data[1:]:
            rssi = RSSIValue()
            rssi.beacon_uuid = row[0]
            rssi.beacon_major = int(row[1])
            rssi.beacon_minor = int(row[2])
            rssi.rssi = int(row[3])
            rssi.timestamp = int(row[4])
            rssi.capture_session = sess
            rssi.save()

        for row in position_data[1:]:
            pos = Position()
            pos.x = float(row[0])
            pos.y = float(row[1])
            pos.timestamp = int(row[2])
            pos.capture_session = sess
            pos.save()
Beispiel #3
0
    def insert_data(self, model, rows):
        """Insert data into database.
        :param model: The model used to insert the data. Usually synonymous with the table.
        :param rows: Data to insert as a list of dictionaries with keys that match
        table field names.
        :return: List of all object ids inserted and/or returned.
        :raises: DataInserterError - If parameter is None.
        """
        if model is None:
            raise DataInserterError("Model cannot be none.")
        if rows is None:
            raise DataInserterError("Data rows cannot be none.")

        key_ids = []
        for row in rows:
            with database.atomic():
                try:
                    inserted_object, is_created = model.get_or_create(**row)
                    key_ids.append(inserted_object.id)
                except IntegrityError as ex:
                    self._log.error('Integrity error occurred: {}'.format(ex))
                    self._log.error('\t{}'.format(row))
                else:
                    if is_created:
                        self._log.info('Data inserted:')
                        self._log.info('\t{}'.format(row))
                    else:
                        self._log.warn('Data already present in database, not inserted:')
                        self._log.warn('\t{}'.format(row))
        return key_ids
Beispiel #4
0
def populate_test_data():
    with database:
        database.drop_tables([Department, Person])
        database.create_tables([Department, Person])

    dpts = [('Бухгалтерия',), ('Отдел автоматизации',), ('Отдел продаж',)]

    with database.atomic():
        Department.insert_many(dpts, fields=[Department.name]).execute()
        Person.insert_many(get_persons()).execute()
Beispiel #5
0
    def patch(self, order_uuid):
        """ Modify a specific order. """
        res = request.get_json(force=True)

        errors = Order.validate_input(res, partial=True)
        if errors:
            return errors, BAD_REQUEST

        data = res['data']['relationships']
        req_items = data.get('items', {})
        req_address = data.get('delivery_address')

        with database.atomic():
            try:
                order = Order.get(uuid=str(order_uuid))
            except Order.DoesNotExist:
                abort(NOT_FOUND)

            address = None
            if req_address:
                try:
                    address = Address.get(
                        Address.uuid == req_address['data']['id'])
                except Address.DoesNotExist:
                    abort(BAD_REQUEST)
                order.delivery_address = address

            # get the user from the flask.g global object registered inside the
            # auth.py::verify() function, called by @auth.login_required decorator
            # and match it against the found user.
            # This is to prevent uses from modify other users' order.
            if auth.current_user != order.user and auth.current_user.admin is False:
                return ({
                    'message': "You can't delete another user's order"
                }, UNAUTHORIZED)

            # Generate the dict of {<Item>: <int:quantity>} to call Order.update_items
            items_uuids = [e['id'] for e in req_items.get('data', [])]
            items = list(Item.select().where(Item.uuid << items_uuids))
            if len(items) != len(items_uuids):
                abort(BAD_REQUEST)
            items_to_add = {
                item: req_item['quantity']
                for item in items for req_item in req_items.get('data', [])
                if str(item.uuid) == req_item['id']
            }
            try:
                order.update_items(items_to_add, new_address=address)
            except InsufficientAvailabilityException:
                abort(BAD_REQUEST)

        return generate_response(order.json(), OK)
Beispiel #6
0
def add():
    form = PostForm()
    if form.validate_on_submit():
        with database.atomic():
            post = Post.create(
                title=form.title.data,
                content=form.content.data,
                is_public=form.is_public.data,
                pub_date=datetime.datetime.now(),
            )
            flash('<script>alert("添加成功")</script>')
            return redirect(url_for('detail', id=post.id))
    return render_template('add.html', form=form)
Beispiel #7
0
def update(id: int):
    post = get_object_by_id_or_404(Post, id=id)
    checked = "checked" if post.is_public else ""
    form = PostForm()
    if request.method == 'POST':
        if form.validate_on_submit:
            with database.atomic():
                post.title = form.title.data
                post.content = form.content.data
                post.is_public = form.is_public.data
                post.pub_time = datetime.datetime.now()
                post.save()
                return redirect(url_for('detail', id=post.id))
    return render_template('update.html',
                           post=post,
                           form=form,
                           checked=checked)
Beispiel #8
0
def index():
    if request.method == 'POST':
        original_url = request.form.get('url')
        if urlparse(original_url).scheme == '':
            original_url = f'http://{original_url}'
        url = Url.select().where(Url.url == original_url).first()
        if url:
            return render_template(
                "index.html",
                short_url=
                f"{request.scheme}://{app.config['SERVER_NAME']}/{Base62.encode(url.id)}",
                views=url.views)
        with database.atomic():
            url = Url.create(url=original_url)
        return render_template(
            "index.html",
            short_url=
            f"{request.scheme}://{app.config['SERVER_NAME']}/{Base62.encode(url.id)}"
        )
    return render_template('index.html')
Beispiel #9
0
    def insert_book(self, book_data):
        total_items = book_data[
            'totalItems'] if 'totalItems' in book_data else 0
        books = []
        created = False
        if 'items' in book_data:
            items = book_data['items']
            for item_data in items:
                google_book_id = item_data['id']
                title = None
                subtitle = None
                description = None
                volume_info = item_data['volumeInfo']
                publisher = None
                page_count = None
                print_type = None
                language = None
                main_category = None
                if volume_info:
                    title = volume_info[
                        'title'] if 'title' in volume_info else None
                    subtitle = volume_info[
                        'subtitle'] if 'subtitle' in volume_info else None
                    description = volume_info[
                        'description'] if 'description' in volume_info else None
                    published_date = volume_info[
                        'publishedDate'] if 'publishedDate' in volume_info else None
                    identifiers = volume_info[
                        'industryIdentifiers'] if 'industryIdentifiers' in volume_info else None
                    publisher = volume_info[
                        'publisher'] if 'publisher' in volume_info else None
                    page_count = volume_info[
                        'pageCount'] if 'pageCount' in volume_info else None
                    print_type = volume_info[
                        'printType'] if 'printType' in volume_info else None
                    language = volume_info[
                        'language'] if 'language' in volume_info else None
                    main_category = volume_info[
                        'mainCategory'] if 'mainCategory' in volume_info else None
                    isbn_10 = None
                    isbn_13 = None
                    for identifier in identifiers:
                        if identifier['type'] == 'ISBN_10':
                            isbn_10 = identifier['identifier']
                        if identifier['type'] == 'ISBN_13':
                            isbn_13 = identifier['identifier']
                    authors = item_data['volumeInfo']['authors'] or []

                with database.atomic():
                    book, created = Book.get_or_create(
                        google_book_id=google_book_id,
                        title=title,
                        subtitle=subtitle,
                        description=description,
                        published_date=published_date,
                        isbn_10=isbn_10,
                        isbn_13=isbn_13,
                        publisher=publisher,
                        page_count=page_count,
                        print_type=print_type,
                        language=language,
                        main_category=main_category)

                    if created:
                        for a in authors:
                            a, _ = Author.get_or_create(name=a)
                            AuthorBook.create(author=a.id, book=book.id)
                books.append(book)
        else:
            raise Exception('isbn not found')
        return (total_items, books)
    def on_put(self, req, resp):
        """
        :param req.header.username: the username
        :param req.header.password: password
        :param req.header.email: email

        :returns: a json contains info of the operation, if the register is
            success or failed
        """
        logging.debug('in account put')
        resp_dict = {}

        try:
            username = req.get_header('username') or ''
            password = req.get_header('password') or ''
            email = req.get_header('email') or 'email'
            # params = req.get_param_as_list()
            # logging.debug('params:%s'%params)
            logging.debug('username:%s, password:%s, email:%s' %
                (username, password, email))
        except:
            raise falcon.HTTPBadRequest('bad req',
                'when read from req, please check if the req is correct.')

        try:
            logging.debug('in account put create')

            with database.atomic():
                # AccountModel.create(username=username,
                #     password=password,
                #     email=email,
                #     join_date=str(datetime.datetime.now())+' GMT+8',
                #     account_level=0,
                #     swift_tenant='test',
                #     swift_username=username,
                #     swift_password=password)
                new_user = AccountModel.create(username=username,
                    password=password,
                    email=email,
                    join_date=str(datetime.datetime.now())+' GMT+8',
                    account_level=0,
                    keystone_tenant=self.conf.account,
                    keystone_username=username,
                    keystone_password=password,
                    disk_container=username+'_'+self.conf.disk_container,
                    keystone_info='')
                logging.debug('in account put create database.atomic')

            # conn = swiftclient.client.Connection(self.conf.auth_url,
            #                       self.conf.account_username,
            #                       self.conf.password,
            #                       auth_version=self.conf.auth_version or 1)
            keystone_info = swiftwrap.create_user(new_user.keystone_tenant,
                new_user.keystone_username,
                new_user.keystone_password,
                new_user.account_level)
            logging.debug('keystone_info:%s' % keystone_info)
            q = AccountModel.update(keystone_info=keystone_info).where(
                AccountModel.username == username,
                AccountModel.password == password)
            q.execute()
            resp_dict['info'] = 'successfully create user:%s' % username
            resp_dict['username'] = username
            resp.status = falcon.HTTP_201
        except KeystoneUserCreateException:
            logging.error('in restapi KeystoneUserCreateException!')
            q = AccountModel.delete().where(AccountModel.username==username,
                    AccountModel.password==password)
            q.execute()
            resp_dict['info'] = 'create user failed, did not create user:%s' % username
            resp.status = falcon.HTTP_500
            resp.body = json.dumps(resp_dict, encoding='utf-8')
        except peewee.IntegrityError:
            logging.warning('in account put create except')

            # `username` is a unique column, so this username already exists,
            # making it safe to call .get().
            old_user = AccountModel.get(AccountModel.username == username)
            logging.warning('user exists...')
            resp_dict['info'] = 'user exists, did not create user:%s' % username
            resp.status = falcon.HTTP_409
            try:
                change_user = AccountModel.get(AccountModel.username==username,
                                AccountModel.password==password)
            except:
                logging.debug('change user data failed...')
        except:
            logging.error('restapi_keystone put account Exception!')
            q = AccountModel.delete().where(AccountModel.username==username,
                    AccountModel.password==password)
            q.execute()
            resp_dict['info'] = 'create user failed, did not create user:%s' % username
            resp.status = falcon.HTTP_400
            resp.body = json.dumps(resp_dict, encoding='utf-8')
        resp.body = json.dumps(resp_dict, encoding='utf-8')
Beispiel #11
0
    def post(self):
        """ Insert a new order."""
        res = request.get_json(force=True)

        errors = Order.validate_input(res)
        if errors:
            return errors, BAD_REQUEST

        # Extract data to create the new order
        req_items = res['data']['relationships']['items']['data']
        req_address = res['data']['relationships']['delivery_address']['data']
        req_user = res['data']['relationships']['user']['data']

        # Check that the address exist
        try:
            user = User.get(User.uuid == req_user['id'])
        except User.DoesNotExist:
            abort(BAD_REQUEST)

        # get the user from the flask.g global object registered inside the
        # auth.py::verify() function, called by @auth.login_required decorator
        # and match it against the found user.
        # This is to prevent users from creating other users' order.
        if auth.current_user != user and auth.current_user.admin is False:
            return ({
                'message': "You can't create a new order for another user"
            }, UNAUTHORIZED)

        # Check that the items exist
        item_ids = [req_item['id'] for req_item in req_items]
        items = Item.select().where(Item.uuid << item_ids)
        if items.count() != len(req_items):
            abort(BAD_REQUEST)

        # Check that the address exist
        try:
            address = Address.get(Address.uuid == req_address['id'])
        except Address.DoesNotExist:
            abort(BAD_REQUEST)

        # Generate the dict of {<Item>: <int:quantity>} to call Order.create_order
        items_to_add = {}
        for req_item in req_items:
            item = next(i for i in items if str(i.uuid) == req_item['id'])
            items_to_add[item] = req_item['quantity']
        with database.atomic():
            try:
                order = Order.create(
                    delivery_address=address,
                    user=auth.current_user,
                )

                for item in items:
                    for req_item in req_items:
                        # if names match add item and quantity, once per
                        # req_item
                        if str(item.uuid) == req_item['id']:
                            order.add_item(item, req_item['quantity'])
                            break
                notify_new_order(address=order.delivery_address,
                                 user=order.user)
            except InsufficientAvailabilityException:
                abort(BAD_REQUEST)

        return generate_response(order.json(), CREATED)
Beispiel #12
0
def delete(id: int):
    post = get_object_by_id_or_404(Post, id=id)
    with database.atomic():
        Post.delete_instance(post)
    flash('<script>alert("删除成功")</script>')
    return redirect(url_for('home'))