#!/usr/bin/python2.5
#
# Copyright 2008 the Melange authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Views for Timeline.
"""

__authors__ = [
    '"Sverre Rabbelier" <sverre@rabbelier.nl>',
    '"Pawel Solyga" <pawel.solyga@gmail.com>',
  ]


from google.appengine.ext import db

from django import forms

from gsoc.models import timeline
from soc.logic import dicts
from soc.logic.models import program as program_logic
from soc.views.helper import params as params_helper
from soc.views.models import base

import soc.logic.models.timeline


class View(base.View):
  """View methods for the Timeline model.
  """

  def __init__(self, params=None):
    """Defines the fields and methods required for the base View class
    to provide the user with list, public, create, edit and delete views.

    Params:
      params: a dict with params for this View
    """

    new_params = {}
    new_params['logic'] = soc.logic.models.timeline.logic
    new_params['edit_template'] = 'soc/timeline/edit.html'
    new_params['name'] = "Timeline"

    patterns = [(r'^%(url_name)s/(?P<access_type>edit)/%(key_fields)s$',
                  'soc.views.models.%(module_name)s.edit', 
                  "Edit %(name_short)s")]

    new_params['django_patterns_defaults'] = patterns

    new_params['edit_dynafields'] = []

    timeline_properties = timeline.Timeline.properties()
    form_fields = {}
    
    # add class 'datetime-pick' for each DateTimeField
    # this is used with datetimepicker js widget
    for key, value in timeline_properties.iteritems():
      if isinstance(value, db.DateTimeProperty):
        form_fields[key] = forms.DateTimeField(required=False,
          widget=forms.TextInput(attrs={'class':'datetime-pick'}))
    
    new_params['create_extra_dynafields'] = form_fields

    params = dicts.merge(params, new_params)

    super(View, self).__init__(params=params)

    for name, value in program_logic.logic.TIMELINE_LOGIC.iteritems():
      create_form = params_helper.getCreateForm(self._params, value.getModel())
      edit_form = params_helper.getEditForm(self._params, create_form)
      self._params['edit_form_%s' % name] = edit_form

  def edit(self, request, access_type,
           page_name=None, params=None, seed=None, **kwargs):
    """See base.View.edit.
    """

    params = dicts.merge(params, self._params)

    program = program_logic.logic.getFromKeyName(kwargs['scope_path'])
    params['edit_form'] = params["edit_form_%s" % program.workflow]

    return super(View, self).edit(request, access_type, page_name=page_name,
                                  params=params, seed=seed, **kwargs)
  
  def _editPost(self, request, entity, fields):
    """See base.View._editPost().
    """
    
    # a timeline can only be edited, so set the scope path using entity
    fields['scope_path'] = entity.scope_path


view = View()

create = view.create
delete = view.delete
edit = view.edit
list = view.list
public = view.public
export = view.export


@@ -811,41 +811,8 @@
 ms%0A%0A
-from gsoc.models import timeline%0A
 from
@@ -889,16 +889,54 @@
 m_logic%0A
+from soc.views.helper import dynaform%0A
 from soc
@@ -1824,70 +1824,180 @@
     
-timeli
+params = dicts.merge(params, 
 ne
+w
 _p
-roperties = timeline.Timeline.propertie
+arams)%0A%0A    super(View, self).__init__(params=params)%0A%0A    for name, logic_value in program_logic.logic.TIMELINE_LOGIC.iteritem
 s()
+:
 %0A    
-form_
+  
 fiel
@@ -2004,25 +2004,29 @@
 ds = %7B%7D%0A    
-%0A
+  %0A  
     # add cl
@@ -2072,16 +2072,18 @@
 eld%0A    
+  
 # this i
@@ -2127,35 +2127,61 @@
     
+  
 for 
-key, value in timeline_
+prop_key, prop_value in logic_value.getModel().
 prop
@@ -2186,16 +2186,18 @@
 operties
+()
 .iterite
@@ -2198,24 +2198,26 @@
 teritems():%0A
+  
       if isi
@@ -2224,16 +2224,21 @@
 nstance(
+prop_
 value, d
@@ -2270,20 +2270,22 @@
     
-form_
+  
 fields%5B
+prop_
 key%5D
@@ -2323,16 +2323,18 @@
 =False,%0A
+  
         
@@ -2400,380 +2400,331 @@
     
+  
 %0A    
-new_params%5B'create_extra_dynafields'%5D = form_fields%0A%0A    params = dicts.m
+  create_form = params_help
 er
+.
 ge
-(params, new_params)%0A%0A    super(View, 
+tCreateForm(
 self
-)
 ._
-_init__(params=params)%0A%0A    for name, value in program_logic.logic.TIMELINE_LOGIC.iteritems():%0A      create_form = params_helper.getCreateForm(self._params, value.getModel())%0A      edit_form = params_helper.getEditForm(self._params, create_form)
+params, logic_value.getModel())%0A      edit_form = dynaform.extendDynaForm(%0A        dynaform = create_form,%0A        dynainclude = self._params%5B'edit_dynainclude'%5D,%0A        dynaexclude = self._params%5B'edit_dynaexclude'%5D,%0A        dynafields = fields,%0A        )%0A        
 %0A   
@@ -2907,25 +2907,24 @@
 it.%0A    %22%22%22%0A
-%0A
     params =
@@ -2954,24 +2954,174 @@
 lf._params)%0A
+    %0A    # TODO(pawel.solyga): If program doesn't exist for timeline display%0A    # customized error message without pointing to 'Create Timeline'%0A    
 %0A    program
@@ -3180,16 +3180,34 @@
 path'%5D)%0A
+    if program:%0A  
     para

Check if timeline exists and when somebody tries to edit non existing timelime display error message. Add TODO regarding error message customization for Timeline. Add support for calendar widget in timeline for both gsoc and ghop programs (gsoc was hardcoded before).