'set or create a title for a particular page and language'

def set_or_create(self, request, page, form, language):

    base_fields = ['slug', 'title', 'meta_description', 'page_title', 'menu_title']
    advanced_fields = ['redirect']
    cleaned_data = form.cleaned_data
    user = request.user
    try:
        obj = self.get(page=page, language=language)
    except self.model.DoesNotExist:
        data = {}
        for name in base_fields:
            if (name in cleaned_data):
                data[name] = cleaned_data[name]
        data['page'] = page
        data['language'] = language
        if page.has_advanced_settings_permission(user):
            overwrite_url = cleaned_data.get('overwrite_url', None)
            if overwrite_url:
                data['has_url_overwrite'] = True
                data['path'] = overwrite_url
            else:
                data['has_url_overwrite'] = False
            for field in advanced_fields:
                value = (cleaned_data.get(field) or None)
                data[field] = value
        return self.create(**data)
    for name in base_fields:
        if (name in form.base_fields):
            value = cleaned_data.get(name, None)
            setattr(obj, name, value)
    if page.has_advanced_settings_permission(user):
        if ('overwrite_url' in cleaned_data):
            overwrite_url = cleaned_data.get('overwrite_url', None)
            obj.has_url_overwrite = bool(overwrite_url)
            obj.path = overwrite_url
        for field in advanced_fields:
            if (field in form.base_fields):
                value = (cleaned_data.get(field) or None)
                setattr(obj, field, value)
    obj.save()
    return obj
