###########################################################################
#          (C) Vrije Universiteit, Amsterdam (the Netherlands)            #
#                                                                         #
# This file is part of AmCAT - The Amsterdam Content Analysis Toolkit     #
#                                                                         #
# AmCAT is free software: you can redistribute it and/or modify it under  #
# the terms of the GNU Affero General Public License as published by the  #
# Free Software Foundation, either version 3 of the License, or (at your  #
# option) any later version.                                              #
#                                                                         #
# AmCAT is distributed in the hope that it will be useful, but WITHOUT    #
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or   #
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public     #
# License for more details.                                               #
#                                                                         #
# You should have received a copy of the GNU Affero General Public        #
# License along with AmCAT.  If not, see <http://www.gnu.org/licenses/>.  #
###########################################################################

from django import forms
from webscript import WebScript
import amcat.scripts.forms
from amcat.tools.table import table3

class ExportAssociation(WebScript):
    name = "Export Association"
    form_template = None
    displayLocation = ('ShowAssociations')
    output_template = None
    is_download = True
    
    class form(amcat.scripts.forms.TableOutputForm):
        export_format = forms.ChoiceField(label="Output Format", choices = (('table', 'Table'), ('list', 'List')), initial=0)
            
    def run(self):
        from api.webscripts.associations import ShowAssociations
        table = ShowAssociations(data=self.data).get_table()
        if self.data['export_format'] == 'table':
            table = self.get_table(table)

        return self.outputResponse(table, table3.Table, filename='Export Association')
            

    def get_table(self, assocTable):
        intervals = sorted({i for (i,q,q2,p) in assocTable})
        cols = {}
        assocs = {(x,y) for (i,x,y,s) in assocTable}
        cols = {u"{x}\u2192{y}".format(x=x, y=y) : (x,y) for (x,y) in assocs}

        colnames = sorted(cols)
        
        result = table3.ListTable(colnames=['Interval'] + list(colnames))
        scores = {(i,x,y) : s for (i,x,y,s) in assocTable}
        for i in intervals:
            row = [i] + [scores.get((i, ) + cols[c], 0) for c in colnames]
            result.addRow(*row)
        return result


@@ -1482,16 +1482,46 @@
 t table3
+%0Afrom functools import partial
 %0A%0Aclass 
@@ -1713,19 +1713,15 @@
     
-class 
 form
-(
+ = 
 amca
@@ -1755,145 +1755,9 @@
 Form
-):%0A        export_format = forms.ChoiceField(label=%22Output Format%22, choices = (('table', 'Table'), ('list', 'List')), initial=0)%0A        
+%0A
     
@@ -1906,62 +1906,8 @@
 e()%0A
-        if self.data%5B'export_format'%5D == 'table':%0A    
     
@@ -2076,19 +2076,85 @@
 socTable
+, rowheader_label=%22interval%22, rowheader_type=str, cell_type=float
 ):%0A
+%0A
         
@@ -2210,26 +2210,8 @@
 e%7D)%0A
-        cols = %7B%7D%0A
     
@@ -2336,16 +2336,75 @@
  assocs%7D
+%0A        scores = %7B(i,x,y) : s for (i,x,y,s) in assocTable%7D
 %0A%0A      
@@ -2466,219 +2466,462 @@
 le3.
-Lis
+Objec
 tTable(
-colnames=%5B'Interval'%5D + list(colnames))%0A        scores = %7B(i,x,y) : s for (i,
+rows=intervals)%0A        result.addColumn(table3.ObjectColumn(label=rowheader_label, cellfunc = lambda row:row,%0A                                             fieldtype = rowheader_type))%0A        %0A        for col, (
 x,y
-,s
 ) in 
-assocTable%7D%0A        for i in intervals:%0A            row = %5Bi%5D + %5Bscores.get((i, ) + cols%5Bc%5D, 0) for c in colnames%5D
+sorted(cols.iteritems()):%0A            result.addColumn(table3.ObjectColumn(label=unicode(col), fieldtype=cell_type,%0A                                                 cellfunc = partial(getscore, scores, x, y)))%0A            
 %0A   
@@ -2929,51 +2929,83 @@
     
-   
+return
  result
-.addRow(*row)%0A        return result%0A
+%0A%0Adef getscore(scores, x, y, i):%0A    return scores.get((i, x, y))
 %0A

fix spss export of associations