Skip to content

Instantly share code, notes, and snippets.

@kurozumi
Last active Jan 24, 2017
Embed
What would you like to do?
【Python】peeweeを使ってEC-CUBE2.13の会員情報をダウンロードする方法
# coding: utf-8
from peewee import *
from playhouse.csv_loader import dump_csv
from playhouse.shortcuts import case
from models import DtbCustomer as C
from models import MtbPref as P
from models import MtbSex as S
from models import MtbJob as J
import datetime
def download(filename):
with open(filename, "w") as fh:
query = C.select(
C.email.alias("メールアドレス"),
C.customer.alias("会員ID"),
C.name01.alias("姓"),
C.name02.alias("名"),
C.kana01.alias("フリガナ(姓)"),
C.kana02.alias("フリガナ(名)"),
fn.CONCAT(C.tel01, "-", C.tel02, "-", C.tel03).alias("電話番号"),
fn.CONCAT(C.zip01, "-", C.zip02).alias("郵便番号"),
P.name.alias("都道府県"),
C.addr01.alias("住所1"),
C.addr02.alias("住所2"),
S.name.alias("性別"),
J.name.alias("職業"),
fn.DATE(C.birth).alias("誕生日"),
case(None, ((C.mailmaga_flg == 1, "HTMLメール"), (C.mailmaga_flg == 2, "テキストメール"), (C.mailmaga_flg == 3, "希望しない")), None).alias("メルマガ"),
fn.DATE(C.first_buy_date).alias("初回購入日"),
fn.DATE(C.last_buy_date).alias("最終購入日"),
C.buy_times.alias("購入回数"),
C.buy_total.alias("購入累計金額"),
C.point.alias("ポイント残高"),
fn.DATE(C.create_date).alias("登録日"),
fn.DATE(C.update_date).alias("更新日")
).join(
P, on=(P.id==C.pref)
).join(
S, on=(S.id==C.sex)
).join(
J, on=(J.id==C.job)
).where(
(C.status != 1) &
(C.del_flg == 0)
)
dump_csv(query, fh)
if __name__ == "__main__":
download("customer.csv")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment