Esempio n. 1
0
    def test_large_file_error(self):
        import psycopg2
        with self.app.test_request_context():
            db_file = database.get_session().get_db_file(13)
            with self.assertRaises(psycopg2.OperationalError):
                data = ''.join(db_file.iter_data())

        with self.app.test_request_context():
            db_file = database.get_session().get_db_file(13)
            with self.assertRaises(psycopg2.OperationalError):
                db_file.save_from(StringIO("bla bla"))
Esempio n. 2
0
    def test_save(self):
        with self.app.test_request_context():
            session = database.get_session()
            session.save(database.PersonRow(hello="world"))
            session.commit()

        with self.app.test_request_context():
            cursor = database.get_session().conn.cursor()
            cursor.execute("SELECT * FROM person")
            [row] = list(cursor)
            self.assertEqual(row[1], {u"hello": u"world"})
Esempio n. 3
0
    def test_large_file(self):
        with self.app.test_request_context():
            session = database.get_session()
            db_file = session.get_db_file()
            db_file.save_from(StringIO("hello large data"))
            session.commit()
            db_file_id = db_file.id

        with self.app.test_request_context():
            session = database.get_session()
            db_file = session.get_db_file(db_file_id)
            data = ''.join(db_file.iter_data())
            self.assertEqual(data, "hello large data")
Esempio n. 4
0
    def test_delete(self):
        with self.app.test_request_context():
            session = database.get_session()
            session.save(database.PersonRow(hello="world"))
            session.commit()

        with self.app.test_request_context():
            session = database.get_session()
            session.table(database.PersonRow).delete(1)
            session.commit()

        with self.app.test_request_context():
            cursor = database.get_session().conn.cursor()
            cursor.execute("SELECT * FROM person")
            self.assertEqual(list(cursor), [])
def update_local_database():
    app = flask.current_app
    if not app.config.get("DEBUG", False):
        flask.abort(403)

    session = database.get_session()
    cursor = session.conn.cursor()

    # get the database
    url = "http://cites.eaudeweb.ro%s" % flask.url_for("update.dump_database")
    resp = requests.get(url, headers={"X-CitesAuth": app.config["API_KEY"]})
    assert resp.status_code == 200

    # drop everything
    database.drop_all()
    database.create_all()

    for table, items in resp.json.items():
        for i in items:
            row = getattr(database, table)()
            row.update(i)
            session.save(row, row.get("id", None))
            session.commit()

    for table in resp.json.keys():
        table = getattr(database, table)
        cursor.execute("SELECT setval('%s_id_seq', (SELECT MAX(id) FROM %s)+1)" % (table._table, table._table))

    return flask.jsonify({"status": "success"})
Esempio n. 6
0
def upload(resolution_id):
    resolution_row = database.get_or_404('resolution', resolution_id)
    resolution_schema = schema.ResolutionSchema.from_flat(resolution_row)
    session = database.get_session()

    doc = flask.request.files["file"]
    lang = flask.request.form.get('lang')

    if lang not in ('E', 'F', 'S'):
        flask.abort(400)

    db_file = session.get_db_file()

    if doc.filename:
        resolution_schema['filename'][lang].set(doc.filename)
        resolution_schema['doc_id'][lang].set(db_file.id)

        if resolution_schema.validate():
            db_file.save_from(doc)
            resolution_row.update(resolution_schema.flatten())
            session.save(resolution_row)
            session.commit()
            flask.flash('Document saved', 'success')

    return flask.redirect(
        flask.url_for('resolution.view', resolution_id=resolution_id))
Esempio n. 7
0
    def test_remove_large_file(self):
        with self.app.test_request_context():
            session = database.get_session()
            db_file = session.get_db_file()
            db_file.save_from(StringIO("hello large data"))
            session.commit()
            db_file_id = db_file.id

        with self.app.test_request_context():
            session = database.get_session()
            session.del_db_file(db_file_id)
            session.commit()

        with self.app.test_request_context():
            cursor = database.get_session().conn.cursor()
            cursor.execute("SELECT DISTINCT loid FROM pg_largeobject")
            self.assertEqual(list(cursor), [])
