Beispiel #1
0
    def create_person(cls):
        """
        create a random person in the db and return the person just created
        if the person already exists, return it
        """

        try:
            op_path = os.path.join(settings.PROJECT_ROOT, "testdatabuilder", "openpolis_samples")
            f = open(os.path.join(op_path, "op_politician_first_names_sex.csv"), "r")
            l = open(os.path.join(op_path, "op_politician_last_names.csv"), "r")
            loc = open(os.path.join(op_path, "op_politician_birth_locations.csv"), "r")
            dat = open(os.path.join(op_path, "op_politician_birth_dates.csv"), "r")

            first_names = cls.unify(f.readlines())
            last_names = cls.unify(l.readlines())
            birth_dates = cls.unify(dat.readlines())
            birth_locations = cls.unify(loc.readlines())

            first_name = random.choice(first_names)
            first_names.remove(first_name)
            first_name = first_name.strip()
            (names, sex) = first_name.split(',')
            if sex == 'M':
                sex = Person.MALE_SEX
            else:
                sex = Person.FEMALE_SEX
            names = names.split()
            first_name = random.choice(names)

            last_name = random.choice(last_names)
            last_names.remove(last_name)
            last_name = last_name.strip()

            birth_date = random.choice(birth_dates)
            birth_dates.remove(birth_date)
            birth_date = birth_date.strip()
            birth_date = datetime.datetime.strptime(birth_date, "%Y-%m-%d %H:%M:%S").strftime("%Y-%m-%d")

            birth_location = random.choice(birth_locations)
            birth_locations.remove(birth_location)
            birth_location = birth_location.strip()

            persons = Person.objects.filter(first_name=first_name, last_name=last_name, birth_date=birth_date, birth_location=birth_location)
            if not persons:
                p = Person(first_name=first_name, last_name=last_name, birth_date=birth_date, birth_location=birth_location, sex=sex)
                p.save()
            else:
                p = persons[0]

            return p


        except IOError as e:
            print "Error while opening file: %s" % e
            return 0
Beispiel #2
0
    def create_person(cls):
        """
        create a random person in the db and return the person just created
        if the person already exists, return it
        """

        try:
            op_path = os.path.join(settings.PROJECT_ROOT, "testdatabuilder", "openpolis_samples")
            f = open(os.path.join(op_path, "op_politician_first_names_sex.csv"), "r")
            l = open(os.path.join(op_path, "op_politician_last_names.csv"), "r")
            loc = open(os.path.join(op_path, "op_politician_birth_locations.csv"), "r")
            dat = open(os.path.join(op_path, "op_politician_birth_dates.csv"), "r")

            first_names = cls.unify(f.readlines())
            last_names = cls.unify(l.readlines())
            birth_dates = cls.unify(dat.readlines())
            birth_locations = cls.unify(loc.readlines())

            first_name = random.choice(first_names)
            first_names.remove(first_name)
            first_name = first_name.strip()
            (names, sex) = first_name.split(',')
            if sex == 'M':
                sex = Person.MALE_SEX
            else:
                sex = Person.FEMALE_SEX
            names = names.split()
            first_name = random.choice(names)

            last_name = random.choice(last_names)
            last_names.remove(last_name)
            last_name = last_name.strip()

            birth_date = random.choice(birth_dates)
            birth_dates.remove(birth_date)
            birth_date = birth_date.strip()
            birth_date = datetime.datetime.strptime(birth_date, "%Y-%m-%d %H:%M:%S").strftime("%Y-%m-%d")

            birth_location = random.choice(birth_locations)
            birth_locations.remove(birth_location)
            birth_location = birth_location.strip()

            persons = Person.objects.filter(first_name=first_name, last_name=last_name, birth_date=birth_date, birth_location=birth_location)
            if not persons:
                p = Person(first_name=first_name, last_name=last_name, birth_date=birth_date, birth_location=birth_location, sex=sex)
                p.save()
            else:
                p = persons[0]

            return p


        except IOError as e:
            print "Error while opening file: %s" % e
            return 0
Beispiel #3
0
    def handlePerson(self, xml_person):
        first_name = xml_person.get("first_name")
        last_name = xml_person.get("last_name")
        try:
            om_person = Person.objects.all().get(first_name=first_name,
                last_name=last_name)
            print("Person %s %s already imported" % (first_name,last_name))
            return om_person
        except Exception:
            # do nothing, continue to import it
            om_person = None

        om_person = Person()
        om_person.first_name = first_name
        om_person.last_name = last_name
        om_person.birth_date = BIRTH_DATE_DEF
        om_person.sex = SEX_DEF
        om_person.save()
        return om_person
Beispiel #4
0
        msg = _(
            "%(subject)s.\n\nDetails: %(error)s\n\nProvided data:\n\n%(data)s"
        ) % {
            "subject": subject,
            "error": smart_text(e),
            "data": smart_text(log_data)
        }

        d = {"subject": subject}

        logger.warning(msg, extra=d)
        # continue even in case of error

    # create the person

    person = Person()
    person.first_name = user.first_name
    person.last_name = user.last_name
    person.birth_date = form.cleaned_data["birth_date"]
    person.birth_location = form.cleaned_data.get("birth_location", None)
    person.sex = form.cleaned_data["sex"]
    person.img = request.FILES.get('image', None)

    try:
        person.save()

        # link the extra_data object to the person
        extra_data.person = person
        extra_data.image = person.img
        extra_data.save()
Beispiel #5
0
    try:
        extra_data.save()
    except Exception, e:
        subject =_(u"It was not possible to register user: %(user)s") % { "user":smart_text(user) }

        msg = _("%(subject)s.\n\nDetails: %(error)s\n\nProvided data:\n\n%(data)s") % { "subject": subject, "error": smart_text(e), "data": smart_text(log_data) }

        d = { "subject": subject }


        logger.warning(msg, extra=d)
        # continue even in case of error

    # create the person

    person = Person()
    person.first_name = user.first_name
    person.last_name = user.last_name
    person.birth_date = form.cleaned_data["birth_date"]
    person.birth_location = form.cleaned_data.get("birth_location", None)
    person.sex = form.cleaned_data["sex"]
    person.img = request.FILES.get('image', None)

    try:
        person.save()

        # link the extra_data object to the person
        extra_data.person = person
        extra_data.image = person.img
        extra_data.save()
Beispiel #6
0
        subject = _(u"It was not possible to register user: %(user)s") % {"user": smart_text(user)}

        msg = _("%(subject)s.\n\nDetails: %(error)s\n\nProvided data:\n\n%(data)s") % {
            "subject": subject,
            "error": smart_text(e),
            "data": smart_text(log_data),
        }

        d = {"subject": subject}

        logger.warning(msg, extra=d)
        # continue even in case of error

    # create the person

    person = Person()
    person.first_name = user.first_name
    person.last_name = user.last_name
    # person.birth_date = form.cleaned_data["birth_date"]
    # person.birth_location = form.cleaned_data.get("birth_location", None)
    # person.sex = form.cleaned_data["sex"]
    person.img = request.FILES.get("image", None)

    try:
        person.save()

        # link the extra_data object to the person
        extra_data.person = person
        extra_data.image = person.img
        extra_data.save()