Ejemplo n.º 1
0
    def setUp(self):
        from sqlalchemy import create_engine

        engine = create_engine('sqlite://')
        try:
            # nosetest vs pycharm test runner, lets flex
            settings = get_appsettings('../tests.ini')
        except:
            settings = get_appsettings('tests.ini')
        self.config = testing.setUp(settings=settings)
        DBSession.configure(bind=engine)
        Base.metadata.create_all(engine)
        with transaction.manager:
            self.founder = User(username='******', password='******', email='*****@*****.**', activated=True,
                                active=True, firstname='founder1_firstname', lastname='founder1_lastname',
                                city='founder1_city', state='founder1_state', title='founder1_title')
            self.company = BaseCompany(name='testcompany', founded=2015, description='company_desc',
                                       startup_teamdescription='company_team_desc', website='company_website',
                                       linkedin='company_linkedin', twitterhandle='company_twitter',
                                       angelcoprofile='company_angellist')
            self.admin = User(username='******', password='******', email=settings.get('admin_mail'), activated=False,
                              groups='admin')
            DBSession.add(self.founder)
            DBSession.add(self.company)
            DBSession.add(self.admin)
            for k, v in Settings.settings.iteritems():
                if type(v) == dict:
                    DBSession.add(Settings(key=unicode(k), value=unicode(v['value'])))
                else:
                    if isinstance(v, basestring) and v.startswith('app_config.'):
                        v = settings.get(v[len('app_config.'):])
                    DBSession.add(Settings(key=unicode(k), value=unicode(v)))
            DBSession.flush()
            self.user_id = self.founder.user_id
        self.founders_created = []
Ejemplo n.º 2
0
def handle_if_company(k, v, required=True, request=False):
    from alchemist.models.base import DBSession
    from alchemist.models.company import BaseCompany
    from alchemist.models.settings import Settings

    if k in ['company', 'company_id']:
        is_name = False
        try:
            company_id = int(v)
            company = DBSession.query(BaseCompany).get(company_id)
        except:
            v = v.replace(new_company_text, '')
            company = DBSession.query(BaseCompany).filter(BaseCompany.name == v).first()
            if not company:
                company = DBSession.query(BaseCompany).filter(BaseCompany.name.startswith(v.strip())).first()
            is_name = True
        assert not required or company, 'The company does not exist.'
        if not company and is_name:
            company = BaseCompany()
            company.name = v
            if request and request.referer and 'add/founder' in request.referer:
                company.is_alchemist = True
                company.alchemistclass = int(Settings.get('alchemistclass'))
                company.companytype = 'startup'
            DBSession.add(company)
            DBSession.flush()
        return company.id if k == 'company_id' else company
    return v
Ejemplo n.º 3
0
def add_empty_connection(request):
    user = User(primary_type='base')
    user.activated = True
    user.login_enabled = False
    DBSession.add(user)
    DBSession.flush()
    return HTTPFound('/base/%s' % user.id)
Ejemplo n.º 4
0
def create_conn(by_user_id, to_id):
    try:
        conn = Connection(by_user_id=by_user_id, user_id=to_id)
    except:
        logging.error('Please specify by_user_id and to_id fields', exc_info=1)
        return
    DBSession.add(conn)
    DBSession.flush()
    _, conn.code = gen_ver_code(conn.id, 14)
    return conn
Ejemplo n.º 5
0
Archivo: user.py Proyecto: RaHus/portal
 def company(self, value):
     if self.company and value and self.company.id == value.id:
         return
     if self.company:
         DBSession.query(UserXCompany).filter(UserXCompany.company == self.company,
                                              UserXCompany.user == self,
                                              UserXCompany.end_date == None). \
             update({'end_date': utcnow().datetime})
     DBSession.add(UserXCompany(company=value, user=self, start_date=utcnow().datetime,
                                relation=TypeOfRelation.employee))
