In [21]:
from WebAPI.app import db, Vote, Talk, base_path, group_by_time, Address
import collections
from glob import glob
from os import path
import yaml

In [22]:
# 投票結果
db.session.expire_all()
all = Vote.query.all()
counter = collections.Counter(map(lambda v: v.nid, all))

rank = counter.most_common(4)
rank = rank[1:]

for i, r in enumerate(rank):
    print("#" + str(i + 1) + ": ", Talk.query.get(r[0]), str(r[1]) + "票")


#1:  <Talk '110','RubyでXcodeプロジェクトを解析してみよう'> 4票
#2:  <Talk '84','ユーザーに受け入れられ、問題を起こしづらい大規模リニューアルの進め方'> 3票
#3:  <Talk '72','Swift3 Web Framework SlimaneとServer Side Swift'> 2票

In [ ]:
all = Vote.query

In [ ]:
all = Vote.query

In [ ]:
all = Vote.query.all

In [ ]:
all = Vote.query.all

In [ ]:
Talk.query.all()

In [ ]:
def vote(code, nid):
    v = Vote.query.get(code)
    if v is not None:
        v.nid = nid
        try:
            db.session.add(v)
            db.session.commit()
            return True
        except:
            db.session.rollback()
    return False
# db.session.commit()

In [ ]:
# db.session.rollback()

In [ ]:
vote("hoge", "101")

In [ ]:
vote("hoge3", "")

In [ ]:
import csv

In [ ]:
def codes_from_csv(filename):
    with open(filename, newline='') as csvfile:
        reader = csv.reader(csvfile, delimiter=',', quotechar='"')
        header = next(reader)
#         print(header)
        for row in reader:
            yield row[0]

for code in codes_from_csv("special.csv"):
    print(code)

In [ ]:
filenames = ["normal.csv", "rebuild.csv", "speaker.csv", "special.csv", "sponsor-extra.csv", "sponsor.csv", "staff.csv"]

In [ ]:
# 初期データ
for f in filenames:
    for code in codes_from_csv(f):
        v = Vote.query.get(code)
        if v is None:
            print(code, v)
#         v = Vote(code=code, nid="-1")
#         print(v)
#         db.session.add(v)
#         db.session.commit()

In [ ]:
# 追加用
codes = []

for code in codes:
    v = Vote(code=code, nid="-1")
    db.session.add(v)
    db.session.commit()

In [ ]:
# トーク初期データ
# yaml_path = path.join(base_path, 'data', 'speakers', '*.yaml')
# path_list = glob(yaml_path)
# data = list(map(lambda path: yaml.load(open(path).read()), path_list))
# for d in data:
#     print(d["session"]["time"])
#     if not d["session"]["time"].startswith("5分"):
#         t = Talk(nid=d["session"]["nid"], title=d["session"]["title"], speaker=d["session"]["speaker"]["name"])
#         db.session.add(t)
#         print(t, t.speaker)
# db.session.commit()

In [ ]:
# Vote.query.all()
db.session.expire_all()
Address.query.all()