Пример #1
0
def enable_accounts(db):
    view = db.view('account/status', include_docs=True, key=constants.PENDING)
    for row in view:
        account = row.doc
        account['status'] = constants.ENABLED
        account['password'] = None
        account['code'] = utils.get_iuid()
        account['modified'] = utils.timestamp()
        db.save(account)
        utils.log(db,
                  None,
                  account,
                  changed=dict(password=None, status=constants.ENABLED))
        # Prepare message to send later
        try:
            template = settings['ACCOUNT_MESSAGES']['enabled']
        except KeyError:
            pass
        else:
            with MessageSaver(db=db) as saver:
                saver.set_params(account=account['email'],
                                 password_url=absolute_reverse_url('password'),
                                 password_code_url=absolute_reverse_url(
                                     'password',
                                     email=account['email'],
                                     code=account['code']),
                                 code=account['code'])
                saver.set_template(template)
                saver['recipients'] = [account['email']]
        print(account['email'])
        time.sleep(PAUSE)
Пример #2
0
def enable_accounts(db):
    view = db.view('account/status',
                   include_docs=True,
                   key=constants.PENDING)
    for row in view:
        account = row.doc
        account['status'] = constants.ENABLED
        account['password'] = None
        account['code'] = utils.get_iuid()
        account['modified'] = utils.timestamp()
        db.save(account)
        utils.log(db, None, account,
                  changed=dict(password=None, status=constants.ENABLED))
        # Prepare message to send later
        try:
            template = settings['ACCOUNT_MESSAGES']['enabled']
        except KeyError:
            pass
        else:
            with MessageSaver(db=db) as saver:
                saver.set_params(
                    account=account['email'],
                    password_url=absolute_reverse_url('password'),
                    password_code_url=absolute_reverse_url(
                        'password',
                        email=account['email'],
                        code=account['code']),
                    code=account['code'])
                saver.set_template(template)
                saver['recipients'] = [account['email']]
        print(account['email'])
        time.sleep(PAUSE)
Пример #3
0
 def __init__(self, doc=None, rqh=None, db=None):
     assert self.doctype
     if rqh is not None:
         self.rqh = rqh
         self.db = rqh.db
     elif db is not None:
         self.rqh = None
         self.db = db
     else:
         raise AttributeError('neither db nor rqh given')
     self.doc = doc or dict()
     self.changed = dict()
     if '_id' in self.doc:
         assert self.doctype == self.doc[constants.DOCTYPE]
     else:
         self.doc[constants.DOCTYPE] = self.doctype
         self.doc['_id'] = utils.get_iuid()
         self.initialize()
     self.setup()
Пример #4
0
 def __init__(self, doc=None, rqh=None, db=None):
     assert self.doctype
     if rqh is not None:
         self.rqh = rqh
         self.db = rqh.db
     elif db is not None:
         self.rqh = None
         self.db = db
     else:
         raise AttributeError('neither db nor rqh given')
     self.doc = doc or dict()
     self.changed = dict()
     if '_id' in self.doc:
         assert self.doctype == self.doc[constants.DOCTYPE]
     else:
         self.doc[constants.DOCTYPE] = self.doctype
         self.doc['_id'] = utils.get_iuid()
         self.initialize()
     self.setup()
Пример #5
0
 def reset_password(self):
     "Invalidate any previous password and set activation code."
     self.erase_password()
     self['code'] = utils.get_iuid()
Пример #6
0
                    saver['subject'] = None
                saver['address'] = dict(
                    address=self.get_argument('address', None),
                    zip=self.get_argument('zip', None),
                    city=self.get_argument('city', None),
                    country=self.get_argument('country', None))
                saver['invoice_ref'] = self.get_argument('invoice_ref', None)
                saver['invoice_address'] = dict(
                    address=self.get_argument('invoice_address', None),
                    zip=self.get_argument('invoice_zip', None),
                    city=self.get_argument('invoice_city', None),
                    country=self.get_argument('invoice_country', None))
                saver['phone'] = self.get_argument('phone', None)
                saver['other_data'] = self.get_argument('other_data', None)
                if utils.to_bool(self.get_argument('api_key', False)):
                    saver['api_key'] = utils.get_iuid()
                saver['update_info'] = False
                saver.check_required()
        except ValueError, msg:
            self.see_other('account_edit', account['email'], error=str(msg))
        else:
            self.see_other('account', account['email'])