Ejemplo n.º 6
0
def mark_unwanted(request):
    unwanted_user = int(request.matchdict.get('user_id'))
    weekly_focus = request.user.weekly_focus.lower().replace(' ', '_')
    rp = request.POST.get
    DBSession.add(UnWanted(by_user=request.user, user_id=unwanted_user,
                           weekly_focus=weekly_focus, connected=rp('connected') == 'true',
                           rating=rp('rating'), feedback=rp('feedback')))
    cache = get_region('main')
    cache.invalidate()
    return HTTPFound(request.referer or '/')
Ejemplo n.º 7
0
def vote_to_answer(request):
    aid = request.POST.get('aid', -1)
    answer = DBSession.query(Answers).filter(Answers.id == aid).first()
    vote = DBSession.query(AnswerVotes).filter(AnswerVotes.answer_id == aid).filter(
        AnswerVotes.user_id == request.user.id).first()
    if vote is not None:
        DBSession.delete(vote)
    else:
        vote = AnswerVotes(answer=answer, user=request.user)
        DBSession.add(vote)
    vote_count = DBSession.query(func.count(AnswerVotes.id)).filter(AnswerVotes.answer_id == aid).scalar()
    return {'votes': vote_count}
Ejemplo n.º 8
0
def add_company_to_notablecustomers_view(request):
    if request.method == 'POST':
        current_company_id = request.POST.get('company_id', False)
        current_company = DBSession.query(BaseCompany).filter(BaseCompany.id == current_company_id).first()
        assert current_company, 'The company you are trying to edit does not exist'
        new_company_name = request.POST.get('name')
        new_company = BaseCompany(name=new_company_name)
        DBSession.add(new_company)
        current_company.notablecustomers.append(new_company)
        return {'item': current_company,
                'is_my_comp': request.user and request.user.company_id and
                              request.user.company_id == current_company.id}
Ejemplo n.º 9
0
def add_user_view(request):
    utype = request.matchdict.get('type')
    user = {'type': utype}
    company_match = False
    if request.method == 'POST':
        try:
            user = User()
            for k, v in request.POST.iteritems():
                if v == 'on':
                    v = True
                v = {'true': True, 'false': False}.get(v, v)
                v = Tag.handle_if_tags(k, v)
                if k == 'company_id' or k == 'company':
                    try:
                        company_match = getattr(request.user, k) == int(v)
                    except:
                        company_match = request.user.company.name == v.strip() if request.user.company else False
                        v = v.replace(' - Add new company', '')
                    assert company_match or request.user.is_admin, \
                        'You just tried to add an user to a company You don\'t have access to. You can\'t do that!'
                    k = 'company'
                v = handle_if_company(k, v, required=False, request=request)
                v = handle_file_save('%s_adm_cr_%s' % (getattr(user, 'nicename', user.id),
                                                       request.authenticated_userid), v)
                setattr(user, k, v)

            plain_pass, _ = gen_ver_code(user.nicename)
            user.password = plain_pass
            user.activated = True
            user.login_enabled = False
            user.primary_type = utype
            if not user.username:
                user.username = user.email
            _, user.ver_code = gen_ver_code('signup_%s' % user.id)

            DBSession.add(user)
            DBSession.flush()
            admin_emails = Settings.get('admin_mail')
            send_mail(admin_emails, 'roleadd', request, {'user': user,
                                                         'new_role': utype})
            user.send_mail('invite', request, {'plain_pass': plain_pass,
                                               'user_id': user.id,
                                               'hash': user.ver_code})
            request.session.flash('Successfully added a new %s' % (
                'Team Member' if company_match else str(user.primary_type).capitalize()))
            url = '/%s/%s' % (user.primary_type, user.id)
            return HTTPFound(url)
        except Exception, e:
            request.session.flash('Error: %s' % e.message, 'error')
            if request.referer:
                return HTTPFound(request.referer)
            raise
Ejemplo n.º 10
0
def addto_track(request):
    qid = request.POST.get('qid', -1)
    question = DBSession.query(Questions).filter(Questions.id == qid).first()
    bookmark_ = (request.POST.get('type') == 'bookmark')
    existed = DBSession.query(Trackers).filter(Trackers.questions_id == qid).filter(
        Trackers.tracker_id == request.user.id,
        Trackers.is_bookmark == bookmark_).first()
    if existed:
        DBSession.delete(existed)
        return {'type': 'removed'}
    else:
        tracker = Trackers(tracker=request.user, question=question, is_bookmark=bookmark_)
        DBSession.add(tracker)
        return {'type': 'added'}
