def populate_user_with_linkedin_data( user, user_data, companies_info, positions, schools_info, educations, indirect=False ): user.linkedin_id = user_data["id"] if "emailAddress" in user_data: user.email = user_data["emailAddress"].encode("utf8") if "firstName" in user_data: user.first_name = user_data["firstName"].encode("utf8") if "lastName" in user_data: user.last_name = user_data["lastName"].encode("utf8") user.name = user.first_name + " " + user.last_name # print user.name if "headline" in user_data: user.headline = user_data["headline"].encode("utf8") user.picture_url = user_data.get("pictureUrl") if "siteStandardProfileRequest" in user_data: user.linkedin_url = user_data["siteStandardProfileRequest"]["url"] if not indirect: user.last_linkedin_update = datetime.utcnow() if "positions" in user_data and "values" in user_data["positions"]: for position_data in user_data["positions"]["values"]: position = Position() populate_position_with_linkedin_data(position, position_data) if not indirect: position.last_linkedin_update = datetime.utcnow() if position_data["company"].get("id"): # if the company exists, use its linkedin id to find it later company_linkedin_id = str(position_data["company"].get("id")) # important! convert to string else: # otherwise, generate a fake id company_linkedin_id = gen_fake_linkedin_id() company_name = position_data["company"].get("name") # TODO make it more intelligent -- but for now some companies don't have company_name... WTF linkedin... if company_linkedin_id and company_name: companies_info.append({"name": company_name, "linkedin_id": company_linkedin_id}) positions.append((position, company_linkedin_id, user.linkedin_id)) if "educations" in user_data and "values" in user_data["educations"]: for education_data in user_data["educations"]["values"]: education = Education() populate_education_with_linkedin_data(education, education_data) if not indirect: education.last_linkedin_update = datetime.utcnow() school_name = education_data.get("schoolName") school_identifier = ( school_name.lower() if school_name else None ) # we don't have linkedin id's like we do for companies -> cross-reference by name if school_name: schools_info.append({"name": school_name, "identifier": school_identifier}) educations.append((education, school_identifier, user.linkedin_id))
def populate(): Position.insert_positions() with app.open_resource('./testing/data/teams.json') as teamdata: team_dict = json.load(teamdata)['teams'] team_models = [] for t in team_dict: Team.add_team(name=t['name'], conference=t['conference'], gm=t['gm'], season=2014) with app.open_resource('./testing/data/players.json') as playerdata: player_dict = json.load(playerdata)['players'] for p in player_dict: Player.add_player(name=p['name'], positions=p['positions'], team=p['team']) Player_Season populate_games() populate_stats()
# add Department dep1 = Department(id=1, department_name='Admin') db.session.add(dep1) dep2 = Department(id=2, department_name='Business') db.session.add(dep2) dep3 = Department(id=3, department_name='Support') db.session.add(dep3) dep4 = Department(id=4, department_name='Devs') db.session.add(dep4) dep5 = Department(id=5, department_name='HR') db.session.add(dep5) dep6 = Department(id=6, department_name='Test') db.session.add(dep6) # add Position pos1 = Position(id=1, position_name='Intern') db.session.add(pos1) pos2 = Position(id=2, position_name='Junior') db.session.add(pos2) pos3 = Department(id=3, position_name='Regular') db.session.add(pos3) pos4 = Position(id=4, position_name='Senior') db.session.add(pos4) pos5 = Department(id=5, position_name='Specialist') db.session.add(pos5) pos6 = Position(id=6, position_name='Team Leader') db.session.add(pos6) pos7 = Department(id=7, position_name='Boss') db.session.add(pos7) pos8 = Position(id=8, position_name='CEO') db.session.add(pos8)
def update_position(self): task = Task(name=NameTask.updating_positions.value) task.success = False task.result_msg = "Start update_position" db.session.add(task) db.session.commit() categories_have_positions = db.session.query(Category).filter(Category.turn, Category.nl_leaf).all() for category in categories_have_positions: response = NLReceiver(app.config["NL_GOODS"]["URL"].format(catalog_name=app.config["NL_CATALOG_MAIN"], category_id=category.nl_id), app.config["NL_GOODS"]["DATA_KEY"]) for position in Position.gen_el_to_db(response.data): try: db.session.add(position) db.session.commit() except Exception as e: # logger_app.error("{}: {}".format("Add Position: ", str(e))) db.session.rollback() position_update = Position.update_position(position) try: db.session.add(position_update) db.session.commit() except Exception as e: logger_app.error("{}: {}".format("Update Position: ", str(e))) db.session.rollback() else: ######################### # This place GET characteristics to position response = NLReceiver(app.config["NL_GOOD"]["URL"].format(catalog_name=app.config["NL_CATALOG_MAIN"], category_id=category.nl_id, position_id=position.nl_id), app.config["NL_GOOD"]["DATA_KEY"]) characteristics = Characteristic.gen_el_to_db(position, response.data) or {} for characteristic in characteristics: try: db.session.add(characteristic) db.session.commit() except Exception as e: db.session.rollback() ######################### # This place GET image to position response = NLReceiver(app.config["NL_IMG_GOOD"]["URL"].format(goodsId=position.nl_id), app.config["NL_IMG_GOOD"]["DATA_KEY"]) m = hashlib.md5() if not response.data: with open(app.config["DEFAULT_IMAGE_FOR_CATALOG"], "rb") as response_image: response_image_content = response_image.read() m.update(response_image_content) else: url_image = response.data["items"][0]["properties"]["Url"] response_image = requests.get( url_image.rsplit("&", 1)[0] + app.config["LOGOTYPE"]) # it is string fot URL response_image_content = response_image.content m.update(response_image_content) hash_image = m.hexdigest() image = db.session.query(Image).filter(Image.hash==hash_image).first() if not image: sub_folder_name = hash_image[0:2] sub_folder = os.path.join(app.config["UPLOAD_FOLDER"], sub_folder_name) if not os.path.exists(sub_folder): os.mkdir(sub_folder) image_name = hash_image + ".jpg" path_to_image = os.path.join(sub_folder, image_name) with open(path_to_image, "wb") as f: f.write(response_image_content) image = Image(original_name=image_name, name=image_name, hash=hash_image, path=os.path.join(sub_folder_name, image_name)) position.images.append(image) db.session.add(position) db.session.commit() task.success = True task.result_msg = "Finish update_position" db.session.add(task) db.session.commit()
def fake_data(): """ flask fake-data """ # if not Role.query.first(): # create_role() r_a = db.session.query(Role).filter(Role.name == 'admin').first() r_m = db.session.query(Role).filter(Role.name == 'moderator').first() r_u = db.session.query(Role).filter(Role.name == 'user').first() desc = 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. '\ 'Cum sociis natoque penatibus et' o = Organization(name='OOO Organization of Project', description=desc) d = Department(name='Group of accountants', description=desc) d.organization = o d1 = Department(name='Group of lawyers', description=desc) d1.organization = o d2 = Department(name='Administrators', description=desc) d2.organization = o admin_user = User(name='Pety', second_name='Petrovich', last_name='Petrov', username='******', email='*****@*****.**', description='it is admin') admin_user.set_password('pass') admin_user.department = d2 admin_user.role = r_a p = Position(name='Accountant', description=desc) p1 = Position(name='Lawyer', description=desc) u = User(name='Ivan', second_name='Ivanovich', last_name='Ivanov', username='******', email='*****@*****.**', description=desc) u.set_password('pass') u.department = d u.position = p u.role = r_u u1 = User(name='Vladimir', second_name='Vladimirovich', last_name='Ivanov', username='******', email='*****@*****.**', description=desc) u1.set_password('pass') u1.department = d1 u1.position = p1 u1.role = r_m ord_ = Order(name='Problems with access to the Internet', description=desc) ord_.user = u ord1 = Order(name='The printer is not working', description=desc) ord1.user = u1 db.session.add_all([ord_, ord1]) db.session.commit() g = GroupOrder(name='General problem', description=desc) g.user_performer = admin_user ord_.group_order = g ord1.group_order = g db.session.add_all([ord_, ord1]) db.session.commit() s = Service(name='The Internet', description=desc) s1 = Service(name='Copying and copying equipment', description=desc) g.services.append(s) g.services.append(s1) db.session.add_all([g, g]) db.session.commit() r = Result(name='Performance of equipment', description='it is ok') g.results.append(r) r1 = Result(name='A small salary for a system administrator', description='It is necessary to increase the salary ' 'of the system administrator') r1.positive = True g.results.append(r1) db.session.add_all([g, g]) db.session.commit() n = Note(name='some note') n1 = Note(name='some note 1') n2 = Note( name= 'Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the' ' industry\'s standard dummy text ever since the 1500s, when an unknown printer took a galley of ' 'type and scrambled it to make a type specimen book.') c = Consultation( name= 'Lorem Ipsum is simply dummy text of the printing and typesetting industry', description= 'Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem ' 'Ipsum has been the industry\'s standard dummy text ever since the 1500s, when an ' 'unknown printer took a galley of type and scrambled it to make a type specimen bok.', organization='Some Organization') admin_user.notes.append(n) admin_user.notes.append(n1) admin_user.notes.append(n2) admin_user.consultations.append(c) db.session.add_all([admin_user]) db.session.commit()