def add_company_save(request): """ This method is for adding a new company Extended description of function. arg(request) """ if request.method != "POST": return HttpResponse("Method Not Allowed") else: company_name = request.POST.get("company_name") logo = request.FILES.get("logo") email = request.POST.get("email") website = request.POST.get("website") try: company_model = Company(name=company_name, email=email, website=website, logo=logo) company_model.save() messages.success(request, "Successfully Added A Company") return HttpResponseRedirect(reverse("add_company")) except: messages.error(request, "Failed to Add A Company") return HttpResponseRedirect(reverse("add_company"))
def new_multiple_companies(app): companies = [] for each in range(3): params = {'name': fake.company()} company = Company(**params) companies.append(company.save()) return companies
def __scrape_company(self): """ Scrapes company data from html elements Creates a Company object and saves to database """ registration_date = self.__get_soup_value('DataList1_Label5_0') registration_date = datetime.strptime(registration_date, '%m/%d/%Y %I:%M:%S %p') registration_date = make_aware(registration_date) personnel_data = self.soup.find('tr', {'id': 'Relations_ListView_Tr1'}) missing_personnel_data = False if personnel_data else True self.company = Company( cr_id=self.cr_id, cr_sub_id=self.cr_sub_id, source_url=self.source_url, registration_number=self.__get_soup_value('DataList1_Label1_0'), name=self.__get_soup_value('DataList1_Label2_0'), additional_name=self.__get_soup_value('DataList1_Label3_0'), governorate=self.governorate, registration_date=registration_date, record_type=self.__get_soup_value('DataList1_Label6_0'), company_status=self.__get_soup_value('DataList1_Label7_0'), company_duration=self.__get_soup_value('DataList1_Label8_0'), legal_form=self.__get_soup_value('DataList1_Label9_0'), capital=self.__get_soup_value('DataList1_Label10_0', 'decimal'), title=self.__get_soup_value('DataList1_Label11_0'), description=self.__get_soup_value('DataList1_Label12_0'), missing_personnel_data=missing_personnel_data) self.company.save()
def test_save(self, init_db): """Test for creating a new company Args: init_db(SQLAlchemy): fixture to initialize the test database """ params = {'name': fake.company()} company = Company(**params) assert company == company.save()
def create_entry(company_data, user_data): from api.models import Company, User try: company = Company(**company_data) company.save() user = User(**dict(company_id=company.id, **user_data)) user.save() except Exception as e: print(e) pass
def import_companies(self): csv_file = self.read_file('importer/data/companies.csv') print 'Importing Companies...' for row in csv_file: try: Company.objects.get(name=row[0]) print 'Company', row[0], 'already registered.' except ObjectDoesNotExist: company = Company() company.name = row[0] company.save()
def company(request): if request.method == 'GET': ticker = request.GET.get('ticker') name = Company.objects.get(ticker=ticker).name return HttpResponse('GET success: ' + ticker + ' ' + name) elif request.method == 'PUT': ticker = request.GET.get('ticker') name = request.GET.get('name') print(ticker, name) c = Company(ticker=ticker, name=name) c.save() return HttpResponse("PUT success: " + ticker + ' ' + name)
class CompanyModelTestCase(TestCase): """This class defines the test suite for the Company model.""" def setUp(self): """Define the test client and other test variables.""" self.company_name = "" self.company = Company(name=self.company_name) def test_model_can_create_a_company(self): """Test the company model can create a company.""" old_count = Company.objects.count() self.company.save() new_count = Company.objects.count() self.assertNotEqual(old_count, new_count)
def create(self, validated_data): client_data = copy.deepcopy(validated_data) del client_data['contacts'] contact_list_data = validated_data['contacts'] company = Company(**client_data) company.save() for contact_data in contact_list_data: contact = Contact(**contact_data) contact.save() companycontact = CompanyContact() companycontact.company = company companycontact.contact = contact companycontact.save() return company
def create(self, validated_data): company = Company() company.name = validated_data.get('name') company.description = validated_data.get('description') company.city = validated_data.get('city') company.address = validated_data.get('address') company.save() return company
def create_company(): data = request.get_json() logger.info("Data recieved: %s", data) if "name" not in data: msg = "Please supply a company name." logger.info(msg) return create_response(status=422, message=msg) if "company_type" not in data: msg = "Please supply a company_type." logger.info(msg) return create_response(status=422, message=msg) if "description" not in data: msg = "Please supply a company description." logger.info(msg) return create_response(status=422, message=msg) if "address" not in data: msg = "Please supply a company address." logger.info(msg) return create_response(status=422, message=msg) try: # create SQLAlchemy Objects new_company = Company(name=data["name"]) company_type = data[ "company_type"] #CompanyType(id=data["company_type"]) print(company_type) new_company.company_type = company_type new_company.description = data["description"] new_company.address = data["address"] new_company.phone = data["phone"] new_company.MSA = data["MSA"] new_company.NDS = data["NDS"] new_company.url = data["url"] new_company.logo = data["logo"] db.session.add_all([new_company]) db.session.commit() except Exception as e: print("+++++++++++++++++++++++++++++") print(e) return create_response(status=422, message=e) return create_response( message= f"Successfully created company {new_company.name} with id: {new_company.id}" )
def test_get(self, init_db, new_company): """Test for get method Args: init_db(SQLAlchemy): fixture to initialize the test database new_company (Company): Fixture to create a new company """ assert Company.get(new_company.id) == new_company
def handle(self, *args, **options): if len(Company.objects.all()) < 5: company_names = [ "charity", "grocery", "assignment", "industry", "negotiation", "mud", "performance", "committee", "patience", "meaning", "student", "teaching", "message", "agency", "county", "inspector", "president", "fortune", "instruction", "television", "estate", "inflation", "refrigerator", "examination", "organization", "death", "teacher", "warning", "idea", "psychology", "establishment", "mode", "session", "politics", "analysis", "transportation", "difference", "variation", "version", "information", "addition", "dinner", "homework", "protection", "property", "writing", "hotel", "possession", "problem" ] company_titles = ["LLC", "Inc", "Co", "Corp", "Ltd"] names = [ "Jacob", "Michael", "Matthew", "Joshua", "Christopher", "Nicholas", "Andrew", "Joseph", "Daniel", "Tyler", "William", "Brandon", "Ryan", "John", "Zachary", "David", "Anthony", "James", "Justin", "Alexander", "Jonathan", "Christian", "Austin", "Dylan", "Ethan", "Benjamin", "Noah", "Samuel", "Robert", "Nathan", "Cameron", "Kevin", "Thomas", "Jose", "Hunter", "Jordan", "Kyle", "Caleb", "Jason", "Logan", "Aaron", "Eric", "Brian", "Gabriel", "Adam", "Jack", "Isaiah", "Juan", "Luis", "Connor" ] last_names = [ "Smith", "Johnson", "Williams", "Brown", "Jones", "Miller", "Davis", "Garcia", "Rodriguez", "Wilson", "Martinez", "Anderson", "Taylor", "Thomas", "Hernandez", "Moore", "Martin", "Jackson", "Thompson", "White", "Lopez", "Lee", "Gonzalez", "Harris", "Clark", "Lewis", "Robinson", "Walker", "Perez", "Hall", "Young", "Allen", "Sanchez", "Wright", "King", "Scott", "Green", "Baker", "Adams", "Nelson", "Hill", "Ramirez", "Campbell", "Mitchell", "Roberts", "Carter", "Phillips", "Evans", "Turner", "Torres" ] for index, company in enumerate(company_names): print("*****Creating users for : " + company + " - " + str(index) + " of " + str(len(company_names)) + "*****") company = Company.create(company.capitalize() + " " + random.choice(company_titles)) for name in range(1, random.randrange(1, 20)): fullname = names[name] + " " + random.choice( last_names) + " " + str(random.randrange(1, 9999)) user = PerpayUser.objects.create_user(username=fullname, email=fullname + "@gmail.com", password="******", company=company.id) for i in range(1, random.randrange(1, 20)): Payment.create(random.randrange(1, 5000), user) else: self.stdout.write(self.style.SUCCESS('Skipped Database fill'))
def company_detail(request, company_id): try: company = Company.objects.get(id=company_id) except Company.DoesNotExist as e: return Response({'error': str(e)}) if request.method == 'GET': serializer = CompanySerializer2(company) return Response(serializer.data) elif request.method == 'PUT': serializer = CompanySerializer2(instance=company, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response({'error': serializer.errors}) elif request.method == 'DELETE': Company.delete() return Response({'deleted': True})
def create(self, validated_data): company = Company() company.name = validated_data.get('name', 'default name') company.description = validated_data.get('description', 'default description') company.save() return company
def set_companies(): tickers = ["FB", "AAPL", "NFLX", "GOOG"] for ticker in tickers: company_entries = Company.query.filter_by(ticker=ticker).count() if company_entries == 0: company_info = Finance().get_company_info(ticker) company = Company(short_name=company_info["shortName"], sector=company_info["sector"], address=company_info["address1"], ticker=ticker) db.session.add(company) db.session.commit() logger.info("#### Companies set ####")
def load_companies(qty): """Load mock companies into the DB.""" click.echo('Loading companies...') fake = Faker() for _ in range(int(qty)): company = Company( external_id=fake.ssn(), name=fake.company(), phone=fake.phone_number(), address=fake.address(), ) db.session.add(company) db.session.commit() click.echo('Data load success.')
def route_company_create(name, description, company_type): if company_type.lower() not in ["buyer", "seller"]: return abort( 400, "{0} is not a valid company type. Please choose buyer or seller.". format(company_type)) company = Company( name=name, description=description, company_type=company_type, ) db.session.add(company) db.session.commit() db.session.refresh(company) return company
def get(self): """ Gets companies list """ companies = Company.filter() company_schema = CompanySchema(many=True) return ( { "data": company_schema.dump(companies).data, "message": SUCCESS_MESSAGES["FETCHED"].format("Companies"), "status": "success", }, 200, )
def handle(self, *args, **options): # company = Company.objects.get_or_create("AdminCompany") if len(Company.objects.all()) ==0: company = Company.create("AdminCompany") user=get_user_model() user = PerpayUser.objects.create_superuser(username="******",email="*****@*****.**",password="******",company=company.id) for i in range(1,random.randrange(1,10)): Payment.create(random.randrange(1,5000),user) self.stdout.write(self.style.SUCCESS('Successfully created Admin user and Company')) else: self.stdout.write(self.style.SUCCESS('Skipped initial setup'))
def test_get_company_info(client): rs = client.get("/company-info") assert rs.status_code == 200 ret_dict = rs.json assert ret_dict["success"] == True assert ret_dict["result"]["data"] == [] # create Company and test whether it returns it temp_company = Company(address="1601 Willow Road", sector="Communication Services", short_name="Facebook, Inc.", ticker="FB") db.session.add(temp_company) db.session.commit() rs = client.get("/company-info") ret_dict = rs.json assert len(ret_dict["result"]["data"]) == 1 assert ret_dict["result"]["data"][0]["short_name"] == "Facebook"
def add_company(): data = company_schema.load(request.json) company = Company(**data) db.session.add(company) db.session.commit() return jsonify(company_schema.dump(company)), 201
class LCRScrape: def __init__(self, cr_sub_id, gov): self.cr_sub_id = cr_sub_id self.governorate = gov self.cr_id = str(gov) + str(cr_sub_id).zfill(9) self.source_url = constants.COMMERCIAL_REGISTRY_URL + self.cr_id self.soup = None self.company = None self.personnel = {} def extract_data(self): self.__get_soup() self.__get_company() self.__get_personnel() def __get_soup(self): try: r = requests.get(self.source_url) self.soup = BeautifulSoup(r.content, 'html.parser') except Exception as e: scrape_error = ScrapeError(cr_id=self.cr_id, model_type='UK', error_message=str(e)) scrape_error.save() raise Exception("Failed to get soup from source url") def __get_company(self): try: self.__scrape_company() except Exception as e: company_scrape_error = ScrapeError(cr_id=self.cr_id, model_type='CO', error_message=str(e)) company_scrape_error.save() raise Exception("Failed to scrape company") def __get_personnel(self): if self.company.missing_personnel_data: return False try: self.__scrape_personnel() except Exception as e: personnel_scrape_error = ScrapeError(cr_id=self.cr_id, model_type='PE', error_message=str(e)) personnel_scrape_error.save() raise Exception("Failed to scrape personnel table") def __scrape_company(self): """ Scrapes company data from html elements Creates a Company object and saves to database """ registration_date = self.__get_soup_value('DataList1_Label5_0') registration_date = datetime.strptime(registration_date, '%m/%d/%Y %I:%M:%S %p') registration_date = make_aware(registration_date) personnel_data = self.soup.find('tr', {'id': 'Relations_ListView_Tr1'}) missing_personnel_data = False if personnel_data else True self.company = Company( cr_id=self.cr_id, cr_sub_id=self.cr_sub_id, source_url=self.source_url, registration_number=self.__get_soup_value('DataList1_Label1_0'), name=self.__get_soup_value('DataList1_Label2_0'), additional_name=self.__get_soup_value('DataList1_Label3_0'), governorate=self.governorate, registration_date=registration_date, record_type=self.__get_soup_value('DataList1_Label6_0'), company_status=self.__get_soup_value('DataList1_Label7_0'), company_duration=self.__get_soup_value('DataList1_Label8_0'), legal_form=self.__get_soup_value('DataList1_Label9_0'), capital=self.__get_soup_value('DataList1_Label10_0', 'decimal'), title=self.__get_soup_value('DataList1_Label11_0'), description=self.__get_soup_value('DataList1_Label12_0'), missing_personnel_data=missing_personnel_data) self.company.save() def __scrape_personnel(self): """ Loops through personnel table on company website - Scrapes person data from html elements - If person has already been scraped - Update Person object to include extra relationship - Else - Create Person object and add to database """ table_rows = self.soup.find('tr', {'id': 'Relations_ListView_Tr1'}) table_rows = table_rows.parent.findAll('tr') table_rows = table_rows[1:] # Ignore column names person_dict = {} for index, row in enumerate(table_rows): name = self.__get_soup_value('Relations_ListView_desigLabel_' + str(index)) relationship = self.__get_soup_value( 'Relations_ListView_relLabel_' + str(index)) if name in person_dict.keys(): person = person_dict[name] person = self.__update_person(person, relationship, index) else: person = Person( name=name, company_id=self.company.id, nationality=self.__get_soup_value( 'Relations_ListView_countryLabel_' + str(index)), relationship=relationship, stock=self.__get_soup_value( 'Relations_ListView_a_valLabel_' + str(index), 'int'), quota=self.__get_soup_value( 'Relations_ListView_s_valLabel_' + str(index), 'int'), ratio=self.__get_soup_value( 'Relations_ListView_r_valLabel_' + str(index), 'int')) person_dict[name] = person self.personnel = person_dict.values() Person.objects.bulk_create(self.personnel) def __update_person(self, person, relationship, index): person.relationship = person.relationship + ' \\ ' + relationship stock = self.__get_soup_value( 'Relations_ListView_a_valLabel_' + str(index), 'int') quota = self.__get_soup_value( 'Relations_ListView_s_valLabel_' + str(index), 'int') ratio = self.__get_soup_value( 'Relations_ListView_r_valLabel_' + str(index), 'int') if person.stock == 0: person.stock = stock if person.quota == 0: person.quota = quota if person.ratio == 0: person.ratio = ratio def __get_soup_value(self, tag_id, cast_type='string'): """ Helper for soup element casting """ contents = self.soup.find('span', {'id': tag_id}).contents if contents: if cast_type == 'string': return str(contents[0]) elif cast_type == 'decimal': return Decimal(contents[0]) elif cast_type == 'int': return int(contents[0]) else: return ''
def create(self, validated_data): company = Company(**validated_data) company.save() return company
def new_company(app): params = { 'name': fake.company(), } company = Company(**params) return company.save()
def setUp(self): """Define the test client and other test variables.""" self.company_name = "" self.company = Company(name=self.company_name)
def create(self, validated_data): # {'name': 'new category 4'} # name='new category 4' company = Company(**validated_data) company.save() return company
from api.models import Company def get_url(item_name, code_df): code = code_df.query( "name=='{}'".format(item_name))['code'].to_string(index=False) url = 'http://finance.naver.com/item/sise_day.nhn?code={code}'.format( code=code.strip()) return url code_df = pd.read_html( 'http://kind.krx.co.kr/corpgeneral/corpList.do?method=download&searchType=13', header=0)[0] code_df.종목코드 = code_df.종목코드.map('{:06d}'.format) code_df = code_df[['회사명', '종목코드', '업종', '주요제품']] df_records = code_df.to_dict('records') # print(df_records) model_instances = [ Company(name=record['회사명'], code=record['종목코드'], category=record['업종'], main=record['주요제품']) for record in df_records ] Company.objects.all().delete() Company.objects.bulk_create(model_instances)
def create(self, validated_data): company = Company() company.name = validated_data.get('name') company.save() return company
import os os.environ.setdefault("DJANGO_SETTINGS_MODULE", "backend.settings") from django import setup setup() from api.models import Company companies = [ "トEタ自動車(株)", "ソフトパンクグループ(株)", "任天丼(株)", "ンニー(株)", "(株)NTTコドモ", "中内製薬(株)", "アマテラス製薬(株)", "丈田薬品工業(株)", "(株)目立製作所", "(株)木寸田製作所", "エヌスリー(株)", "往友商事(株)", ] for company in companies: company = Company(name=company) company.save()