Ejemplo n.º 11
0
def add_answer(request):
    editor_tags = ['ul', 'li', 'ol', 'strong', 'em', 'p', 'hr', 'span', 'del', 'a', 'br']
    answer_text = bleach.clean(request.POST.get('answer', ''), editor_tags, {
        "*": ["style"],
        "img": ["src", "width", "height"],
    }, styles=['text-align'])
    qid = request.POST.get('qid', -1)
    question = DBSession.query(Questions).filter(Questions.id == qid).first()
    had_answers = len(question.answers)
    answer = Answers(user=request.user, question=question, text=answer_text)
    answer.created_at = datetime.now()  # TODO put as default (also for question)
    DBSession.add(answer)
    DBSession.flush()
    scheduler.add_job(send_emails_immediate_answer, next_run_time=now(scheduler.timezone).replace(seconds=+30).datetime,
                      kwargs={'answer_id': answer.id, 'attr_name': 'answer_notification',
                              'nice_type': 'New Answer', 'type': 'Answer'})
    return HTTPFound('/exchange/%s/show%s' % (qid, '?first_answer=1' if had_answers == 0 else '?answered=1'))
Ejemplo n.º 12
0
def add_question(request):
    question = {}
    if request.method == 'POST':
        questiontext = bleach.clean(request.POST.get('questiontext'), strip=True)
        str_tags = request.POST.get('hidTags')
        question = Questions(text=questiontext, user=request.user)
        question.tags = Tag.handle_if_tags('tags', str_tags)
        question.created_at = datetime.now()
        DBSession.add(question)
        tracker = Trackers(tracker=request.user, question=question, is_bookmark=False)
        DBSession.add(tracker)
        DBSession.flush()  # make sure there's no DB Errors before sending out the emails
        scheduler.add_job(send_emails_immediate_question,
                          next_run_time=now(scheduler.timezone).replace(seconds=+30).datetime,
                          kwargs={'question_id': question.id, 'attr_name': 'question_notification',
                                  'nice_type': 'New Question Asked', 'type': 'Question'})

    return {'question': question, 'now': now(request.registry.settings.get('scheduler.timezone'))}
Ejemplo n.º 13
0
 def __getattribute__(self, name):
     if 'tags' in name:
         name = 'tags'
     try:
         return super(UserMixin, self).__getattribute__(name)
     except AttributeError:
         if not self._user and self.user_id:
             self._user = User.bid(self.user_id)
         if not self._user:
             self._user = User()
             DBSession.add(self._user)
             DBSession.flush()
             self.user_id = self._user.id
         if name == 'type':
             return self._user.type.split(',')
         return getattr(self._user, name)
     except DetachedInstanceError:
         DBSession.add(self._user)
         return super(UserMixin, self).__getattribute__(name)
Ejemplo n.º 14
0
 def __setattr__(self, name, value):
     if name == 'user_id':
         if not self._user:
             self._user = DBSession.query(User).get(value)
         types = self._user.type.split(',')
         types.append(self.__tablename__)
         self._user.type = ','.join(set(types))
         return super(UserMixin, self).__setattr__(name, value)
     if 'tags' in name:
         name = 'tags'
     if hasattr(User, name):
         if not self._user and self.user_id:
             self._user = DBSession.query(User).get(self.user_id)
         if not self._user:
             _user = User()
             DBSession.add(_user)
             DBSession.flush()
             self.user_id = _user.id
         else:
             DBSession.add(self._user)
         try:
             return setattr(self._user, name, value)
         except DetachedInstanceError:
             DBSession.add(self._user)
             return setattr(self._user, name, value)
     else:
         return super(UserMixin, self).__setattr__(name, value)