Esempio n. 8
0
    def test_autoincrement_id(self):
        with self.app.test_request_context():
            p1 = database.PersonRow()
            p2 = database.PersonRow()
            session = database.get_session()
            session.save(p1)
            session.save(p2)
            session.commit()
            self.assertEqual(p1.id, 1)
            self.assertEqual(p2.id, 2)

        with self.app.test_request_context():
            cursor = database.get_session().conn.cursor()
            cursor.execute("SELECT * FROM person")
            [row1, row2] = list(cursor)
            self.assertEqual(row1[0], 1)
            self.assertEqual(row2[0], 2)
Esempio n. 9
0
def create_mock_app():
    from cites.app import create_app
    from cites import database

    app = create_app()
    app.config["DATABASE_URI"] = _get_testing_db_uri()
    app.config["TESTING"] = True
    app.config["MAIL_SUPPRESS_SEND"] = True
    database.initialize_app(app)
    with app.test_request_context():
        database.get_session().create_all()

    def app_teardown():
        with app.test_request_context():
            database.get_session().drop_all()

    return app, app_teardown
Esempio n. 10
0
def capitalize_names(meeting_id):
    session = database.get_session()
    persons = models.Person.select()
    persons = sugar.append_persons_query(persons, meeting_id)
    for p in persons:
        p.data["personal_first_name"] = schema.common.clean_and_cap(p.data["personal_first_name"])
        p.data["personal_last_name"] = schema.common.clean_and_cap(p.data["personal_last_name"], is_last_name=True)
        p.save()
    session.commit()
Esempio n. 11
0
    def query(self, options, **kwargs):
        where = {}
        search = options['search']
        order_by = options.get('order_by')
        meeting_id = flask.request.args.get('meeting_id')
        revised = flask.request.args.get('revised')
        corrected = flask.request.args.get('corrected')
        repealed = flask.request.args.get('repealed')

        RE = htables.op.RE
        if search:
            where['title_E'] = RE('.*%s.*' % rescape(unidecode(search.lower())))
        if meeting_id:
            where['meeting_id'] = meeting_id
        if revised:
            where['revised'] = revised
        if corrected:
            where['corrected'] = corrected
        if repealed:
            where['repealed'] = repealed

        filters = {
            'where': where,
            'offset': options['offset'],
            'limit': options['limit'],
            'count': options['count'],
        }

        if order_by and not options['count']:
            column = self.COLUMNS.get(order_by[0])
            filters['order_by'] = column if order_by[1] == 'asc' else \
                                  htables.op.Reversed(column)

        if not order_by and not options['count']:
            filters['order_by'] = 'order'

        dbs = database.get_session()
        results = dbs['resolution'].query(**filters)
        if options['count']:
            return results

        def table_row(resolution_row):
            resolution = schema.Resolution.from_flat(resolution_row)

            url = flask.url_for('.view', resolution_id=resolution.id)
            title = '<a href="%s">%s</a>' % (url, resolution['title']['E'])
            if resolution.get('active'):
                active = '<i class="icon-ok"></i>'
            else:
                active = ''

            return {
                'number': resolution['number'],
                'title': title,
                'active': active,
            }
        return (table_row(resolution_row) for resolution_row in results)
Esempio n. 12
0
    def test_load(self):
        with self.app.test_request_context():
            session = database.get_session()
            session.save(database.PersonRow(hello="world"))
            session.commit()

        with self.app.test_request_context():
            person = _get_person(1)
            self.assertEqual(person, {"hello": "world"})
