# Path: festi/survey/forms.py
# class SurveyForm(forms.ModelForm):
#     class Meta:
#         model = Survey
#         fields = ('props',)
#         widgets = {
#             'props': SplitJSONWidget,
#         }
# 
#     def clean_props(self):
#         props = self.cleaned_data['props']
#         for prop in props:
#             if not prop.get('answer', '').strip():
#                 raise forms.ValidationError(u'모든 답변을 채워주세요.')
#         return props
# 
# Path: festi/survey/models.py
# class Event(Base):
#     slug = models.SlugField(unique=True)
#     name = models.CharField(max_length=100, db_index=True)
#     props = JSONField(verbose_name='Questions', blank=True)
#     begin = models.DateTimeField()
#     end = models.DateTimeField()
#     is_public = models.BooleanField(default=False, db_index=True)
#     is_closed = models.BooleanField(default=True, db_index=True)
#     approve_email_content = models.TextField(blank=True, null=True, help_text=u'첫 줄은 제목')
# 
#     def __unicode__(self):
#         return self.name
# 
#     def as_dict(self):
#         return {
#             'id': self.id,
#             'name': self.name,
#             'props': self.props,
#             'begin': self.begin,
#             'end': self.end,
#         }
# 
#     def export_csv(self, f):
#         header = ['email', 'name', 'gender', 'is_approved', 'is_notified', 'is_attended']
# 
# 
#         f.write(BOM_UTF8)
#         writer = csv.writer(f)
#         writer.writerow([(col or '').encode('utf8') for col in header])
# 
#         for survey in self.survey_set.select_related('user').all():
#             try:
#                 social = survey.user.socialaccount_set.first()
#                 name = social.extra_data['name']
#                 gender = social.extra_data['gender']
#             except (AttributeError, KeyError):
#                 name = ''
#                 gender = ''
# 
#             row = [survey.user.email, name, gender, survey.is_approved, survey.is_notified, survey.is_attended]
#             row.extend([prop['answer'] for prop in survey.props])
#             writer.writerow([(col or '').encode('utf8') for col in row])
# 
#         return f
# 
# class Speaker(models.Model):
#     event = models.ForeignKey(Event)
#     idx = models.IntegerField(default=0)
#     name = models.CharField(max_length=100)
#     profile = models.CharField(max_length=100, blank=True)
#     profile_image = models.ImageField(upload_to=random_name)
#     keyword = models.CharField(max_length=20, blank=True)
#     topic = models.CharField(max_length=100, blank=True)
#     detail = models.TextField(blank=True)
# 
#     class Meta:
#         ordering = ('idx', 'id')
# 
# class Survey(Base):
#     event = models.ForeignKey(Event)
#     user = models.ForeignKey(settings.AUTH_USER_MODEL)
#     props = JSONField()
#     is_approved = models.BooleanField(default=False, db_index=True, verbose_name=u'승인여부')
#     is_notified = models.BooleanField(default=False, db_index=True, verbose_name=u'알림여부')
#     is_attended = models.BooleanField(default=False, db_index=True, verbose_name=u'참석여부')
# 
#     class Meta:
#         unique_together = (('event', 'user'),)
# 
#     def as_dict(self):
#         return {
#             'props': self.props,
#             'is_approved': self.is_approved,
#             'is_notified': self.is_notified,
#             'is_attended': self.is_attended,
#         }


from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django.http import HttpResponse
from django.shortcuts import get_object_or_404, redirect, render
from .forms import SurveyForm
from .models import Event, Speaker, Survey
# coding: utf8


def facebook_user_required(fn):
    def wrap(request, *args, **kwargs):
        facebook_account = request.user.socialaccount_set.filter(provider='facebook').first()
        if not facebook_account:
            messages.error(request, u'페이스북 유저가 아닙니다.')
            return redirect('account_logout')
        request.facebook_account = facebook_account

        return fn(request, *args, **kwargs)
    return login_required(wrap)


def index(request):
    return render(request, 'survey/index.html', {
    })


def detail(request, event_slug):
    event = get_object_or_404(Event, slug=event_slug)

    survey = None
    if request.user.is_authenticated():
        try:
            survey = Survey.objects.get(event=event, user=request.user)