Ejemplo n.º 15
0
def main(argv=sys.argv):
    if len(argv) < 2:
        usage(argv)
    config_uri = argv[1]
    options = parse_vars(argv[2:])
    setup_logging(config_uri)
    settings = get_appsettings(config_uri, options=options)
    engine = engine_from_config(settings, 'sqlalchemy.')
    DBSession.configure(bind=engine)
    Base.metadata.drop_all(engine)
    Base.metadata.create_all(engine)
    with transaction.manager:
        user = User(username='******', password='******', email=settings.get('admin_mail'), activated=True,
                    groups='admin')
        DBSession.add(user)
        DBSession.flush()
        for k, v in Settings.settings.iteritems():
            if type(v) == dict:
                DBSession.add(Settings(key=unicode(k), value=unicode(v['value'])))
            else:
                if isinstance(v, basestring) and v.startswith('app_config.'):
                    v = settings.get(v[len('app_config.'):])
                DBSession.add(Settings(key=unicode(k), value=unicode(v)))

        DBSession.flush()
Ejemplo n.º 16
0
 def __getattribute__(self, item):
     try:
         return super(User, self).__getattribute__(item)
     except DetachedInstanceError:  # if cached we need to attach session
         DBSession.add(self)
         return super(User, self).__getattribute__(item)
Ejemplo n.º 17
0
def useredit_ajax_view(request):
    auth_user = User.bid(request.authenticated_userid)
    userid = request.matchdict.get('user_id', auth_user.id) if auth_user.is_admin else auth_user.id
    current_type = request.matchdict.get('type')
    if not current_type:
        if 'connection' in request.path:
            current_type = 'connection'
        else:
            current_type = User.gettype(userid)
    realtype = all_types.get(current_type, current_type)
    if current_type == 'company':
        cid = request.matchdict.get('user_id')
        if 'company' in request.referer:
            item = DBSession.query(BaseCompany).get(cid)
        else:
            item = DBSession.query(BaseCompany).join(User).filter(User.id == cid).first()
        assert auth_user.is_admin or int(auth_user.id) in [e.id for e in item.employees], \
            'No rights to access this company'
    elif current_type == 'base':
        item = User.bid(userid, False)
    elif current_type == 'tag':
        item = DBSession.query(Tag).filter(Tag.id == userid).first()
        if not item and auth_user.is_admin and request.POST:
            item = Tag(text=request.POST.get('value', '').strip())
            DBSession.add(item)
    elif current_type == 'connection':
        con_id = request.matchdict.get('con_id')
        item = DBSession.query(Connection).filter(Connection.id == con_id).first() if con_id else None
    else:
        item = DBSession.query(realtype).join(User).filter(User.id == userid).first()
    if not item and current_type in user_types:
        item = realtype(user_id=userid)
        DBSession.add(item)
    if request.method == 'POST':
        post_data = request.POST
        field = post_data.get('name')
        value = post_data.get('value')
        select2_company_fields = ['notablecustomers', 'previous_companies', 'startup_wishcustomers',
                                  'areas_of_interest']
        select2_founder_fields = ['areas_of_interest']
        assert field not in getattr(item, 'only_admin_edit_fields',
                                    []) or auth_user.is_admin, 'Only admin can edit that field'
        try:
            value = value.strip()
        except AttributeError:
            pass
        if field == 'privatefields_values':
            value = cPickle.dumps(
                {k.replace('value[', '').replace(']', ''): v for k, v in post_data.items() if 'value' in k})
        elif field == 'acquireddate' or field == 'shutdowndate':
            value = datetime.strptime(value, '%d %b %Y') if value.strip() else None
        if field == 'name' and current_type == 'company':
            if value.strip() != item.name.strip():
                admin_mail = Settings.get('admin_mail').split(',')[0]
                send_mail(admin_mail, 'company_changed', request, {'name': value,
                                                                   'old_name': item.name})
        if field == 'password':
            # TODO move to model, use assertions
            if len(value) < 8 or not any(i.isdigit() for i in value):
                raise AssertionError('Min password length is 8 characters and must contain at least one digit')
        if not value and not field.endswith('tags') and field not in select2_company_fields:
            base_checkbox_field = post_data.get('value[]', '').split('_')[0]
            setattr(item, field, base_checkbox_field)

            for bool_field in item.boolean_fields:
                if bool_field.startswith(base_checkbox_field):
                    setattr(item, bool_field, False)

            other_field = '%s_other' % base_checkbox_field
            if hasattr(item, other_field):
                setattr(item, other_field, '')

            for field in post_data.getall('value[]'):
                if 'other' in field:
                    fsplit = field.split('_:::_')
                    if len(fsplit) > 1:
                        setattr(item, fsplit[0], fsplit[1])
                else:
                    setattr(item, field, True)

        elif field.endswith('tags') or field in select2_founder_fields:
            value = handle_if_tags(field, ','.join(post_data.getall('value[]')))
        elif field in select2_company_fields:
            value = handle_if_company_tags(field, ','.join(post_data.getall('value[]')))
        elif auth_user.is_admin:
            value = handle_if_company(field, value, required=False)
        handle_if_typechange(item, field, value)
        setattr(item, field, value)
        return 'OK'
    return ''
