<s> import os <EOL> import sys <EOL> import json <EOL> import time <EOL> from glob import glob <EOL> from datetime import date , datetime <EOL> from nagare import database <EOL> from kansha . user . models import DataUser <EOL> from kansha . card . models import DataCard <EOL> from kansha . board . models import DataBoard <EOL> from kansha . column . models import DataColumn <EOL> from kansha . card_addons . label import DataLabel <EOL> from kansha . card_addons . comment import DataComment <EOL> DEFAULT_LABELS = ( <EOL> ( u'<STR_LIT>' , u'<STR_LIT>' ) , <EOL> ( u'<STR_LIT>' , u'<STR_LIT>' ) , <EOL> ( u'<STR_LIT>' , u'<STR_LIT>' ) , <EOL> ( u'<STR_LIT>' , u'<STR_LIT>' ) , <EOL> ( u'<STR_LIT>' , u'<STR_LIT>' ) , <EOL> ( u'<STR_LIT>' , u'<STR_LIT>' ) <EOL> ) <EOL> def create_boards_from_templates ( user , folder ) : <EOL> for template_filename in glob ( os . path . join ( folder , '<STR_LIT>' ) ) : <EOL> template = json . loads ( open ( template_filename ) . read ( ) ) <EOL> board = DataBoard ( title = template [ '<STR_LIT:title>' ] ) <EOL> labels_def = template . get ( '<STR_LIT>' , DEFAULT_LABELS ) <EOL> for i , ( title , color ) in enumerate ( labels_def ) : <EOL> __ = DataLabel ( title = title , <EOL> color = color , <EOL> index = i , <EOL> board = board ) <EOL> database . session . flush ( ) <EOL> for i , col in enumerate ( template . get ( '<STR_LIT>' , [ ] ) ) : <EOL> cards = col . pop ( '<STR_LIT>' , [ ] ) <EOL> col = DataColumn ( title = col [ '<STR_LIT:title>' ] , <EOL> index = i , <EOL> board = board ) <EOL> for j , card in enumerate ( cards ) : <EOL> comments = card . pop ( '<STR_LIT>' , [ ] ) <EOL> labels = card . pop ( '<STR_LIT>' , [ ] ) <EOL> due_date = card . pop ( '<STR_LIT>' , None ) <EOL> if due_date : <EOL> due_date = time . strptime ( due_date , '<STR_LIT>' ) <EOL> due_date = date ( * due_date [ : <NUM_LIT:3> ] ) <EOL> card = DataCard ( title = card [ '<STR_LIT:title>' ] , <EOL> description = card . get ( '<STR_LIT:description>' , u'<STR_LIT>' ) , <EOL> due_date = due_date , <EOL> creation_date = datetime . utcnow ( ) , <EOL> column = col , <EOL> index = j , <EOL> labels = [ board . labels [ i ] for i in labels ] ) <EOL> for comment in comments : <EOL> DataComment ( comment = comment , <EOL> author = user , <EOL> creation_date = datetime . utcnow ( ) , <EOL> card = card ) <EOL> board . members . append ( user ) <EOL> board . managers . append ( user ) <EOL> database . session . flush ( ) <EOL> if len ( sys . argv ) != <NUM_LIT:3> : <EOL> print '<STR_LIT>' % sys . argv [ <NUM_LIT:0> ] <EOL> user = DataUser . get_by_email ( sys . argv [ <NUM_LIT:1> ] ) <EOL> if not user : <EOL> print '<STR_LIT>' <EOL> sys . exit ( <NUM_LIT:0> ) <EOL> folder = sys . argv [ <NUM_LIT:2> ] <EOL> create_boards_from_templates ( user , folder ) <EOL> database . session . commit ( ) </s>
