Ejemplo n.º 1
0
def main():
    from docassemble.webapp.config import daconfig, S3_ENABLED, s3_config
    certs_location = daconfig.get('certs', None)
    if S3_ENABLED:
        import docassemble.webapp.amazon
        s3 = docassemble.webapp.amazon.s3object(s3_config)
        bucket = None
        prefix = None
        if certs_location is None:
            bucket = s3.bucket
            prefix = 'certs'
        else:
            m = re.search(r'^s3://([^/]+)/(.*)', certs_location)
            if m:
                bucket = s3.conn.get_bucket(m.group(1))
                prefix = m.group(2)
        if bucket is not None and prefix is not None:
            if not re.search(r'/$', prefix):
                prefix = prefix + '/'
            dest = daconfig.get('cert_install_directory', '/etc/ssl/docassemble')
            if dest:
                if not os.path.isdir(dest):
                    os.makedirs(dest)
                for key in bucket.list(prefix=prefix, delimiter='/'):
                    filename = re.sub(r'.*/', '', key.name)
                    fullpath = os.path.join(dest, filename)
                    sys.stderr.write("install_certs: saving " + str(key.name) + " to " + str(fullpath) + "\n")
                    key.get_contents_to_filename(fullpath)
                    os.chmod(fullpath, stat.S_IRUSR)
            else:
                sys.stderr.write("SSL destination directory not known")
                sys.exit(1)
            return
    if certs_location is None:
        if os.path.isdir('/usr/share/docassemble/certs'):
            certs_location = '/usr/share/docassemble/certs'
        else:
            return
    if not os.path.isdir(certs_location):
        sys.stderr.write("certs directory " + str(certs_location) + " does not exist")
        sys.exit(1)
    import shutil
    dest = daconfig.get('cert_install_directory', '/etc/ssl/docassemble')
    if dest:
        if os.path.isdir(dest):
            shutil.rmtree(dest)
        shutil.copytree(certs_location, dest)
        for root, dirs, files in os.walk(dest):
            for the_file in files:
                os.chmod(os.path.join(root, the_file), stat.S_IRUSR)
    else:
        sys.stderr.write("SSL destination directory not known")
        sys.exit(1)
    return
Ejemplo n.º 2
0
def get_installed_distributions():
    from docassemble.webapp.config import daconfig
    PACKAGE_DIRECTORY = daconfig.get('packages', '/usr/share/docassemble/local')
    results = list()
    output, err = subprocess.Popen([daconfig.get('pip', os.path.join(PACKAGE_DIRECTORY, 'bin', 'pip')), 'freeze'], stdin=subprocess.PIPE, stdout=subprocess.PIPE).communicate()
    for line in output.split('\n'):
        a = line.split("==")
        if len(a) == 2:
            results.append(Object(key=a[0].lower(), version=a[1]))
        #else:
            #logmessage("Did not understand line: " + str(line))
    return results
Ejemplo n.º 3
0
def get_installed_distributions():
    from docassemble.webapp.config import daconfig
    PACKAGE_DIRECTORY = daconfig.get('packages', '/usr/share/docassemble/local')
    results = list()
    output, err = subprocess.Popen([daconfig.get('pip', os.path.join(PACKAGE_DIRECTORY, 'bin', 'pip')), 'freeze'], stdin=subprocess.PIPE, stdout=subprocess.PIPE).communicate()
    for line in output.split('\n'):
        a = line.split("==")
        if len(a) == 2:
            results.append(Object(key=a[0].lower(), version=a[1]))
        else:
            logmessage("Did not understand line: " + str(line))
    return results
