def __create_or_update_event_with_location (form, user, event):
    "Creates or updates an Event from the submitted EventForm. If the given Event is None a new Event is created."
    if event == None:
        event = Event()
    
    event.title=form.cleaned_data['title']
    event.set_date_time_begin_cet(form.cleaned_data['date_time_begin'])
    event.set_date_time_end_cet(form.cleaned_data['date_time_end'])
    event.url=form.cleaned_data['url']
    event.description=form.cleaned_data['description']
    event.location=form.cleaned_data['location']
    event.tags=form.cleaned_data['tags']
    
    if event.location == None:
        location = __create_location(form)
        event.location=location
    
    # Only when a new event is created
    if event.id == None:
        # auto-publish for staff users
        event.published = user.is_staff
        # link event to user
        if user.is_authenticated():
            event.user=user
    
    # Compute and store the archived flag
    event.update_archived_flag()
    
    event.save()
    
    return event
Beispiel #2
0
def import_from_json(input_file):
    input = open(input_file, 'rb')
    mixxt_events = json.load(input)
    
    print 'Importing %s Events from JSON' % len(mixxt_events)
    for mixxt_event in mixxt_events:
        l_name = mixxt_event['location']['name']
        l_street = mixxt_event['location']['street']
        if l_street is None:
            l_street = u'-'
        l_city = mixxt_event['location']['city']
        if l_city is None:
            l_city = u'-'
        location = None
        if l_name:
            print 'Creating Location %s' % l_name
            location, created = Location.objects.get_or_create(name=l_name, defaults={'street':l_street, 'city':l_city})
            if not created:
                print ' ... already exists'

        print 'Creating Event %s' % mixxt_event['title']
        event = Event()
        event.location = location
        event.title = mixxt_event['title']
        event.url = mixxt_event['url']
        if event.url is None:
            event.url = "http://www.example.com"
        event.description = mixxt_event['description']
        event.set_date_time_begin_cet(_parse_datetime(mixxt_event['begin']))
        event.set_date_time_end_cet(_parse_datetime(mixxt_event['end']))
        event.published = True
        event.update_archived_flag()
        event.save()
    
    print 'Done'
    input.close()