def profile_alpha(request, userid): user = get_object_or_404(User, pk=userid) if not user.is_verified: url = reverse('phone_setup', kwargs={'userid': userid}) return HttpResponseRedirect(url) # process submitted form if request.method == 'POST': form = UserProfileForm(request.POST) # A form bound to the POST data if form.is_valid(): # All validation rules pass # save form goals = form.cleaned_data.get('goal') new_profile = form.save(commit=False) new_profile.userid = user new_profile.goal = ' '.join(each.encode('utf-8') for each in goals) new_profile.goal = new_profile.goal[0] #print type(new_profile.goal) #print type(new_profile.goal[0]) new_profile.save() # send text msg = new_profile.get_edit_profile_message() send_text(msg, to=user.phone) # reactivate user if not user.is_active: user.is_active = True user.save() # send email send_mail('WattTime account activated', messages.account_activated_message(user.userid, user.name, user.phone).msg, EMAIL_HOST_USER, [user.email], fail_silently=False) # redirect url = reverse('welcome_alpha') return HttpResponseRedirect(url) else: form = UserProfileForm() # An unbound form # display form return render(request, 'accounts/signup_alpha.html', { 'form': form, 'userid': userid, })
def send_verification_code(user): up = user.get_profile() code = new_phone_verification_number() up.verification_code = code up.save() print ("Sending {} verification code {:d}".format(up.name, code)) msg = messages.verify_phone_message(code) sent = twilio_utils.send_text(msg, up, force = True) if sent: print ("Send successful.") else: print ("Send unsuccessful.") return sent
def phone_verify(request, userid): user = get_object_or_404(User,pk=userid) print request.get_full_path()[-6:] if "resend" == request.get_full_path()[-6:]: print "resending message..." send_verification_code(user) form = UserVerificationForm() return render(request, 'accounts/phone_verify.html', { 'form': form, 'userid': userid, 'reenter': False, 'resend' : True, 'phone' : user.phone, }) # Note: code after this won't get called until event triggers (this is like a listener) if user.is_verified: url = reverse('profile_alpha', kwargs={'userid':userid}) return HttpResponseRedirect(url) if request.method == 'POST': form = UserVerificationForm(request.POST) if form.is_valid(): code1 = form.cleaned_data['verification_code'] code2 = user.verification_code print ("Checking codes: {:d} vs. {:d}".format(code1, code2)) if code1 == code2: # save verification and activation state user.is_verified = True user.is_active = True user.save() # send email send_mail('WattTime account activated', messages.account_activated_message(user.userid, user.name, user.phone).msg, EMAIL_HOST_USER, [user.email], fail_silently=False) # send text msg = messages.intro_message() send_text(msg, to=user.phone) # redirect url = reverse('profile_alpha', kwargs={'userid':userid}) return HttpResponseRedirect(url) else: # Meh #url = reverse('phone_verify', kwargs={'userid': userid, 'reenter' :True}) return render(request, 'accounts/phone_verify.html', {'form': form, 'userid' : userid, 'reenter' : True, 'resend' : False, 'phone' : user.phone}) #url = reverse('phone_setup', kwargs={'userid': userid}) #return HttpResponseRedirect(url) else: form = UserVerificationForm() return render(request, 'accounts/phone_verify.html', { 'form': form, 'userid': userid, 'reenter': False, 'resend' : False, 'phone' : user.phone, })
def send_ne_texts_if_necessary(): now = _now() now_ne = _now('America/New_York') is_weekday = (now_ne.isoweekday() <= 5) is_daytime = (9 <= now_ne.hour < 17) is_evening = (17 <= now_ne.hour < 22) fuel = NE.objects.all().latest().marginal_fuel fuel_name = MARGINAL_FUELS[fuel] # 0 = coal # 1 = oil # 2 = natural gas # 3 = refuse # 4 = hydro # 5 = wood # 6 = nuclear # 7 = solar # 8 = wind # 9 = none dirty_fuel = [0, 1] clean_fuel = [8, 7, 6, 5, 4] is_dirty = fuel in dirty_fuel is_clean = fuel in clean_fuel # No more than one message in each 20 hour period last_okay_time = now - datetime.timedelta(hours = 20) ups = UserProfile.objects.all() # Dirty texts, working hours if is_weekday and is_daytime and is_dirty: last_msg = latest_by_category(LastMessageSent.NE_dirty_daytime) if last_msg is None or last_msg.date < last_okay_time: if last_msg is None: last_msg = LastMessageSent() last_msg.category = LastMessageSent.NE_dirty_daytime last_msg.date = now last_msg.save() debug("Sending NE texts for dirty, working hours {}".format(last_msg.date)) for up in ups: if up.user.is_active and up.is_verified and up.region() == newengland: if up.get_region_settings().message_frequency == 0: message = ne_message_dirty_daytime(up, fuel_name) res = send_text(message, up) msg = 'Sent text "{}" to {}'.format(message.msg, up) if not res: msg = "FAILED: " + msg debug(msg) add_to_report(msg) # Dirty texts, after hours if is_dirty and (is_evening or (is_daytime and (not is_weekday))): last_msg = latest_by_category(LastMessageSent.NE_dirty_evening) if last_msg is None or last_msg.date < last_okay_time: if last_msg is None: last_msg = LastMessageSent() last_msg.category = LastMessageSent.NE_dirty_evening last_msg.date = now last_msg.save() debug("Sending NE texts for dirty, after hours {}".format(last_msg.date)) for up in ups: if up.user.is_active and up.is_verified and up.region() == newengland: if up.get_region_settings().message_frequency == 1: message = ne_message_dirty_evening(up, fuel_name) res = send_text(message, up) msg = 'Sent text "{}" to {}'.format(message.msg, up) if not res: msg = "FAILED: " + msg debug(msg) add_to_report(msg) # Clean texts, after hours if is_clean and (is_evening or (is_daytime and (not is_weekday))): last_msg = latest_by_category(LastMessageSent.NE_clean) if last_msg is None or last_msg.date < last_okay_time: if last_msg is None: last_msg = LastMessageSent() last_msg.category = LastMessageSent.NE_clean last_msg.date = now last_msg.save() debug("Sending NE texts for clean {}".format(last_msg.date)) for up in ups: if up.user.is_active and up.is_verified and up.region() == newengland: if up.get_region_settings().message_frequency == 2: message = ne_message_clean(up, fuel_name) res = send_text(message, up) msg = 'Sent text "{}" to {}'.format(message.msg, up) if not res: msg = "FAILED: " + msg debug(msg) add_to_report(msg)
localtime = up.local_now() if (up.is_verified and up.user.is_active and is_good_time_to_message(localtime, up.user_id, up)): debug(" is verified, is active, and is good time to message") else: debug(' either not verified, not active, or not good time to message') continue # get message # TODO: method does not exist! msg = up.get_personalized_message(percent_greens[ba_ind], percent_coals[ba_ind], marginal_fuels[ba_ind]) # send notification print msg if msg: if len(msg) <= 140: debug(' sending message!') # send text send_text(msg, to=up.user) notified_users.append(up.user_id) else: debug(' Failed to send message "{}" as it is too long.'.format(msg)) else: debug(' active, verified user, but not right fuel now') # return return notified_users