def test_add_company(self):
        """
        Create environment to test for every possible case--

         - Existing relationship but the name is different                 pk=10
         - No existing relationship, but the company exists in the database (as
           established by the BusinessUnit title matching a company name)  pk=11
         - No relationship and the company is not in the database          pk=12

        Start with  2 Company objects and 3 BusinessUnit objects
        End up with 3 Company objects and 3 BusinessUnit objects

        """

        for i in range(10, 4):
            add_company(BusinessUnit.get(id=i))

            # The names of the BU and the Co should be the same
            self.assertEquals(BusinessUnit.get(id=i).title,
                              Company.get(id=i).name,
                              msg="Company names do not match")

            # ensure the relationship was formed
            self.assertIn(Company.objects.get(id=i),
                          BusinessUnit.objects.get(id=i).company_set.all(),
                          msg="Company is not related to job feed")
Beispiel #2
0
def add_company(bu):
    """
    Add the company to the Django database if it doesn't exist already.
    If the company does exist, but the business unit isn't related to it,
    add the relationship.

    Inputs
    :bu: An instance of BusinessUnit, passed in from update_solr().

    Returns
    None.

    Writes/Modifies
    The Django database is updated to reflect a new company or the
    name change of an existing company.

    """
    # See if there is an existing relationship. For now, we are assuming only
    # one or zero instances of a company exist in the database--this may change
    companies = bu.company_set.all()
    if companies:
        co = companies[0]
    else:
        co = False

    # Attempt to look up the company by the name of the BusinessUnit title
    try:
        existing_company = Company.objects.get(user_created=False,
                                               name=bu.title)
    except Company.DoesNotExist:
        existing_company = False

    # If a matching company exists, associate bu with it.
    # This removes any existing relationships.
    if existing_company:
        logger.debug("Associating Company '%s' with BusinessUnit '%s'", co,
                     existing_company)
        bu.company_set = [existing_company]
        co = existing_company
        return co

    # If a relationship exists but the name is different
    # and the company is not tied to another business unit
    if co and bu.title and co.name != bu.title:
        if co.job_source_ids.all().count() == 1:
            logger.warn("Changing name of Company '%s' to BusinessUnit title"
                        " '%s'", co, bu.title)
            # change the name of the company related to the BusinessUnit object
            co.name = bu.title
            co.save()
            return co
        else:
            co = False

    # If a there is NOT a relationship, or the company is associated with
    # more than one business unit
    if not co:
        logger.info("Creating a company for BusinessUnit %s", bu)
        # Remove existing relationships from business unit
        # Create the company
        slug = slugify(bu.title)
        logo_url = '//d2e48ltfsb5exy.cloudfront.net/100x50/seo/%s.gif'
        co = Company(name=bu.title, company_slug=slug, logo_url=logo_url % slug)
        try:
            co.save()
        except IntegrityError:
            logging.error("Failed to add BUID %s to Company object with title, "
                          "%s. Company already exists." % (bu.id, bu.title))
        else:
            bu.company_set = [co]
            return co
Beispiel #3
0
def add_company(bu):
    """
    Add the company to the Django database if it doesn't exist already.
    If the company does exist, but the business unit isn't related to it,
    add the relationship.

    Inputs
    :bu: An instance of BusinessUnit, passed in from update_solr().

    Returns
    None.

    Writes/Modifies
    The Django database is updated to reflect a new company or the
    name change of an existing company.

    """
    # See if there is an existing relationship. For now, we are assuming only
    # one or zero instances of a company exist in the database--this may change
    companies = bu.company_set.all()
    if companies:
        co = companies[0]
    else:
        co = False

    # Attempt to look up the company by the name of the BusinessUnit title
    try:
        existing_company = Company.objects.get(user_created=False,
                                               name=bu.title)
    except Company.DoesNotExist:
        existing_company = False

    # If a matching company exists, associate bu with it.
    # This removes any existing relationships.
    if existing_company:
        logger.debug("Associating Company '%s' with BusinessUnit '%s'", co,
                     existing_company)
        bu.company_set = [existing_company]
        co = existing_company
        return co

    # If a relationship exists but the name is different
    # and the company is not tied to another business unit
    if co and bu.title and co.name != bu.title:
        if co.job_source_ids.all().count() == 1:
            logger.warn(
                "Changing name of Company '%s' to BusinessUnit title"
                " '%s'", co, bu.title)
            # change the name of the company related to the BusinessUnit object
            co.name = bu.title
            co.save()
            return co
        else:
            co = False

    # If a there is NOT a relationship, or the company is associated with
    # more than one business unit
    if not co:
        logger.info("Creating a company for BusinessUnit %s", bu)
        # Remove existing relationships from business unit
        # Create the company
        slug = slugify(bu.title)
        logo_url = '//d2e48ltfsb5exy.cloudfront.net/100x50/seo/%s.gif'
        co = Company(name=bu.title,
                     company_slug=slug,
                     logo_url=logo_url % slug)
        try:
            co.save()
        except IntegrityError:
            logging.error(
                "Failed to add BUID %s to Company object with title, "
                "%s. Company already exists." % (bu.id, bu.title))
        else:
            bu.company_set = [co]
            return co