class Login(RequestHandler):
    "Login to a account account. Set a secure cookie."

    def get(self):
        self.render('login.html', next=self.get_argument('next', None))

    def post(self):
 def post(self, email):
     try:
         account = self.get_account(email)
         self.check_editable(account)
     except ValueError as msg:
         self.see_other('account_edit', account['email'], error=str(msg))
         return
     try:
         with AccountSaver(doc=account, rqh=self) as saver:
             # Only admin may change role of an account.
             if self.is_admin():
                 role = self.get_argument('role')
                 if role not in constants.ACCOUNT_ROLES:
                     raise ValueError('Invalid role.')
                 saver['role'] = role
             saver['first_name'] = self.get_argument('first_name')
             saver['last_name'] = self.get_argument('last_name')
             university = self.get_argument('university', None)
             if not university:
                 university = self.get_argument('university_other', None)
             saver['university'] = university
             saver['department'] = self.get_argument('department', None)
             saver['pi'] = utils.to_bool(self.get_argument('pi', False))
             try:
                 saver['gender'] = self.get_argument('gender').lower()
             except tornado.web.MissingArgumentError:
                 try:
                     del saver['gender']
                 except KeyError:
                     pass
             try:
                 saver['group_size'] = self.get_argument('group_size')
             except tornado.web.MissingArgumentError:
                 try:
                     del saver['group_size']
                 except KeyError:
                     pass
             try:
                 saver['subject'] = int(self.get_argument('subject'))
             except (tornado.web.MissingArgumentError, ValueError,
                     TypeError):
                 saver['subject'] = None
             saver['address'] = dict(
                 address=self.get_argument('address', None),
                 zip=self.get_argument('zip', None),
                 city=self.get_argument('city', None),
                 country=self.get_argument('country', None))
             saver['invoice_ref'] = self.get_argument('invoice_ref', None)
             saver['invoice_address'] = dict(
                 address=self.get_argument('invoice_address', None),
                 zip=self.get_argument('invoice_zip', None),
                 city=self.get_argument('invoice_city', None),
                 country=self.get_argument('invoice_country', None))
             saver['phone'] = self.get_argument('phone', None)
             saver['other_data'] = self.get_argument('other_data', None)
             if utils.to_bool(self.get_argument('api_key', False)):
                 saver['api_key'] = utils.get_iuid()
             saver['update_info'] = False
             saver.check_required()
     except ValueError as msg:
         self.see_other('account_edit', account['email'], error=str(msg))
     else:
         self.see_other('account', account['email'])
Пример #8
0
def load_accounts(db, filepath='users.json'):
    with open(filepath) as infile:
        accounts = json.load(infile)
    print(len(accounts), 'accounts in input file')
    counter = 0
    for account in accounts:
        doc = collections.OrderedDict()
        doc['_id'] = utils.get_iuid()
        doc[constants.DOCTYPE] = constants.ACCOUNT
        email = account['mail'].lower()
        docs = [r.doc
                for r in db.view('account/email', include_docs=True, key=email)]
        if len(docs) > 0:
            print(email, 'exists already; skipped')
            continue
        status = account['status']
        if status != '1':
            print(email, 'status', status, '; skipped')
            continue
        doc['email'] = email
        doc['status'] = constants.PENDING
        doc['first_name'] = account['field_user_address'].get('first_name')
        doc['last_name'] = account['field_user_address'].get('last_name')
        # Address
        address = account['field_user_address'].get('thoroughfare') or ''
        premise = account['field_user_address'].get('premise')
        if address and premise:
            address += '\n' + premise
        elif premise:
            address = premise
        zip = account['field_user_address'].get('postal_code')
        city = account['field_user_address'].get('locality')
        country = account['field_user_address'].get('country') or 'SE'
        doc['address'] = dict(address=address,
                              zip=zip,
                              city=city,
                              country=country)
        doc['invoice_address'] = dict(address=address,
                                      zip=zip,
                                      city=city,
                                      country=country)
        doc['invoice_ref'] = None
        university = account['field_user_address'].get('organisation_name')
        university = ' '.join(university.replace(',', ' ').strip().split())
        uni = utils.to_ascii(university)
        try:
            uni = uni.split()[0]
        except IndexError:
            university = None
        else:
            if uni.upper() not in settings['UNIVERSITIES']:
                try:
                    university = UNI_LOOKUP[uni.lower()]
                except KeyError:
                    university_lower = university.lower()
                    try:
                        university = UNI_LOOKUP[university_lower]
                    except KeyError:
                        for uni in UNI_LOOKUP:
                            if uni in university_lower:
                                university = UNI_LOOKUP[uni]
                                break
        doc['university'] = university
        doc['department'] = None
        other_data = []
        try:
            other_data.append(u"old portal name: {0}".format(account['name']))
        except KeyError:
            pass
        try:
            other_data.append(u"old portal uid: {0}".format(account['uid']))
        except KeyError:
            pass
        doc['other_data'] = '\n'.join(other_data)
        doc['update_info'] = True
        doc['password'] = None
        # Role '2' is Drupal admin
        doc['role'] = set(account['roles']).difference(set([2])) \
            and constants.ADMIN or constants.USER
        doc['owner'] = email
        doc['created'] = utils.epoch_to_iso(account['created'])
        doc['modified'] = utils.epoch_to_iso(account.get('last_access') or account['created'])
        print('loaded', email)
        db.save(doc)

        counter += 1
    print(counter, 'accounts loaded')
