Example #1
0
def do_event_import(event_object_dict):
    """Creates and Event and Place for the given event_object_dict
    """
    event = Event()
    place = Place()

    # assure the correct fields get the right value types
    color_set = TypeColorSet.objects.all()[0]  # default color set
    for field in EVENT_FIELDS:
        if field in event_object_dict:
            if field == "type":
                try:
                    event_type = Type.objects.get(name=event_object_dict[field])
                except Type.DoesNotExist:
                    event_type = Type(
                                    name=event_object_dict[field],
                                    slug=slugify(event_object_dict[field]),
                                    color_set=color_set
                                    )
            else:
                field_type = Event._meta.get_field_by_name(field)[0]
                if isinstance(field_type, models.DateTimeField):
                    setattr(event, field, datetime.strptime(event_object_dict[field], VALID_DATE_FORMAT))
                elif isinstance(field_type, models.BooleanField):
                    if event_object_dict[field].lower() == "false" or event_object_dict[field] == "0":
                        setattr(event, field, False)
                    else:
                        setattr(event, field, True)
                else:  # assume its a string
                    if field_type.max_length:
                        setattr(event, field, unicode(event_object_dict[field])[:field_type.max_length])
                    else:
                        setattr(event, field, unicode(event_object_dict[field]))

    for field in PLACE_FIELDS:
        if field in event_object_dict:
            p_field = field.replace('place__', '')
            field_type = Place._meta.get_field_by_name(p_field)[0]
            if isinstance(field_type, models.DateTimeField):
                setattr(place, p_field, datetime.strptime(event_object_dict[field], VALID_DATE_FORMAT))
            elif isinstance(field_type, models.BooleanField):
                setattr(place, p_field, bool(ast.literal_eval(event_object_dict[field])))
            else:  # assume its a string
                if field_type.max_length:
                    setattr(place, p_field, unicode(event_object_dict[field])[:field_type.max_length])
                else:
                    setattr(place, p_field, unicode(event_object_dict[field]))

    event_type.save()
    place.save()

    event.type = event_type
    event.place = place
    event.save()

    return event
Example #2
0
def do_event_import(event_object_dict):
    """Creates and Event and Place for the given event_object_dict
    """
    event = Event()
    place = Place()

    # assure the correct fields get the right value types
    color_set = TypeColorSet.objects.all()[0]  # default color set
    for field in EVENT_FIELDS:
        if field in event_object_dict:
            if field == "type":
                try:
                    event_type = Type.objects.get(name=event_object_dict[field])
                except Type.DoesNotExist:
                    event_type = Type(
                                    name=event_object_dict[field],
                                    slug=slugify(event_object_dict[field]),
                                    color_set=color_set
                                    )
            else:
                field_type = Event._meta.get_field_by_name(field)[0]
                if isinstance(field_type, models.DateTimeField):
                    setattr(event, field, datetime.strptime(event_object_dict[field], VALID_DATE_FORMAT))
                elif isinstance(field_type, models.BooleanField):
                    if event_object_dict[field].lower() == "false" or event_object_dict[field] == "0":
                        setattr(event, field, False)
                    else:
                        setattr(event, field, True)
                else:  # assume its a string
                    if field_type.max_length:
                        setattr(event, field, unicode(event_object_dict[field])[:field_type.max_length])
                    else:
                        setattr(event, field, unicode(event_object_dict[field]))

    for field in PLACE_FIELDS:
        if field in event_object_dict:
            p_field = field.replace('place__', '')
            field_type = Place._meta.get_field_by_name(p_field)[0]
            if isinstance(field_type, models.DateTimeField):
                setattr(place, p_field, datetime.strptime(event_object_dict[field], VALID_DATE_FORMAT))
            elif isinstance(field_type, models.BooleanField):
                setattr(place, p_field, bool(ast.literal_eval(event_object_dict[field])))
            else:  # assume its a string
                if field_type.max_length:
                    setattr(place, p_field, unicode(event_object_dict[field])[:field_type.max_length])
                else:
                    setattr(place, p_field, unicode(event_object_dict[field]))

    event_type.save()
    place.save()

    event.type = event_type
    event.place = place
    event.save()

    return event