# Path: festi/survey/models.py
# def random_name(instance, filename):
#     def __unicode__(self):
#     def as_dict(self):
#     def export_csv(self, f):
#     def as_dict(self):
# class Base(models.Model):
#     class Meta:
# class Event(Base):
# class Speaker(models.Model):
#     class Meta:
# class Survey(Base):
#     class Meta:
# 
# Path: festi/survey/forms_api.py
# class UserCreateForm(forms.ModelForm):
#     class Meta:
#         model = User
#         fields = ('uid', 'name', 'profile_image_url', 'email')
# 
# class UserUpdateForm(forms.ModelForm):
#     class Meta:
#         model = User
#         fields = ('name', 'profile_image_url', 'email')
# 
# class SurveyCreateForm(forms.ModelForm):
#     def __init__(self, data=None, *args, **kwargs):
#         user_id = data.get('user')
#         if user_id:
#             try:
#                 found_user = User.objects.get(uid=user_id)
#                 data = data.copy()
#                 data.update({'user': found_user.id})
#             except User.DoesNotExist:
#                 pass
# 
#         super(SurveyCreateForm, self).__init__(data, *args, **kwargs)
# 
#     class Meta:
#         model = Survey
#         fields = ('event', 'user', 'props')
# 
# class SurveyUpdateForm(forms.ModelForm):
#     class Meta:
#         model = Survey
#         fields = ('props',)


from django.conf.urls import patterns, include, url
from django.shortcuts import render, get_object_or_404
from django.views.decorators.csrf import csrf_exempt
from django.views.decorators.http import require_http_methods
from .models import User, Event, Survey
from .forms_api import UserCreateForm, UserUpdateForm, SurveyCreateForm, SurveyUpdateForm
@require_http_methods(['POST'])
@csrf_exempt
def user_new(request):
    u'새 유저 등록'
    form = UserCreateForm(request.POST)
    if form.is_valid():
        user = form.save()
        return dict(ok=True)
    return dict(ok=False, errors=form.errors)


@require_http_methods(['PUT'])
@csrf_exempt
def user_update(request, user_uid):
    u'유저 정보 수정'
    user = get_object_or_404(User, uid=user_uid)
    form = UserUpdateForm(request.PUT, instance=user)
    if form.is_valid():
        user = form.save()
        return dict(ok=True)
    return dict(ok=False, errors=form.errors)


def events_list(request):
    u'이벤트 목록 조회'
    return Event.objects.filter(is_public=True)


def event_detail(request, event_id):
    u'이벤트 내용 조회'

# Path: festi/survey/models.py
# def random_name(instance, filename):
#     def __unicode__(self):
#     def as_dict(self):
#     def export_csv(self, f):
#     def as_dict(self):
# class Base(models.Model):
#     class Meta:
# class Event(Base):
# class Speaker(models.Model):
#     class Meta:
# class Survey(Base):
#     class Meta:
# 
# Path: festi/survey/forms_api.py
# class UserCreateForm(forms.ModelForm):
#     class Meta:
#         model = User
#         fields = ('uid', 'name', 'profile_image_url', 'email')
# 
# class UserUpdateForm(forms.ModelForm):
#     class Meta:
#         model = User
#         fields = ('name', 'profile_image_url', 'email')
# 
# class SurveyCreateForm(forms.ModelForm):
#     def __init__(self, data=None, *args, **kwargs):
#         user_id = data.get('user')
#         if user_id:
#             try:
#                 found_user = User.objects.get(uid=user_id)
#                 data = data.copy()
#                 data.update({'user': found_user.id})
#             except User.DoesNotExist:
#                 pass
# 
#         super(SurveyCreateForm, self).__init__(data, *args, **kwargs)
# 
#     class Meta:
#         model = Survey
#         fields = ('event', 'user', 'props')
# 
# class SurveyUpdateForm(forms.ModelForm):
#     class Meta:
#         model = Survey
#         fields = ('props',)


# Path: festi/survey/api_v1.py
from django.conf.urls import patterns, include, url
from django.shortcuts import render, get_object_or_404
from django.views.decorators.csrf import csrf_exempt
from django.views.decorators.http import require_http_methods
from .models import User, Event, Survey
from .forms_api import UserCreateForm, UserUpdateForm, SurveyCreateForm, SurveyUpdateForm

@require_http_methods(['POST'])
@csrf_exempt
def user_new(request):
    u'새 유저 등록'
    form = UserCreateForm(request.POST)
    if form.is_valid():
        user = form.save()
        return dict(ok=True)
    return dict(ok=False, errors=form.errors)


@require_http_methods(['PUT'])
@csrf_exempt
def user_update(request, user_uid):
    u'유저 정보 수정'
    user = get_object_or_404(User, uid=user_uid)
    form = UserUpdateForm(request.PUT, instance=user)
    if form.is_valid():
        user = form.save()
        return dict(ok=True)
    return dict(ok=False, errors=form.errors)


def events_list(request):
    u'이벤트 목록 조회'
    return Event.objects.filter(is_public=True)


def event_detail(request, event_id):
    u'이벤트 내용 조회'

    return get_object_or_404(Event, id=event_id)