# Path: web/processors/event.py
# def create_or_update_event(event_id=None, **event_data):
#     """
#     Creates or updates Event object
#     """
#     event = Event.objects.filter(id=event_id)
#     if event:
#         event = event[0]
# 
#         if event_data:
#             # many to many fields have to updated after other fields are
#             # updated
#             new_audiences = event_data['audience']
#             event_data.pop('audience')
#             new_themes = event_data['theme']
#             event_data.pop('theme')
# 
#             event_tags = []
#             if 'tags' in event_data:
#                 event_tags = event_data['tags']
#                 event_data.pop('tags')
# 
#             # in case we have geoposition data in event_data
#             if 'geoposition' in event_data and event_data['geoposition'] != '':
#                 # updating geoposition field is a bit fuzzy
#                 event_latitude = event_data['geoposition'][0]
#                 event_longitude = event_data['geoposition'][1]
#                 event_data.pop('geoposition')
#                 # updating all other fields
#                 event.__dict__.update(event_data)
#                 # setting new values for geoposition
#                 event.__dict__['geoposition'].latitude = event_latitude
#                 event.__dict__['geoposition'].longitude = event_longitude
#                 event.save()
#             else:
#                 event.__dict__.update(event_data)
#                 event.save()
# 
#             if 'picture' not in event_data:
#                 event.picture = ''
#                 event.save()
# 
#             # delete old categories and tags and store new ones
#             event.audience.clear()
#             event.audience.add(*new_audiences)
#             event.theme.clear()
#             event.theme.add(*new_themes)
#             event.tags.set(*event_tags)
# 
#     else:
#         event = Event.objects.create(**event_data)
#         send_email_to_country_ambassadors(event)
# 
#     return event


import json
import datetime
from geoposition import Geoposition
from web.processors.event import create_or_update_event
            "geoposition": Geoposition(
                46.05528,
                14.51444),
            "location": "Ljubljana",
            "audience": [1],
            "theme": [1],
            "tags": [
                "tag1",
                "tag2"],
        }

        event = create_or_update_event(**event_data)
        event.status = 'APPROVED'
        event.save()

        event_data = {
            "start_date": datetime.datetime.now() - datetime.timedelta(days=1, hours=3),
            "end_date": datetime.datetime.now() + datetime.timedelta(days=3, hours=3),
            "organizer": "other organizer",
            "creator": admin_user,
            "title": "Event in IS",
            "pub_date": datetime.datetime.now(),
            "country": "IS",
            "geoposition": Geoposition(64.13244, -21.85690),
            "location": "Reykjavík",
            "audience": [1],
            "theme": [1],
            "tags": ["tag1", "tag2"],
        }


# Path: web/processors/event.py
# def create_or_update_event(event_id=None, **event_data):
#     """
#     Creates or updates Event object
#     """
#     event = Event.objects.filter(id=event_id)
#     if event:
#         event = event[0]
# 
#         if event_data:
#             # many to many fields have to updated after other fields are
#             # updated
#             new_audiences = event_data['audience']
#             event_data.pop('audience')
#             new_themes = event_data['theme']
#             event_data.pop('theme')
# 
#             event_tags = []
#             if 'tags' in event_data:
#                 event_tags = event_data['tags']
#                 event_data.pop('tags')
# 
#             # in case we have geoposition data in event_data
#             if 'geoposition' in event_data and event_data['geoposition'] != '':
#                 # updating geoposition field is a bit fuzzy
#                 event_latitude = event_data['geoposition'][0]
#                 event_longitude = event_data['geoposition'][1]
#                 event_data.pop('geoposition')
#                 # updating all other fields
#                 event.__dict__.update(event_data)
#                 # setting new values for geoposition
#                 event.__dict__['geoposition'].latitude = event_latitude
#                 event.__dict__['geoposition'].longitude = event_longitude
#                 event.save()
#             else:
#                 event.__dict__.update(event_data)
#                 event.save()
# 
#             if 'picture' not in event_data:
#                 event.picture = ''
#                 event.save()
# 
#             # delete old categories and tags and store new ones
#             event.audience.clear()
#             event.audience.add(*new_audiences)
#             event.theme.clear()
#             event.theme.add(*new_themes)
#             event.tags.set(*event_tags)
# 
#     else:
#         event = Event.objects.create(**event_data)
#         send_email_to_country_ambassadors(event)
# 
#     return event


# Path: api/tests/test_restapi.py
import json
import datetime
from geoposition import Geoposition
from web.processors.event import create_or_update_event

            "geoposition": Geoposition(
                46.05528,
                14.51444),
            "location": "Ljubljana",
            "audience": [1],
            "theme": [1],
            "tags": [
                "tag1",
                "tag2"],
        }

        event = create_or_update_event(**event_data)
        event.status = 'APPROVED'
        event.save()

        event_data = {
            "start_date": datetime.datetime.now() - datetime.timedelta(days=1, hours=3),
            "end_date": datetime.datetime.now() + datetime.timedelta(days=3, hours=3),
            "organizer": "other organizer",
            "creator": admin_user,
            "title": "Event in IS",
            "pub_date": datetime.datetime.now(),
            "country": "IS",
            "geoposition": Geoposition(64.13244, -21.85690),
            "location": "Reykjavík",
            "audience": [1],
            "theme": [1],
            "tags": ["tag1", "tag2"],
        }


        event = create_or_update_event(**event_data)