# Path: festi/survey/forms.py
# class SurveyForm(forms.ModelForm):
#     class Meta:
#         model = Survey
#         fields = ('props',)
#         widgets = {
#             'props': SplitJSONWidget,
#         }
# 
#     def clean_props(self):
#         props = self.cleaned_data['props']
#         for prop in props:
#             if not prop.get('answer', '').strip():
#                 raise forms.ValidationError(u'모든 답변을 채워주세요.')
#         return props
# 
# Path: festi/survey/models.py
# class Event(Base):
#     slug = models.SlugField(unique=True)
#     name = models.CharField(max_length=100, db_index=True)
#     props = JSONField(verbose_name='Questions', blank=True)
#     begin = models.DateTimeField()
#     end = models.DateTimeField()
#     is_public = models.BooleanField(default=False, db_index=True)
#     is_closed = models.BooleanField(default=True, db_index=True)
#     approve_email_content = models.TextField(blank=True, null=True, help_text=u'첫 줄은 제목')
# 
#     def __unicode__(self):
#         return self.name
# 
#     def as_dict(self):
#         return {
#             'id': self.id,
#             'name': self.name,
#             'props': self.props,
#             'begin': self.begin,
#             'end': self.end,
#         }
# 
#     def export_csv(self, f):
#         header = ['email', 'name', 'gender', 'is_approved', 'is_notified', 'is_attended']
# 
# 
#         f.write(BOM_UTF8)
#         writer = csv.writer(f)
#         writer.writerow([(col or '').encode('utf8') for col in header])
# 
#         for survey in self.survey_set.select_related('user').all():
#             try:
#                 social = survey.user.socialaccount_set.first()
#                 name = social.extra_data['name']
#                 gender = social.extra_data['gender']
#             except (AttributeError, KeyError):
#                 name = ''
#                 gender = ''
# 
#             row = [survey.user.email, name, gender, survey.is_approved, survey.is_notified, survey.is_attended]
#             row.extend([prop['answer'] for prop in survey.props])
#             writer.writerow([(col or '').encode('utf8') for col in row])
# 
#         return f
# 
# class Speaker(models.Model):
#     event = models.ForeignKey(Event)
#     idx = models.IntegerField(default=0)
#     name = models.CharField(max_length=100)
#     profile = models.CharField(max_length=100, blank=True)
#     profile_image = models.ImageField(upload_to=random_name)
#     keyword = models.CharField(max_length=20, blank=True)
#     topic = models.CharField(max_length=100, blank=True)
#     detail = models.TextField(blank=True)
# 
#     class Meta:
#         ordering = ('idx', 'id')
# 
# class Survey(Base):
#     event = models.ForeignKey(Event)
#     user = models.ForeignKey(settings.AUTH_USER_MODEL)
#     props = JSONField()
#     is_approved = models.BooleanField(default=False, db_index=True, verbose_name=u'승인여부')
#     is_notified = models.BooleanField(default=False, db_index=True, verbose_name=u'알림여부')
#     is_attended = models.BooleanField(default=False, db_index=True, verbose_name=u'참석여부')
# 
#     class Meta:
#         unique_together = (('event', 'user'),)
# 
#     def as_dict(self):
#         return {
#             'props': self.props,
#             'is_approved': self.is_approved,
#             'is_notified': self.is_notified,
#             'is_attended': self.is_attended,
#         }


# Path: festi/survey/views.py
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django.http import HttpResponse
from django.shortcuts import get_object_or_404, redirect, render
from .forms import SurveyForm
from .models import Event, Speaker, Survey

# coding: utf8


def facebook_user_required(fn):
    def wrap(request, *args, **kwargs):
        facebook_account = request.user.socialaccount_set.filter(provider='facebook').first()
        if not facebook_account:
            messages.error(request, u'페이스북 유저가 아닙니다.')
            return redirect('account_logout')
        request.facebook_account = facebook_account

        return fn(request, *args, **kwargs)
    return login_required(wrap)


def index(request):
    return render(request, 'survey/index.html', {
    })


def detail(request, event_slug):
    event = get_object_or_404(Event, slug=event_slug)

    survey = None
    if request.user.is_authenticated():
        try:
            survey = Survey.objects.get(event=event, user=request.user)

        except Survey.DoesNotExist: