# -*- coding: utf-8 -*-

from django import forms

from message.models import Message, MessageType
from core.models import Region, Category
from core.widgets import CategoryTree


class SimpleRequestForm(forms.ModelForm):
    '''Simple request form'''
    class Meta:
        model = Message

    def __init__(self, *args, **kwargs):
        super(SimpleRequestForm, self).__init__(*args, **kwargs)
        self.fields['messageType'].initial = 1


class RequestForm(forms.ModelForm):
    class Meta:
        model = Message
        exclude = ('messageType', 'flags', 'status', 'date_add', 'last_edit',
            'expired_date','location', 'sender', 'subdomain', 'edit_key',
            'notes', 'user', '')
        widgets = {'category': CategoryTree(tree=Category.objects.
            filter(subdomain=None).values('id', 'name', 'parentId')),
            'message': forms.Textarea(attrs={'rows': 5, 'cols': 5})
        }

    lat = forms.FloatField(widget=forms.HiddenInput)
    lon = forms.FloatField(widget=forms.HiddenInput)
    region = forms.ModelChoiceField(Region.objects.all(), label='Регион')
    #category = forms.ModelChoiceField(Category.objects.filter(subdomain=None)
    #    .exclude(parentId=None),widget=CategoryWidget())
#    messageType = forms.ModelChoiceField(MessageType.objects.filter(id__lt=5), label='Тип сообщения')
    #msgType = forms.ChoiceField(choices = Message.MESSAGE_TYPE, label = 'Тип сообщения')
    address = forms.CharField(label='Адрес')
#    active = forms.BooleanField(label='Сообщение активно',required=False) #Флаг может быть в состоянии on/off
#    important = forms.BooleanField(label='Сообщение важно', required=False)#Флаг может быть в состоянии on/off

#    def __init__(self, *args, **kwargs):
#        msg = kwargs.get('instance',None)
#        if msg:
#            initial = {
#                'address': msg.address(),
#                'lat': msg.latitude(),
#                'lon': msg.longtitude(),
#                'active':msg.active(),
#                'important': msg.important(),
#                'region': msg.region(),
#                }
#            kwargs['initial'] = initial
#        super(MessageForm, self).__init__(*args, **kwargs)

#    def save(self, *args, **kwargs):
#        msg = self.instance
#        ce = self.cleaned_data
#        msg.latitude(ce['lat'])
#        msg.longtitude(ce['lon'])
#        msg.address(ce['address'])
#        msg.region( ce['region'])
#        msg.set_flag(Message.ACTIVE, ce['active'])
#        msg.set_flag(Message.IMPORTANT, ce['important'])
#        super(MessageForm, self).save(*args, **kwargs)


@@ -440,17 +440,387 @@
 al = 1%0A%0A
-%0A
+    address = forms.CharField()%0A%0A    def clean_status(self):%0A        status = self.cleaned_data%5B'status'%5D%0A        if status is None:%0A            return self.fields%5B'status'%5D.initial%0A        return status%0A%0A    def clean_flags(self):%0A        flags = self.cleaned_data%5B'flags'%5D%0A        if flags is None:%0A            return self.fields%5B'flags'%5D.initial%0A        return flags%0A#
 class Re
@@ -843,24 +843,25 @@
 ModelForm):%0A
+#
     class Me
@@ -856,32 +856,33 @@
     class Meta:%0A
+#
         model = 
@@ -889,16 +889,17 @@
 Message%0A
+#
         
@@ -968,16 +968,17 @@
 _edit',%0A
+#
         
@@ -1043,16 +1043,17 @@
 t_key',%0A
+#
         
@@ -1077,16 +1077,17 @@
 r', '')%0A
+#
         
@@ -1145,16 +1145,17 @@
 bjects.%0A
+#
         
@@ -1216,16 +1216,17 @@
 tId')),%0A
+#
         
@@ -1285,16 +1285,17 @@
 s': 5%7D)%0A
+#
         
@@ -1296,17 +1296,19 @@
       %7D%0A
-%0A
+#%0A#
     lat 
@@ -1348,24 +1348,25 @@
 iddenInput)%0A
+#
     lon = fo
@@ -1406,16 +1406,17 @@
 nInput)%0A
+#
     regi
@@ -1807,24 +1807,25 @@
 %D1%81%D0%BE%D0%BE%D0%B1%D1%89%D0%B5%D0%BD%D0%B8%D1%8F')%0A
+#
     address 

Add default form values for message