from otree.api import ( models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer, Currency as c, currency_range, ) import os from base64 import b64decode from GmailApi.GmailApi import SendEmail author = 'Your name here' doc = """ Your app description """ class Constants(BaseConstants): name_in_url = 'payment_T5' players_per_group = 4 num_rounds = 1 expt_point = 0.2 class Subsession(BaseSubsession): def update_grouping(self): all_subessions = self.in_rounds(1, Constants.num_rounds) for subsession in all_subessions: players = subsession.get_players() players.sort(key=lambda player: player.participant.vars['slider_value']) yellowGroup = players[:Constants.players_per_group] redGroup = players[Constants.players_per_group:] subsession.set_group_matrix([yellowGroup, redGroup]) class Group(BaseGroup): def send_email_pdf_figs(self, path_to_pdf, subject, message, destination): ## credits: http://linuxcursor.com/python-programming/06-how-to-send-pdf-ppt-attachment-with-html-body-in-python-script from socket import gethostname #import email from email.mime.application import MIMEApplication from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText import smtplib server = smtplib.SMTP('smtp.gmail.com', 587) server.starttls() server.login('alvinx.tan@gmail.com', 'tanls8890') # Craft message (obj) msg = MIMEMultipart() message = f'{message}\nSend from Hostname: {gethostname()}' msg['Subject'] = subject msg['From'] = 'me@gmail.com' msg['To'] = destination # Insert the text to the msg going by e-mail msg.attach(MIMEText(message, "plain")) # Attach the pdf to the msg going by e-mail print(path_to_pdf) with open(path_to_pdf, "rb") as f: #attach = email.mime.application.MIMEApplication(f.read(),_subtype="pdf") attach = MIMEApplication(f.read(),_subtype="pdf") attach.add_header('Content-Disposition','attachment',filename=str(path_to_pdf)) msg.attach(attach) # send msg server.send_message(msg) def ack_payment(self, id_in_group, payload): b64 = payload['base64pdf'] player = self.get_player_by_id(id_in_group) pdfPath = f'{self.session.code}_{player.participant.id_in_session}.pdf' bytes_decoded = b64decode(b64, validate=True) with open(pdfPath, 'wb') as file: file.write(bytes_decoded) file.close() send_email = SendEmail() send_email.send(pdfPath, f'{self.session.code} Participant: {player.participant.id_in_session}') # self.send_email_pdf_figs(pdfPath, f'{self.session.code} Participant: {player.participant.id_in_session}', 'testest', 'alvinx.tan@gmail.com') return { id_in_group: True } class Player(BasePlayer): mobile_number = models.StringField() name = models.StringField() payoff_from_round = models.IntegerField() lottery_earnings = models.IntegerField() show_up_fee = models.IntegerField() total_earnings = models.FloatField() payoff_from_game_number = models.IntegerField() stage3payoff = models.IntegerField() receipt_ack = models.BooleanField( widget=widgets.CheckboxInput, ) def start(self): self.stage3payoff = self.participant.vars['Stage3Earning']['Earning'] self.payoff_from_game_number = self.participant.vars['Stage3Earning']['Game_number'] self.payoff_from_round = self.participant.vars['Stage3Earning']['Round'] self.lottery_earnings = 6 if self.participant.vars['dice_number'] == 6 else 0 self.show_up_fee = int(self.session.config['participation_fee']) self.total_earnings = self.show_up_fee + float(Constants.expt_point * self.stage3payoff) + self.lottery_earnings self.payoff = self.stage3payoff + self.lottery_earnings / Constants.expt_point def custom_export(players): # header row yield ['session', 'participant_code', 'participant_id', 'show up fee', 'lottery earnings', 'payoff from stage 3', 'payoff from round', 'payoff from game number', 'total earnings', 'full name', 'mobile number'] for p in players: yield [p.session.code, p.participant.code, p.participant.id_in_session, p.show_up_fee, p.lottery_earnings, p.stage3payoff, p.payoff_from_round, p.payoff_from_game_number, p.total_earnings, p.name, p.mobile_number]