def account_password_reset(request): err_msg = '' if request.method == 'POST': form = PasswordResetForm(request.POST) if form.is_valid(): email = form.cleaned_data.get('email') try: user = User.objects.get(email=email) except User.DoesNotExist: err_msg = design.no_such_email_in_database if err_msg == '': # change password to new random hash new_password = create_hash(12) # save it user.set_password(new_password) user.save() # send it in email subject = design.password_reset_on_website context = Context({ 'new_password': new_password, 'host': request.get_host(), }) message_txt = get_template('email/password_reset.txt').render(context) message_html = get_template('email/password_reset.html').render(context) send_html_mail(subject, message_txt, message_html, [email]) return render_to_response('account/password_reset_ok.html', locals(), context_instance=RequestContext(request)) else: form = PasswordResetForm() return render_to_response('account/password_reset.html', locals(), context_instance=RequestContext(request))
def send_invitation_email(invite, host): if not invite.invitee.get_profile().email_notifications: return subject = design.x_is_inviting_you_to_join_y.format(invite.inviter.username, invite.band.title) context = Context({ 'invite': invite, 'host': host, }) message_txt = get_template('workbench/email/invitation_direct.txt').render(context) message_html = get_template('workbench/email/invitation_direct.html').render(context) send_html_mail(subject, message_txt, message_html, [invite.invitee.email])
def send_invitation_email(invite, host): if not invite.invitee.get_profile().email_notifications: return subject = design.x_is_inviting_you_to_join_y.format( invite.inviter.username, invite.band.title) context = Context({ 'invite': invite, 'host': host, }) message_txt = get_template('workbench/email/invitation_direct.txt').render( context) message_html = get_template( 'workbench/email/invitation_direct.html').render(context) send_html_mail(subject, message_txt, message_html, [invite.invitee.email])
def account_password_reset(request): err_msg = '' if request.method == 'POST': form = PasswordResetForm(request.POST) if form.is_valid(): email = form.cleaned_data.get('email') try: user = User.objects.get(email=email) except User.DoesNotExist: err_msg = design.no_such_email_in_database if err_msg == '': # change password to new random hash new_password = create_hash(12) # save it user.set_password(new_password) user.save() # send it in email subject = design.password_reset_on_website context = Context({ 'new_password': new_password, 'host': request.get_host(), }) message_txt = get_template('email/password_reset.txt').render( context) message_html = get_template( 'email/password_reset.html').render(context) send_html_mail(subject, message_txt, message_html, [email]) return render_to_response( 'account/password_reset_ok.html', locals(), context_instance=RequestContext(request)) else: form = PasswordResetForm() return render_to_response('account/password_reset.html', locals(), context_instance=RequestContext(request))
def user_register_plan(request, plan_url): "plan_url of 'free' means free plan" if request.user.is_authenticated(): return change_plan(request, plan_url) if request.method == 'POST': form = RegisterForm(request.POST) if form.is_valid(): plan_id = 0 plan = None # create the user user = User.objects.create_user(form.cleaned_data.get('username'), form.cleaned_data.get('email'), form.cleaned_data.get('password')) user.save() # create a band band = Band() band.title = form.cleaned_data.get('artist_name') band.total_space = settings.BAND_INIT_SPACE band.save() # create a profile profile = Profile() profile.user = user profile.solo_band = band profile.activated = False profile.activate_code = create_hash(32) profile.logon_count = 0 profile.band_count_limit = settings.FREE_BAND_LIMIT profile.save() # make them a manager manager = BandMember() manager.user = user manager.band = band manager.role = BandMember.MANAGER manager.save() # send an activation email subject = design.account_confirmation context = Context({ 'user': user, 'activate_url': request.build_absolute_uri(reverse('confirm', args=[user.username, profile.activate_code])), 'host': request.get_host(), }) message_txt = get_template('email/activation.txt').render(context) message_html = get_template('email/activation.html').render(context) send_html_mail(subject, message_txt, message_html, [user.email]) return HttpResponseRedirect(reverse("register_pending")) else: try: plan = AccountPlan.objects.get(url=plan_url) plan_id = plan.id except AccountPlan.DoesNotExist: plan = None plan_id = 0 form = RegisterForm(initial={'plan': plan_id}) return render_to_response('register.html', {'form': form}, context_instance=RequestContext(request))
def create_project(request, band_id_str): band = get_object_or_404(Band, id=int(band_id_str)) err_msg = '' permission = band.permission_to_work(request.user) if not permission: err_msg = design.only_band_members_can_create_projects if request.method == 'POST': form = NewProjectForm(request.POST, request.FILES) if form.is_valid() and permission: mp3_file = request.FILES.get('file_mp3') source_file = request.FILES.get('file_source') # upload the song result = upload_song(request.user, file_mp3_handle=mp3_file, file_source_handle=source_file, band=band, song_title=form.cleaned_data.get('title'), song_comments=form.cleaned_data.get('comments', ''), filename_appendix="_1") if not result['success']: err_msg = result['reason'] else: # fill in the rest of the fields song = result['song'] song.save() # create the project project = Project() project.band = band project.save() # create the first version version = ProjectVersion() version.project = project version.song = song version.version = 1 version.saveNewVersion() # subscribe the creator project.subscribers.add(request.user) project.save() # email band members saying that a new project is posted. for member in BandMember.objects.filter(band=band).exclude(user=request.user): if member.user.get_profile().email_notifications: context = Context({ 'project': project, 'version': version, 'member': member, 'host': request.get_host(), }) message_txt = get_template('workbench/email/new_project.txt').render(context) message_html = get_template('workbench/email/new_project.html').render(context) subject = design.x_uploaded_new_project_to_y.format(version.owner.username, band.title) send_html_mail(subject, message_txt, message_html, [member.user.email]) return HttpResponseRedirect(reverse("workbench.project", args=[band.id, project.id])) else: form = NewProjectForm() return render_to_response('workbench/band/new_project.html', { 'form': form, 'err_msg': err_msg, 'band': band, 'permission': permission, }, context_instance=RequestContext(request))
def ajax_email_invite(request): "send a band invitation by email." to_email = get_val(request.POST, 'email', '') band = get_obj_from_request(request.POST, 'band', Band) if band is None: return json_failure(design.bad_band_id) if not band.permission_to_invite(request.user): return json_failure(design.lack_permission_to_invite) if to_email == '': return json_failure(design.you_must_supply_an_email_address) if not is_valid_email(to_email): return json_failure(design.invalid_email_address) # if the email is a registered solid composer user, simply translate into direct invitation. try: local_user = User.objects.get(email=to_email) except User.DoesNotExist: local_user = None invite = BandInvitation() invite.inviter = request.user invite.band = band invite.role = BandMember.BAND_MEMBER subject = design.x_is_inviting_you_to_join_y.format(request.user.username, band.title) if local_user is not None: # make sure the user isn't already in the band if BandMember.objects.filter(user=local_user, band=band).count() > 0: return json_failure(design.x_already_in_band.format(local_user.username)) # make sure there isn't already an invitation for them if BandInvitation.objects.filter(invitee=local_user, band=band).count() > 0: return json_failure(design.already_invited_x_to_your_band.format(local_user.username)) invite.invitee = local_user invite.save() # send a heads up email if local_user.get_profile().email_notifications: context = Context({ 'user': request.user, 'band': band, 'invite': invite, 'host': request.get_host(), }) message_txt = get_template('workbench/email/invitation_direct.txt').render(context) message_html = get_template('workbench/email/invitation_direct.html').render(context) send_html_mail(subject, message_txt, message_html, [to_email]) return json_success() else: # create invitation link invite.expire_date = datetime.now() + timedelta(days=30) invite.code = create_hash(32) invite.save() # send the invitation email context = Context({ 'user': request.user, 'band': band, 'invite': invite, 'host': request.get_host(), }) message_txt = get_template('workbench/email/invitation_link.txt').render(context) message_html = get_template('workbench/email/invitation_link.html').render(context) send_html_mail(subject, message_txt, message_html, [to_email]) return json_success()
def user_register_plan(request, plan_url): "plan_url of 'free' means free plan" if request.user.is_authenticated(): return change_plan(request, plan_url) if request.method == 'POST': form = RegisterForm(request.POST) if form.is_valid(): plan_id = 0 plan = None # create the user user = User.objects.create_user(form.cleaned_data.get('username'), form.cleaned_data.get('email'), form.cleaned_data.get('password')) user.save() # create a band band = Band() band.title = form.cleaned_data.get('artist_name') band.total_space = settings.BAND_INIT_SPACE band.save() # create a profile profile = Profile() profile.user = user profile.solo_band = band profile.activated = False profile.activate_code = create_hash(32) profile.logon_count = 0 profile.band_count_limit = settings.FREE_BAND_LIMIT profile.save() # make them a manager manager = BandMember() manager.user = user manager.band = band manager.role = BandMember.MANAGER manager.save() # send an activation email subject = design.account_confirmation context = Context({ 'user': user, 'activate_url': request.build_absolute_uri( reverse('confirm', args=[user.username, profile.activate_code])), 'host': request.get_host(), }) message_txt = get_template('email/activation.txt').render(context) message_html = get_template('email/activation.html').render( context) send_html_mail(subject, message_txt, message_html, [user.email]) return HttpResponseRedirect(reverse("register_pending")) else: try: plan = AccountPlan.objects.get(url=plan_url) plan_id = plan.id except AccountPlan.DoesNotExist: plan = None plan_id = 0 form = RegisterForm(initial={'plan': plan_id}) return render_to_response('register.html', {'form': form}, context_instance=RequestContext(request))
from django.template.loader import get_template import sys from main.models import Profile from django.contrib.sites.models import Site if len(sys.argv) < 3: print("Usage:\n{0} template_name subject\n\ntemplate_name should be the prefix of two templates ending in .html and .txt".format(sys.argv[0])) sys.exit(1) # send mail to everybody who doesn't have newsletters turned off template_name = sys.argv[1] subject = sys.argv[2] html = template_name + ".html" txt = template_name + ".txt" current_site = Site.objects.get_current() for profile in Profile.objects.all(): if profile.email_newsletter: print("Emailing {0}...".format(profile.user.username)) context = Context({ 'user': profile.user, 'host': current_site.domain, }) message_txt = get_template(template_name + '.txt').render(context) message_html = get_template(template_name + '.html').render(context) send_html_mail(subject, message_txt, message_html, [profile.user.email]) else: print("Skipping {0}...".format(profile.user.username))
from django.contrib.sites.models import Site if len(sys.argv) < 3: print( "Usage:\n{0} template_name subject\n\ntemplate_name should be the prefix of two templates ending in .html and .txt" .format(sys.argv[0])) sys.exit(1) # send mail to everybody who doesn't have newsletters turned off template_name = sys.argv[1] subject = sys.argv[2] html = template_name + ".html" txt = template_name + ".txt" current_site = Site.objects.get_current() for profile in Profile.objects.all(): if profile.email_newsletter: print("Emailing {0}...".format(profile.user.username)) context = Context({ 'user': profile.user, 'host': current_site.domain, }) message_txt = get_template(template_name + '.txt').render(context) message_html = get_template(template_name + '.html').render(context) send_html_mail(subject, message_txt, message_html, [profile.user.email]) else: print("Skipping {0}...".format(profile.user.username))
def ajax_email_invite(request): "send a band invitation by email." to_email = get_val(request.POST, 'email', '') band = get_obj_from_request(request.POST, 'band', Band) if band is None: return json_failure(design.bad_band_id) if not band.permission_to_invite(request.user): return json_failure(design.lack_permission_to_invite) if to_email == '': return json_failure(design.you_must_supply_an_email_address) if not is_valid_email(to_email): return json_failure(design.invalid_email_address) # if the email is a registered solid composer user, simply translate into direct invitation. try: local_user = User.objects.get(email=to_email) except User.DoesNotExist: local_user = None invite = BandInvitation() invite.inviter = request.user invite.band = band invite.role = BandMember.BAND_MEMBER subject = design.x_is_inviting_you_to_join_y.format( request.user.username, band.title) if local_user is not None: # make sure the user isn't already in the band if BandMember.objects.filter(user=local_user, band=band).count() > 0: return json_failure( design.x_already_in_band.format(local_user.username)) # make sure there isn't already an invitation for them if BandInvitation.objects.filter(invitee=local_user, band=band).count() > 0: return json_failure( design.already_invited_x_to_your_band.format( local_user.username)) invite.invitee = local_user invite.save() # send a heads up email if local_user.get_profile().email_notifications: context = Context({ 'user': request.user, 'band': band, 'invite': invite, 'host': request.get_host(), }) message_txt = get_template( 'workbench/email/invitation_direct.txt').render(context) message_html = get_template( 'workbench/email/invitation_direct.html').render(context) send_html_mail(subject, message_txt, message_html, [to_email]) return json_success() else: # create invitation link invite.expire_date = datetime.now() + timedelta(days=30) invite.code = create_hash(32) invite.save() # send the invitation email context = Context({ 'user': request.user, 'band': band, 'invite': invite, 'host': request.get_host(), }) message_txt = get_template( 'workbench/email/invitation_link.txt').render(context) message_html = get_template( 'workbench/email/invitation_link.html').render(context) send_html_mail(subject, message_txt, message_html, [to_email]) return json_success()
def create_project(request, band_id_str): band = get_object_or_404(Band, id=int(band_id_str)) err_msg = '' permission = band.permission_to_work(request.user) if not permission: err_msg = design.only_band_members_can_create_projects if request.method == 'POST': form = NewProjectForm(request.POST, request.FILES) if form.is_valid() and permission: mp3_file = request.FILES.get('file_mp3') source_file = request.FILES.get('file_source') # upload the song result = upload_song(request.user, file_mp3_handle=mp3_file, file_source_handle=source_file, band=band, song_title=form.cleaned_data.get('title'), song_comments=form.cleaned_data.get( 'comments', ''), filename_appendix="_1") if not result['success']: err_msg = result['reason'] else: # fill in the rest of the fields song = result['song'] song.save() # create the project project = Project() project.band = band project.save() # create the first version version = ProjectVersion() version.project = project version.song = song version.version = 1 version.saveNewVersion() # subscribe the creator project.subscribers.add(request.user) project.save() # email band members saying that a new project is posted. for member in BandMember.objects.filter(band=band).exclude( user=request.user): if member.user.get_profile().email_notifications: context = Context({ 'project': project, 'version': version, 'member': member, 'host': request.get_host(), }) message_txt = get_template( 'workbench/email/new_project.txt').render(context) message_html = get_template( 'workbench/email/new_project.html').render(context) subject = design.x_uploaded_new_project_to_y.format( version.owner.username, band.title) send_html_mail(subject, message_txt, message_html, [member.user.email]) return HttpResponseRedirect( reverse("workbench.project", args=[band.id, project.id])) else: form = NewProjectForm() return render_to_response('workbench/band/new_project.html', { 'form': form, 'err_msg': err_msg, 'band': band, 'permission': permission, }, context_instance=RequestContext(request))