Esempio n. 13
0
def change_password(token=None):
    if not token and not flask.session.get("logged_in_email"):
        return flask.redirect("/login")

    if flask.request.method == "POST":
        form_data = flask.request.form.to_dict()

        new_pass = form_data["new_pass"]
        check_pass = form_data["check_pass"]

        if token:
            staff_member = database.find("staff", token=token)
            staff_member = staff_member.next()

        else:
            old_pass = form_data["old_pass"]

            email = flask.session["logged_in_email"]
            staff_member = database.find("staff", email=email)
            staff_member = staff_member.next()

            try:
                assert sugar.check_hash(old_pass, staff_member["password"])
            except AssertionError:
                flask.flash("Wrong password.", "error")
                return

        try:
            assert sugar.check_hash(new_pass, sugar.make_hash(check_pass))
        except AssertionError:
            flask.flash("New passwords do not match.", "error")
            return {"token": token}

        try:
            assert new_pass != u""
        except AssertionError:
            flask.flash("Please enter a new password.", "error")
        else:
            session = database.get_session()

            staff_row = database.get_or_404("staff", id=staff_member.id)
            staff_schema = StaffSchema.from_flat(staff_row)

            staff_schema["password"].set(sugar.make_hash(new_pass))

            if staff_schema.validate():
                staff_row.update(staff_schema.flatten())
                session.save(staff_row)
                session.commit()

                flask.flash("Password changed sucessfuly.", "success")

                if token:
                    login_url = flask.url_for("auth.login", next=flask.url_for("meeting.home"))
                    return flask.redirect(login_url)

    return {"token": token}
Esempio n. 14
0
def reset_password():
    email_to_reset_password = flask.request.form.get("email", "")

    if flask.request.method == "POST":
        try:
            staff_member = [i for i in database.find("staff", email=email_to_reset_password)]
            assert len(staff_member) == 1
            staff_member = staff_member[0]
        except AssertionError:
            flask.flash(u"Your email does not exist in our database.", "error")
        else:
            auth_token = sugar.generate_token(email_to_reset_password)

            session = database.get_session()

            staff_row = database.get_or_404("staff", id=staff_member.id)
            staff_schema = StaffSchema.from_flat(staff_row)

            staff_schema["token"].set(auth_token)

            if staff_schema.validate():
                staff_row.update(staff_schema.flatten())
                session.save(staff_row)
                session.commit()

            app = flask.current_app
            mail = Mail(app)

            settings_url = app.config.get("HOSTNAME")
            mail_msg_link = "%s/%s/change-password" % (settings_url, auth_token)

            msg_subject = "Reset your Cites password"
            msg_sender = app.config.get("DEFAULT_MAIL_SENDER")
            msg_recipients = [email_to_reset_password]
            msg_body = str(
                "Forgot your password?\n\nCites received a request "
                "to reset the password for your account.\n"
                "If you want to reset your "
                "password, click on the link below (or copy and "
                "paste the URL into your browser):\n\n%s\n\nThis "
                "link takes you to a secure page where you can "
                "change your password.\nIf you don't want to "
                "reset your password, please ignore this "
                "message. Your password will not be reset."
                "\n\nThe Cites Team" % (mail_msg_link)
            )

            msg = Message(msg_subject, sender=msg_sender, recipients=msg_recipients, body=msg_body)

            mail.send(msg)

            flash_message = str(
                "We've sent password reset instructions to your "
                "email address. Please also check your email's "
                "spam filter."
            )
            flask.flash(flash_message, "success")
Esempio n. 15
0
def capitalize_names(meeting_id):
    session = database.get_session()
    persons = database.find("person", meeting_id=str(meeting_id))
    for p in persons:
        p["personal_first_name"] = schema.common.clean_and_cap(p["personal_first_name"])

        p["personal_last_name"] = schema.common.clean_and_cap(p["personal_last_name"], is_last_name=True)
        session.save(p)
    session.commit()
Esempio n. 16
0
def update_past_mail_logs_schema(meeting_id):
    session = database.get_session()
    mail_logs = database.find("mail_log", meeting_id=str(meeting_id))
    for m in mail_logs:
        if m.get("mail_to"):
            m["mail_to_0"] = m.pop("mail_to")
            m["mail_cc_0"] = m.pop("mail_cc")
            session.save(m)
    session.commit()
Esempio n. 17
0
    def test_update(self):
        with self.app.test_request_context():
            session = database.get_session()
            session.save(database.PersonRow(k1="v1", k2="v2", k3="v3"))
            session.commit()

        with self.app.test_request_context():
            session = database.get_session()
            person = _get_person(1)
            del person["k1"] # remove value
            person["k2"] = "vX" # change value
            # person["k3"] unchanged
            person["k4"] = "v4" # add value
            session.save(person)
            session.commit()

        with self.app.test_request_context():
            person = _get_person(1)
            self.assertEqual(person, {"k2": "vX", "k3": "v3", "k4": "v4"})
Esempio n. 18
0
def delete_document(resolution_id, doc_id, lang):
    resolution_row = database.get_or_404('resolution', resolution_id)
    resolution_row['doc_id_%s' % lang] = ''
    resolution_row['filename_%s' % lang] = ''

    session = database.get_session()
    session.del_db_file(doc_id)
    session.save(resolution_row)
    session.commit()
    return flask.jsonify({'status': 'success'})
Esempio n. 19
0
def generate_users(meeting_id, number_of_copies=27):
    app = flask.current_app
    user_generator = database.find("person", meeting_id=str(meeting_id))
    session = database.get_session()
    for user in user_generator:
        for i in xrange(int(number_of_copies)):
            person_row = database.new('person')
            person_row.update(user)
            session.save(person_row)
    session.commit()
Esempio n. 20
0
def change_name_titles(meeting_id):
    session = database.get_session()
    persons = database.get_all("person")
    persons = [p for p in persons if p["personal_name_title"] in ["Miss", "Mrs"]]

    for p in persons:
        p["personal_name_title"] = u"Ms"
        session.save(p)

    session.commit()
Esempio n. 21
0
def change_region_from_zero_to_None(meeting_id):
    session = database.get_session()
    persons = database.find("person", representing_region="0")

    for p in persons:
        if p["representing_region"] == "0":
            p["representing_region"] = u""
            session.save(p)

    session.commit()
Esempio n. 22
0
 def __init__(self, data):
     session = database.get_session()
     country_row = database.new('country')
     if data:
         country_row.update(data)
         country_schema = CountrySchema.from_flat(data)
         country_row.update(country_schema.flatten())
     session.save(country_row)
     self.id = country_row.id
     session.commit()
Esempio n. 23
0
    def test_new_document(self):
        self.create_document()
        document = models.Document.select().where().get(id=1)
        self.assertEqual('document', document.data['name'])
        self.assertEqual('document-en', document.data['title_E'])

        with self.app.test_request_context():
            session = database.get_session()
            db_file = session.get_db_file(int(document.data['doc_id_E']))
            db_file = list(db_file.iter_data())
            self.assertEqual(1, len(db_file))
Esempio n. 24
0
    def test_load_all(self):
        with self.app.test_request_context():
            session = database.get_session()
            session.save(database.PersonRow(hello="world"))
            session.save(database.PersonRow(x="y"))
            session.commit()

        with self.app.test_request_context():
            all_persons = list(database.get_all_persons())
            self.assertEqual(len(all_persons), 2)
            self.assertEqual(all_persons[0], {'hello': "world"})
            self.assertEqual(all_persons[0].id, 1)
            self.assertEqual(all_persons[1], {'x': "y"})
            self.assertEqual(all_persons[1].id, 2)
Esempio n. 25
0
def edit(resolution_id=None):
    if resolution_id is None:
        resolution_row = None
    else:
        resolution_row = database.get_or_404('resolution', resolution_id)

    meetings = [schema.Meeting.from_flat(m) for m in database.get_all('meeting')]
    session = database.get_session()

    if flask.request.method == "POST":
        form_data = dict(schema.ResolutionSchema.from_defaults().flatten())
        form_data.update(flask.request.form.to_dict())

        resolution_schema = schema.ResolutionSchema()
        resolution_schema.set_meetings(meetings)
        resolution_schema.set_flat(form_data)

        if resolution_schema.validate():
            if resolution_row is None:
                resolution_row = database.new('resolution')
            resolution_row.update(resolution_schema.flatten())
            session.save(resolution_row)
            session.commit()
            flask.flash('Resolution saved', 'success')
            if resolution_id:
                url = flask.url_for('resolution.view',
                                    resolution_id=resolution_id)
            else:
                url = flask.url_for('resolution.home')
            return flask.redirect(url)
        else:
            flask.flash(u"Errors in person information", "error")
    else:
        resolution_schema = schema.ResolutionSchema()
        resolution_schema.set_meetings(meetings)

        if resolution_row:
            resolution_schema.set_flat(resolution_row)
        else:
            resolution_schema = resolution_schema.from_defaults()
            resolution_schema.set_meetings(meetings)

    return {
        'mk': sugar.MarkupGenerator(
            flask.current_app.jinja_env.get_template('widgets/widgets_edit.html')
        ),
        'resolution_id': resolution_id,
        'resolution_schema': resolution_schema,
    }
    def test_no_clobber(self):
        from cites import database
        with self.app.test_request_context():
            session = database.get_session()
            session.save(database.PersonRow(random_key="random value"))
            session.commit()

        resp = self.client.post('/meeting/1/participant/1/edit', data={
            'personal_first_name': u"Joe",
            'personal_last_name': u"Smith",
        }, follow_redirects=True)
        self.assertIn("Person information saved", resp.data)

        [data] = self._get_person_data()
        self.assertEqual(data['random_key'], "random value")
Esempio n. 27
0
def delete(resolution_id):
    resolution_row = database.get_or_404('resolution', resolution_id)
    session = database.get_session()

    if resolution_row['doc_id_E']:
        session.del_db_file(int(resolution_row['doc_id_E']))

    if resolution_row['doc_id_F']:
        session.del_db_file(int(resolution_row['doc_id_F']))

    if resolution_row['doc_id_S']:
        session.del_db_file(int(resolution_row['doc_id_S']))

    session['resolution'].delete(resolution_id)
    session.commit()
    return flask.jsonify({'status': 'success'})
Esempio n. 28
0
def insert_category(meeting_id, file_name):
    import os
    import json
    from cites.schema import MeetingSchema

    meeting_row = database.get_or_404("meeting", id=str(meeting_id))
    meeting_schema = MeetingSchema.from_flat(meeting_row)
    member_schema = meeting_schema["categories"].member_schema
    data = json.load(open(os.path.join(os.path.dirname(__file__), file_name), "rb"))
    new_category_schema = member_schema(data)
    if new_category_schema.validate():
        meeting_schema["categories"].insert(0, new_category_schema)

    if meeting_schema.validate():
        session = database.get_session()
        meeting_row.update(meeting_schema.flatten())
        session.save(meeting_row)
        session.commit()
Esempio n. 29
0
def download(resolution_id, lang):
    import tempfile
    tmpfile = tempfile.NamedTemporaryFile()

    resolution = schema.Resolution.get_or_404(resolution_id)
    session = database.get_session()
    filename = resolution['filename'][lang]

    try:
        doc_id = resolution['doc_id'][lang]
        assert doc_id is not None
        db_file = session.get_db_file(int(doc_id))
    except (AssertionError, KeyError):
        flask.abort(404)

    for chunk in db_file.iter_data():
        tmpfile.write(chunk)
    tmpfile.seek(0)

    return flask.send_file(tmpfile, attachment_filename=filename,
                           as_attachment=True)
Esempio n. 30
0
def edit(country_id=None):
    app = flask.current_app
    session = database.get_session()
    if country_id:
        country_row = database.get_or_404("country", country_id)
        country = Country.from_flat(country_row)
    else:
        country_row = database.new("country")
        country = None

    if flask.request.method == "POST":
        form_data = dict(CountrySchema.from_defaults().flatten())
        form_data.update(flask.request.form.to_dict())

        country_schema = CountrySchema.from_flat(form_data)

        if country_schema.validate():
            if country_row is None:
                country_row = database.new('country')
            country_row.update(country_schema.flatten())
            session.save(country_row)
            session.commit()
            flask.flash('Country saved.', 'success')
            view_url = flask.url_for("country.home")
            return flask.redirect(view_url)
        else:
            flask.flash(u"Errors in country information", "error")
    else:
        if country_row is None:
            country_schema = CountrySchema()
        else:
            country_schema = CountrySchema.from_flat(country_row)

    return {
        "mk": sugar.MarkupGenerator(
            app.jinja_env.get_template("widgets/widgets_edit.html")
        ),
        "country_schema": country_schema,
        "country": country,
    }