Ejemplo n.º 18
0
def register(request):
    form = RegistrationForm(request.POST)

    # Roles for front-end JS fields updating
    roles = DBSession.query(Role).all()
    roles_dict_for_js = json.dumps(dict([(str(i.id), i.title) for i in roles]))

    if request.POST and form.validate():
        duplicate = False
        try:
            duplicate = User.get_by_login(form.email.data.strip())
        except:
            logging.error('Could not check duplicate', exc_info=1)

        # dirty hack to walkaround fieldlist populating user error
        form_meetings = form.meetings
        del form.meetings

        # Updating preferred contact method, if "Other" was specified.
        if form.contact_method.data == 'Other (specify below)':
            form.contact_method.data = form.contact_method_other.data

        company = None
        try:
            company = DBSession.query(BaseCompany).get(int(form.company.data.replace('companies:', '')))
        except ValueError:
            # Creating a new company, if a new company was typed
            cleaned_name = form.company.data.replace(new_company_text, '').replace('companies:', '').strip()
            if cleaned_name:
                company = BaseCompany()
                company.name = cleaned_name
                DBSession.add(company)

        form.company.data = company

        # Creating new user
        new_user = User()

        for m2m_field in ['tags', 'areas_of_interest', 'industries_of_focus', 'speaking_topics',
                          'indinvestinterests', 'areas_of_service']:
            cleaned_data = map(lambda x: (x.split(':')[1] if len(x.split(':')) > 1 else x),
                               form[m2m_field].data.split(','))
            getattr(form, m2m_field).data = Tag.handle_if_tags('tags', ','.join(cleaned_data))

        form.populate_obj(new_user)
        new_user.activated = True
        new_user.login_enabled = False
        new_user.ver_code = ''

        # Make the primary_type, the highest number's match from above
        roles = form.roles.data
        new_user.primary_type = roles[0].title

        del form.tags  # ugly hack because this form library has no way to specify the relation of fields to objects
        if company:
            form.populate_obj(company)
        DBSession.add(new_user)

        # Creating new calendar
        if 'Regular Mentor' == new_user.mentor_type and 'mentor' in new_user.type:
            for subform in form_meetings:
                new_calendar = Calendar()
                new_calendar.user = new_user
                new_calendar.default_location = form.default_location.data
                new_calendar.start_date = form.start_date.data
                subform.form.populate_obj(new_calendar)
                DBSession.add(new_calendar)

        # Saving to Solve360 CRM
        new_user.assistant_name_for_solve = ' '.join([new_user.assistant_firstname, new_user.assistant_lastname])
        new_user.company_name_for_solve = company.name if company else None

        # Mondays 10am-12pm Bi-Weekly,
        # Wednesdays 7pm-10pm Weekly,
        # Wednesdays 7am-11am Bi-Weekly
        new_user._meetings = ',\r\n'.join(
            ['{weekday} {start_time}-{end_time} {event_frequency}'.format(**m.form.data) for m in form_meetings]
        )
        crm.create_contact(payload=crm.map_object(new_user))

        send_mail(
            to=Settings.get('registration_form_to', Settings.get('admin_mail')),
            etype='new_profile_admin',
            request=request,
            data={'u': new_user, 'duplicate': duplicate}
        )
        return HTTPFound(location='/register-profile-success')

    return {
        'form': form,
        'roles_dict_for_js': roles_dict_for_js,
    }