Пример #9
0
def load_accounts(db, filepath='users.json'):
    with open(filepath) as infile:
        accounts = json.load(infile)
    print(len(accounts), 'accounts in input file')
    counter = 0
    for account in accounts:
        doc = collections.OrderedDict()
        doc['_id'] = utils.get_iuid()
        doc[constants.DOCTYPE] = constants.ACCOUNT
        email = account['mail'].lower()
        docs = [
            r.doc
            for r in db.view('account/email', include_docs=True, key=email)
        ]
        if len(docs) > 0:
            print(email, 'exists already; skipped')
            continue
        status = account['status']
        if status != '1':
            print(email, 'status', status, '; skipped')
            continue
        doc['email'] = email
        doc['status'] = constants.PENDING
        doc['first_name'] = account['field_user_address'].get('first_name')
        doc['last_name'] = account['field_user_address'].get('last_name')
        # Address
        address = account['field_user_address'].get('thoroughfare') or ''
        premise = account['field_user_address'].get('premise')
        if address and premise:
            address += '\n' + premise
        elif premise:
            address = premise
        zip = account['field_user_address'].get('postal_code')
        city = account['field_user_address'].get('locality')
        country = account['field_user_address'].get('country') or 'SE'
        doc['address'] = dict(address=address,
                              zip=zip,
                              city=city,
                              country=country)
        doc['invoice_address'] = dict(address=address,
                                      zip=zip,
                                      city=city,
                                      country=country)
        doc['invoice_ref'] = None
        university = account['field_user_address'].get('organisation_name')
        university = ' '.join(university.replace(',', ' ').strip().split())
        uni = utils.to_ascii(university)
        try:
            uni = uni.split()[0]
        except IndexError:
            university = None
        else:
            if uni.upper() not in settings['UNIVERSITIES']:
                try:
                    university = UNI_LOOKUP[uni.lower()]
                except KeyError:
                    university_lower = university.lower()
                    try:
                        university = UNI_LOOKUP[university_lower]
                    except KeyError:
                        for uni in UNI_LOOKUP:
                            if uni in university_lower:
                                university = UNI_LOOKUP[uni]
                                break
        doc['university'] = university
        doc['department'] = None
        other_data = []
        try:
            other_data.append(u"old portal name: {0}".format(account['name']))
        except KeyError:
            pass
        try:
            other_data.append(u"old portal uid: {0}".format(account['uid']))
        except KeyError:
            pass
        doc['other_data'] = '\n'.join(other_data)
        doc['update_info'] = True
        doc['password'] = None
        # Role '2' is Drupal admin
        doc['role'] = set(account['roles']).difference(set([2])) \
            and constants.ADMIN or constants.USER
        doc['owner'] = email
        doc['created'] = utils.epoch_to_iso(account['created'])
        doc['modified'] = utils.epoch_to_iso(
            account.get('last_access') or account['created'])
        print('loaded', email)
        db.save(doc)

        counter += 1
    print(counter, 'accounts loaded')
Пример #10
0
 def reset_password(self):
     "Invalidate any previous password and set activation code."
     self.erase_password()
     self['code'] = utils.get_iuid()
Пример #11
0
                    saver['subject'] = None
                saver['address'] = dict(
                    address=self.get_argument('address', None),
                    zip=self.get_argument('zip', None),
                    city=self.get_argument('city', None),
                    country=self.get_argument('country', None))
                saver['invoice_ref'] = self.get_argument('invoice_ref', None)
                saver['invoice_address'] = dict(
                    address=self.get_argument('invoice_address', None),
                    zip=self.get_argument('invoice_zip', None),
                    city=self.get_argument('invoice_city', None),
                    country=self.get_argument('invoice_country', None))
                saver['phone'] = self.get_argument('phone', None)
                saver['other_data'] = self.get_argument('other_data', None)
                if utils.to_bool(self.get_argument('api_key', False)):
                    saver['api_key'] = utils.get_iuid()
                saver['update_info'] = False
                saver.check_required()
        except ValueError, msg:
            self.see_other('account_edit', account['email'], error=str(msg))
        else:
            self.see_other('account', account['email'])


class Login(RequestHandler):
    "Login to a account account. Set a secure cookie."

    def get(self):
        self.render('login.html', next=self.get_argument('next', None))

    def post(self):