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()
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
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)
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 = []
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)
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
def remove_connection(request): auth_user = User.bid(request.authenticated_userid) assert auth_user, 'No user' conn_id = request.matchdict.get('conn_id') conn = DBSession.query(Connection).filter(Connection.id == conn_id, Connection.by_user_id == auth_user.id).first() assert conn, 'You are not authorized to perform this action' DBSession.delete(conn) DBSession.flush() return ''
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
def send_company(request): cid = int(request.params.get('to')) data = {'company_id': cid, 'message': request.params.get('message')} conn = create_or_update_conn(request.user.id, None, None, **data) DBSession.flush() main_poc = find_poc(conn.company, request) poc_emails = [main_poc[0].email] if not request.params.get('poc_id'): poc_emails = [u.email for u in conn.company.employees if u.point_of_contact and u.email] conn.user = main_poc[0] send_mail(poc_emails, 'company_connect_request', request, {'main_poc': main_poc[0], 'conn': conn, 'hash': conn.code, 'message': prep_message(conn.message)}) request.session.flash(render_template('/messaging/company/success_requestcompany.jinja2', {}, request), 'popup') return HTTPFound(request.referer or '/alchemist_startups')
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'))
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'))}
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)
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)
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))
def initialize_weekly_focus(): if Settings.get('use_weekly_focus'): with transaction.manager: DBSession.query(User).update({User.weekly_focus: None}) DBSession.flush()