Ejemplo n.º 19
0
def add_company_view(request):
    if request.method == 'POST':
        company_name = request.POST.get('name')
        DBSession.add(BaseCompany(name=company_name))
        return {'added': company_name}
    return {}
Ejemplo n.º 20
0
def main(argv=sys.argv):
    if len(argv) < 2:
        usage(argv)
    config_uri = argv[1]
    options = parse_vars(argv[2:])
    setup_logging(config_uri)
    settings = get_appsettings(config_uri, options=options)
    engine = engine_from_config(settings, 'sqlalchemy.')
    DBSession.configure(bind=engine)
    Base.metadata.drop_all(engine)
    Base.metadata.create_all(engine)
    with transaction.manager:
        user = User(username='******', password='******', email=settings.get('admin_mail'), activated=True,
                    groups='admin')
        DBSession.add(user)
        DBSession.flush()
        for k, v in Settings.settings.iteritems():
            if type(v) == dict:
                DBSession.add(Settings(key=unicode(k), value=unicode(v['value'])))
            else:
                if isinstance(v, basestring) and v.startswith('app_config.'):
                    v = settings.get(v[len('app_config.'):])
                DBSession.add(Settings(key=unicode(k), value=unicode(v)))

        DBSession.flush()

        print 'Starting to import data from spreadsheets'
        # if 1:
        if 0:
            from gdata.spreadsheet.service import SpreadsheetsService

            founder_columns = {}
            founders = []
            to_founder_keys = {'Technical Co-Founder List?': 'technical',
                               'Email': 'email',
                               'Address': 'address',
                               'Alchemist Class #': 'alchemistclass',
                               'Public LinkedIn Profile': 'linkedin_profile',
                               'Areas of Expertise': 'tags',
                               'First Name': 'firstname',
                               'Last Name': 'lastname',
                               'Are you the CEO? (One and only one co-founder must be designated CEO)': 'ceo',
                               'Phone (Other)': '',
                               'Title': 'title',
                               'Are you an IoT company?': 'company.iot',
                               'Base Row': '',
                               'Company': 'company',
                               'Phone (Mobile)': 'phone',
                               }
            to_company_keys = {
                'name': 'name',
                'description': 'description',
                'twitterhandle': 'twitterhandle',
                'email': 'email',
                'website': 'website',
                'tags': 'tags',
                'braggingtext': 'braggingtext',
                'totalraise': 'totalraise',
                'haveraise': 'haveraise',
                'logo': 'logo',
                'alchemistclass': 'alchemistclass'
            }
            ss = SpreadsheetsService()
            ss.email = 'email'
            ss.password = '******'
            ss.ProgrammaticLogin()

            for shkey in []:
                print 'Starting to import companies from %s' % shkey
                linenr = 0
                linestoskip = 7
                headerline = 2
                company_columns = {}
                for t in ss.GetListFeed(shkey).entry:
                    linenr += 1
                    if headerline == linenr:
                        for k, v in t.custom.iteritems():
                            if (v.text or '').strip() in to_company_keys:
                                company_columns[k] = to_company_keys[v.text.strip()]
                        print 'Got company_columns %s' % company_columns
                    if linenr <= linestoskip:
                        print 'skipping line %s' % linenr
                        continue
                    company_values = {}
                    for k, v in t.custom.iteritems():
                        if k in company_columns:
                            val = (getattr(v, 'text', '') or '').strip()
                            if company_columns[k] in BaseCompany.boolean_fields:
                                val = val.lower() == 'yes'
                            company_values[company_columns[k]] = val
                    company_values['tags'] = handle_if_tags('tags', company_values.get('tags', '').strip())
                    if not (company_values and company_values.get('name', '').strip() and
                                company_values.get('description', '').strip()):
                        print 'skipping because no values %s ' % company_values
                        continue
                    if 'logo' in company_values:
                        logo_ = 'http%s' % company_values['logo'].split('http')[-1]
                        if 'dropbox' in logo_:
                            logo_ = logo_.split('?')[0] + '?raw=1'
                        elif 'google' in logo_:
                            logo_ = 'https://drive.google.com/uc?export=view&id=%s' % \
                                    logo_.split('/')[-2]
                        company_values['logo'] = logo_

                    company = DBSession.query(BaseCompany).filter(
                        BaseCompany.name == company_values['name']).first()
                    if company and company.description and company.email:
                        print 'not importing %s already filled some info' % company.name
                        continue
                    elif company:
                        for cf, cv in company_values.iteritems():
                            if hasattr(company, cf):
                                setattr(company, cf, cv)
                    else:
                        DBSession.add(BaseCompany(**company_values))

            DBSession.flush()

            print 'stating to import founders'

            for t in ss.GetListFeed('0As9IMrvjc_FqdGdiOHN3ZWhveWg5eHF1WVdmbGI4Vnc').entry:
                if not founder_columns:
                    for k, v in t.custom.iteritems():
                        founder_columns[k] = v.text
                else:
                    founder = {}
                    for k, v in t.custom.iteritems():
                        if k in founder_columns and founder_columns[k] and \
                                to_founder_keys[founder_columns[k].strip()]:
                            founder[to_founder_keys[founder_columns[k].strip()]] = v.text
                    founders.append(founder)

            for founder in founders:
                company_name = founder.pop('company')
                is_iot = founder.pop('company.iot') if 'company.iot' in founder else False
                address = founder.pop('address') if 'address' in founder else ''
                try:
                    if founder.get('email', '').strip():
                        assert re.match(r'[^@]+@[^@]+\.[^@]+', founder['email'].strip()), 'Invalid email address'
                except:
                    print 'invalid email %s' % founder['email'] if 'email' in founder else '(no email)'
                    founder['email'] = '*****@*****.**'
                if company_name:
                    for c_ext in ['INC', 'Inc.', 'Inc', 'LLC', 'llc']:
                        company_name = company_name.replace(c_ext, '').strip()
                    company = DBSession.query(BaseCompany).filter(
                        func.lower(BaseCompany.name) == func.lower(company_name)).first()
                    for split_str in ['/', '  ', ' ']:
                        if company:
                            break
                        for cn_split in company_name.split(split_str):
                            company = DBSession.query(BaseCompany).filter(
                                func.lower(BaseCompany.name).startswith(func.lower(cn_split.strip()))).first()
                            if company:
                                break
                if company:
                    company_id = company.id
                    if is_iot and is_iot.lower() == 'yes':
                        company.iot = True
                    c_address = getattr(company, 'address', '')
                    if address and (not c_address or not str(c_address).strip()):
                        company.address = address
                else:
                    company = BaseCompany(name=company_name, iot=(is_iot and is_iot.lower() == 'yes'),
                                          address=address)
                    DBSession.add(company)
                    DBSession.flush()
                    company_id = company.id
                for k in ['ceo', 'technical']:
                    if k not in founder:
                        founder[k] = False
                    founder[k] = str(founder[k]).lower() == 'yes'
                if company and not company.alchemistclass and 'alchemistclass' in founder and founder[
                    'alchemistclass'].strip():
                    try:
                        company.alchemistclass = int(founder['alchemistclass'].strip())
                    except:
                        pass
                if 'ceo' in founder and founder['ceo'] and not ('CEO' in founder['title'] or 'C.E.O' in founder['title']
                                                                or 'Chief Exec' in founder['title']):
                    founder['title'] += (', ' if founder['title'].strip() else '') + 'CEO'

                if 'ceo' in founder and 'title' in founder and \
                        not founder['ceo'] and ('CEO' in founder['title'] or 'C.E.O' in founder['title']
                                                or 'Chief Exec' in founder['title']):
                    founder['ceo'] = True
                founder['company_id'] = company_id
                founder['activated'] = True
                founder['active'] = True

                founder['tags'] = handle_if_tags('tags', founder.get('tags', '').strip(), [',', 'and', ';'])

                DBSession.add(Founder(**founder))