Ejemplo n.º 4
0
def populate_tables():
    existing_admin = Role.query.filter_by(name=word("admin")).first()
    if existing_admin:
        return
    defaults = daconfig.get(
        "default_admin_account", {"nickname": "admin", "email": "*****@*****.**", "password": "******"}
    )
    docassemble_git_url = daconfig.get("docassemble_git_url", "https://github.com/jhpyle/docassemble")
    admin_role = Role(name=word("admin"))
    user_role = Role(name=word("user"))
    developer_role = Role(name=word("developer"))
    advocate_role = Role(name=word("advocate"))
    db.session.add(admin_role)
    db.session.add(user_role)
    db.session.add(developer_role)
    db.session.add(advocate_role)
    user_manager = UserManager(SQLAlchemyAdapter(db, User, UserAuthClass=UserAuth), app)
    user_auth = UserAuth(password=app.user_manager.hash_password(defaults["password"]))
    user = User(
        active=True,
        nickname=defaults["nickname"],
        social_id="local$" + "".join(random.choice(string.ascii_uppercase + string.digits) for x in xrange(32)),
        email=defaults["email"],
        user_auth=user_auth,
    )
    user.roles.append(admin_role)
    db.session.add(user_auth)
    db.session.add(user)
    db.session.commit()
    installed_packages = sorted(get_installed_distributions())
    for package in installed_packages:
        package_auth = PackageAuth(user_id=user.id)
        if package.key in ["docassemble", "docassemble.base", "docassemble.webapp", "docassemble.demo"]:
            package_entry = Package(
                name=package.key,
                package_auth=package_auth,
                giturl=docassemble_git_url,
                packageversion=package.version,
                gitsubdir=re.sub(r"\.", "_", package.key),
                type="git",
                core=True,
            )
        else:
            package_entry = Package(
                name=package.key, package_auth=package_auth, packageversion=package.version, type="pip", core=True
            )
        db.session.add(package_auth)
        db.session.add(package_entry)
    db.session.commit()
    return
Ejemplo n.º 5
0
 def url_for(self, **kwargs):
     if 'ext' in kwargs:
         extn = kwargs['ext']
         extn = re.sub(r'^\.', '', extn)
     else:
         extn = None
     filename = kwargs.get('filename', self.filename)
     if S3_ENABLED:
         keyname = str(self.section) + '/' + str(
             self.file_number) + '/' + str(filename)
         page = kwargs.get('page', None)
         if page:
             size = kwargs.get('size', 'page')
             page = re.sub(r'[^0-9]', '', page)
             if size == 'screen':
                 keyname += 'screen-' + str(page) + '.png'
             else:
                 keyname += 'page-' + str(page) + '.png'
         elif extn:
             keyname += '.' + extn
         key = s3.get_key(keyname)
         if key.exists():
             return (key.generate_url(3600))
         else:
             return ('about:blank')
     else:
         if extn is None:
             extn = ''
         else:
             extn = '.' + extn
         root = daconfig.get('root', '/')
         fileroot = daconfig.get('fileserver', root)
         if self.section == 'files':
             if 'page' in kwargs and kwargs['page']:
                 page = re.sub(r'[^0-9]', '', str(kwargs['page']))
                 size = kwargs.get('size', 'page')
                 url = fileroot + 'uploadedpage'
                 if size == 'screen':
                     url += 'screen'
                 url += '/' + str(self.file_number) + '/' + str(page)
             else:
                 url = fileroot + 'uploadedfile/' + str(
                     self.file_number) + extn
         else:
             url = 'about:blank'
         return (url)
Ejemplo n.º 6
0
 def url_for(self, **kwargs):
     if 'ext' in kwargs:
         extn = kwargs['ext']
         extn = re.sub(r'^\.', '', extn)
     else:
         extn = None
     filename = kwargs.get('filename', self.filename)
     if S3_ENABLED:
         keyname = str(self.section) + '/' + str(self.file_number) + '/' + str(filename)
         page = kwargs.get('page', None)
         if page:
             size = kwargs.get('size', 'page')
             page = re.sub(r'[^0-9]', '', page)
             if size == 'screen':
                 keyname += 'screen-' + str(page) + '.png'
             else:
                 keyname += 'page-' + str(page) + '.png'
         elif extn:
             keyname += '.' + extn
         key = s3.get_key(keyname)
         if key.exists():
             return(key.generate_url(3600))
         else:
             return('about:blank')
     else:
         if extn is None:
             extn = ''
         else:
             extn = '.' + extn
         root = daconfig.get('root', '/')
         fileroot = daconfig.get('fileserver', root)
         if self.section == 'files':
             if 'page' in kwargs and kwargs['page']:
                 page = re.sub(r'[^0-9]', '', str(kwargs['page']))
                 size = kwargs.get('size', 'page')
                 url = fileroot + 'uploadedpage'
                 if size == 'screen':
                     url += 'screen'
                 url += '/' + str(self.file_number) + '/' + str(page)
             else:
                 url = fileroot + 'uploadedfile/' + str(self.file_number) + extn
         else:
             url = 'about:blank'
         return(url)