Ejemplo n.º 21
0
def email_migrate(argv=sys.argv):
    if len(argv) < 2:
        usage(argv)
    config_uri = argv[1]
    options = parse_vars(argv[2:])
    setup_logging(config_uri)
    settings = get_appsettings(config_uri, options=options)
    engine = engine_from_config(settings, 'sqlalchemy.')
    DBSession.configure(bind=engine)

    from petl import fromcsv
    conn_data = fromcsv('import_connections.csv').dicts()
    # conn_data = []
    users_changed = []
    users_added = 0
    state_to_abbr = dict([(x[1], x[0]) for x in USPS_CHOICES])
    print 'processing connections'
    with transaction.manager:
        for v in conn_data:
            user = DBSession.query(User).filter(
                or_(func.lower(User.email) == v['_email'].lower(),
                    func.lower(User.linkedin_email) == v['_email'].lower(),
                    and_(func.lower(User.firstname) == v['firstname'].lower(),
                         func.lower(User.lastname) == v['lastname'].lower()))
            ).first()
            if user:
                for attrn, val in v.iteritems():
                    if not val or not attrn or attrn in ('_email',):
                        continue
                    val = val.strip()
                    if attrn == 'company' and not user.company_id:
                        repl_val = val.lower().rstrip('.').replace('llc', '').replace('corporation', ''). \
                            replace('corp', '').replace('inc', '').rstrip(',').strip()
                        print 'searching:', repl_val, val
                        company = DBSession.query(BaseCompany).filter(
                            or_(BaseCompany.name == val,
                                func.lower(BaseCompany.name) == val.lower(),
                                func.regexp_replace(func.lower(func.btrim(BaseCompany.name)),
                                                    ',\s(inc|corporation|corp|llc)\.?$', '') == repl_val,
                                func.regexp_replace(func.lower(func.btrim(BaseCompany.name)),
                                                    '\s(inc|corporation|corp|llc)\.?$', '') == repl_val
                                )).first()
                        if not company and val:
                            company = BaseCompany(name=val)
                            DBSession.add(company)
                        val = company
                    if attrn == 'area of expertise' and val:
                        attrn = 'tag'
                        tag = DBSession.query(Tag).filter(Tag.text == val).first()
                        if tag and len(filter(lambda t: t.text == val, user.tags)) == 0:
                            user.tags.append(tag)
                    if attrn == 'state':
                        val = state_to_abbr.get(val)
                    if val and hasattr(user, attrn) and not getattr(user, attrn):
                        print 'setting connection.%s.%s = %s' % (user.id, attrn, val)
                        setattr(user, attrn, val)
                        users_changed.append(user.id)
        DBSession.flush()
        users_changed = list(set(users_changed))
        print '%s new users added, %s users changed: %s' % (users_added, len(users_changed), users_changed)

    del conn_data

    comp_data = fromcsv('companies_data.csv').dicts()
    companys_changed = []
    companys_added = 0
    print 'starting to process companies'
    with transaction.manager:
        for v in comp_data:
            if not v['name'].strip():
                continue
            company = DBSession.query(BaseCompany).filter(
                or_(BaseCompany.name == v['name'].strip())
            ).first()
            if company:
                for attrn, val in v.iteritems():
                    if not val or not attrn or attrn in ('_email',):
                        continue
                    val = val.strip()
                    attrn = attrn.replace('company_', '')
                    if attrn == 'founded':
                        try:
                            val = int(val)
                        except:
                            continue

                    if val and hasattr(company, attrn) and not getattr(company, attrn):
                        print 'setting company.%s.%s = %s' % (company.id, attrn, val)
                        setattr(company, attrn, val)
                        companys_changed.append(company.id)
        DBSession.flush()
        companys_changed = list(set(companys_changed))
        print '%s new companys added, %s companys changed: %s' % (
            companys_added, len(companys_changed), companys_changed)
Ejemplo n.º 22
0
def setting_add_view(request):
    key = request.matchdict.get('key_to_add')
    DBSession.add(Settings(key=key, value=''))
    return HTTPFound('/settings')
Ejemplo n.º 23
0
 def notificate(self, text):
     return DBSession.add(Notification(user=self, message=text))