Ejemplo n.º 7
0
 def connect(self):
     legalserver = daconfig.get('legalserver', None)
     if legalserver is None:
         raise Exception("Could not get Legal Server connection information")
     try:
         self.conn = psycopg2.connect("dbname='" + legalserver['database'] + "' user='******'username'] + "' host='" + legalserver['host'] + "' password='******'password'] + "'")
     except Exception as e:
         raise Exception("Could not connect to Legal Server: " + str(e))
     self.connected = True
     return
Ejemplo n.º 8
0
 def connect(self):
     legalserver = daconfig.get('legalserver', None)
     if legalserver is None:
         raise Exception(
             "Could not get Legal Server connection information")
     try:
         self.conn = psycopg2.connect("dbname='" + legalserver['database'] +
                                      "' user='******'username'] +
                                      "' host='" + legalserver['host'] +
                                      "' password='******'password'] + "'")
     except Exception as e:
         raise Exception("Could not connect to Legal Server: " + str(e))
     self.connected = True
     return
Ejemplo n.º 9
0
 def save_survey_results(self, the_dict):
     storage = daconfig.get('storage', None)
     if storage is None:
         raise Exception("Could not get database connection information")
     try:
         conn = psycopg2.connect("dbname='" + storage['database'] + "' user='******'username'] + "' host='" + storage['host'] + "' password='******'password'] + "'")
     except Exception as e:
         raise Exception("Could not connect to database: " + str(e))
     answer_json = json.dumps(the_dict)
     cur = conn.cursor()
     cur.execute("""insert into survey_answers (content) values (%s)""", (str(answer_json),))
     conn.commit()
     cur.close()
     conn.close()
     return
Ejemplo n.º 10
0
 def save_survey_results(self, the_dict):
     storage = daconfig.get('storage', None)
     if storage is None:
         raise Exception("Could not get database connection information")
     try:
         conn = psycopg2.connect("dbname='" + storage['database'] +
                                 "' user='******'username'] +
                                 "' host='" + storage['host'] +
                                 "' password='******'password'] + "'")
     except Exception as e:
         raise Exception("Could not connect to database: " + str(e))
     answer_json = json.dumps(the_dict)
     cur = conn.cursor()
     cur.execute("""insert into survey_answers (content) values (%s)""",
                 (str(answer_json), ))
     conn.commit()
     cur.close()
     conn.close()
     return
Ejemplo n.º 11
0
import os
import re
import shutil
import urllib
import tempfile
import mimetypes
from docassemble.base.logger import logmessage
from docassemble.base.error import DAError
from docassemble.webapp.config import daconfig, s3_config, S3_ENABLED

if S3_ENABLED:
    import docassemble.webapp.amazon
    s3 = docassemble.webapp.amazon.s3object(s3_config)

UPLOAD_DIRECTORY = daconfig.get('uploads', '/usr/share/docassemble/files')

class SavedFile(object):
    def __init__(self, file_number, extension=None, fix=False, section='files', filename='file'):
        self.file_number = file_number
        self.extension = extension
        self.fixed = False
        self.section = section
        self.filename = filename
        if not S3_ENABLED:
            if self.section == 'files':
                parts = re.sub(r'(...)', r'\1/', '{0:012x}'.format(int(file_number))).split('/')
                self.directory = os.path.join(UPLOAD_DIRECTORY, *parts)
            else:
                self.directory = os.path.join(UPLOAD_DIRECTORY, str(self.section), str(file_number))
            self.path = os.path.join(self.directory, filename)
        if fix:
Ejemplo n.º 12
0
import os
import re
import shutil
import urllib
import tempfile
import mimetypes
from docassemble.base.logger import logmessage
from docassemble.base.error import DAError
from docassemble.webapp.config import daconfig, s3_config, S3_ENABLED

if S3_ENABLED:
    import docassemble.webapp.amazon
    s3 = docassemble.webapp.amazon.s3object(s3_config)

UPLOAD_DIRECTORY = daconfig.get('uploads', '/usr/share/docassemble/files')


class SavedFile(object):
    def __init__(self,
                 file_number,
                 extension=None,
                 fix=False,
                 section='files',
                 filename='file'):
        self.file_number = file_number
        self.extension = extension
        self.fixed = False
        self.section = section
        self.filename = filename
        if not S3_ENABLED:
            if self.section == 'files':
Ejemplo n.º 13
0
from docassemble.webapp.packages.models import Package, PackageAuth, Install
from docassemble.webapp.core.models import Attachments, Uploads, SpeakList, Supervisors, KVStore
from docassemble.webapp.users.models import User, UserAuth, Role, UserRoles, UserDict, UserDictKeys, UserDictLock
from docassemble.webapp.update import get_installed_distributions
from sqlalchemy import create_engine, MetaData
import docassemble.webapp.database
import random
import string

# from flask import Flask
# from flask.ext.sqlalchemy import SQLAlchemy
from flask.ext.user import UserManager, SQLAlchemyAdapter

if __name__ == "__main__":
    app.config["SQLALCHEMY_DATABASE_URI"] = docassemble.webapp.database.alchemy_connection_string()
    app.secret_key = daconfig.get("secretkey", "28ihfiFehfoU34mcq_4clirglw3g4o87")


def populate_tables():
    existing_admin = Role.query.filter_by(name=word("admin")).first()
    if existing_admin:
        return
    defaults = daconfig.get(
        "default_admin_account", {"nickname": "admin", "email": "*****@*****.**", "password": "******"}
    )
    docassemble_git_url = daconfig.get("docassemble_git_url", "https://github.com/jhpyle/docassemble")
    admin_role = Role(name=word("admin"))
    user_role = Role(name=word("user"))
    developer_role = Role(name=word("developer"))
    advocate_role = Role(name=word("advocate"))
    db.session.add(admin_role)
Ejemplo n.º 14
0
import sys
import os
import docassemble.webapp.config
from docassemble.webapp.config import daconfig, s3_config, S3_ENABLED
if __name__ == "__main__":
    docassemble.webapp.config.load(arguments=sys.argv)
WEBAPP_PATH = daconfig.get('webapp',
                           '/usr/share/docassemble/webapp/docassemble.wsgi')


def main():
    if S3_ENABLED:
        import docassemble.webapp.amazon
        s3 = docassemble.webapp.amazon.s3object(s3_config)
        key = s3.get_key('config.yml')
        if key.exists():
            key.get_contents_to_filename(config_file)
            sys.stderr.write("Wrote config file based on copy on s3\n")

    wsgi_file = WEBAPP_PATH
    if os.path.isfile(wsgi_file):
        with open(wsgi_file, 'a'):
            os.utime(wsgi_file, None)
    sys.exit(0)


if __name__ == "__main__":
    main()
Ejemplo n.º 15
0
import sys
import os
import docassemble.webapp.config
from docassemble.webapp.config import daconfig, s3_config, S3_ENABLED
if __name__ == "__main__":
    docassemble.webapp.config.load(arguments=sys.argv)
WEBAPP_PATH = daconfig.get('webapp', '/usr/share/docassemble/webapp/docassemble.wsgi')

def main():
    if S3_ENABLED:
        import docassemble.webapp.amazon
        s3 = docassemble.webapp.amazon.s3object(s3_config)
        key = s3.get_key('config.yml')
        if key.exists():
            key.get_contents_to_filename(config_file)
            sys.stderr.write("Wrote config file based on copy on s3\n")

    wsgi_file = WEBAPP_PATH
    if os.path.isfile(wsgi_file):
        with open(wsgi_file, 'a'):
            os.utime(wsgi_file, None)
    sys.exit(0)

if __name__ == "__main__":
    main()