def add_channel_member(request, channel_id): """ Invite channel members. """ context = {} channel = get_object_or_404(Channel, id=channel_id, owner=request.user) form = InvitationForm() if request.method == "POST": form = InvitationForm(request.POST) if form.is_valid(): # Save confirmation data email = form.cleaned_data['email'] status = 'pending' # Automatically adds the invited user to the channel; invited user still receives confirmation link # Update: Create the user when it does not exist. user_created = False try: invited_user = User.objects.get(email=email) new_username = '' new_password = '' except User.DoesNotExist: new_username = generate_username(email) new_password = generate_random_string(6) invited_user = User.objects.create_user(username=new_username, password=new_password, email=email) user_created = True invited_user_profile = invited_user.userprofile invited_user_profile.channels.add(channel) invited_user_profile.save() Message.objects.create(user=invited_user, from_user=request.user, level=persistent_messages.INFO, message="Congratulations! You are now a contributor on the %s channel. You can \ create videos and save them to this channel after they are created. Now \ you can enjoy the features of a premium account at no cost to you." % channel.name) confirmation_info, created = ConfirmationInfo.objects.get_or_create( email=email, channel=channel ) # Send email to the new user confirmation_info.status = 'pending' confirmation_info.save() subject = "Invitation from Screenbird" from_mail = settings.DEFAULT_FROM_EMAIL link = "".join(("http://",request.META['HTTP_HOST'],reverse("confirm_channel_member"),"?key="+confirmation_info.key)) reg_link = "".join(("http://",request.META['HTTP_HOST'],reverse("register"))) if user_created: account_link = "".join(("http://",request.META['HTTP_HOST'],reverse("manage_account"))) message = render_to_string("email/collaborate_user_created.txt", { 'username':new_username, 'password':new_password, 'email': email, 'user':request.user.username, 'account_link':account_link, 'confirm_link':link }) else: message = render_to_string("email/collaborate.txt", { 'email': email, 'user':request.user.username, 'reg_link':reg_link, 'confirm_link':link }) send_mail(subject, message, from_mail, [email], fail_silently=False) context = {"success":True} messages.success(request, 'An invitation has been sent to %s.' % email) else: messages.error(request, 'Failed to send invitation.') context['form'] = form context['channel'] = channel return redirect(reverse('update_channel', args=[channel.id,]))
def add_cocreate_member(request, cocreate_id): """ Invite cocreate members. """ context = {} cocreate = get_object_or_404(CoCreate, id=cocreate_id, owner=request.user) form = InvitationForm() if request.method == "POST": form = InvitationForm(request.POST) if form.is_valid(): # Save confirmation data email = form.cleaned_data['email'] status = 'pending' # Automatically adds the invited user to the channel; invited user still receives confirmation link # Update: Create the user when it does not exist. created = False try: invited_user = User.objects.get(email=email) new_username = '' new_password = '' except User.DoesNotExist: new_username = generate_username(email) new_password = generate_random_string(6) invited_user = User.objects.create_user(username=new_username, password=new_password, email=email) created = True option = CocreateOption(user=invited_user, cocreate=cocreate) option.save() Message.objects.create(user=invited_user, from_user=request.user, level=persistent_messages.INFO, message="Congratulations! You are now a contributor on %s co-create videos. You can \ create videos and save them as a part of one big video project" % request.user.username) """ # Send email to the new user subject = "Invitation from Screenbird" from_mail = settings.DEFAULT_FROM_EMAIL reg_link = "".join(("http://",request.META['HTTP_HOST'],reverse("register"))) # TODO: Move mailing part to after create cocreate section # TODO: use date_joined and last_login to determine whether user is new or not if created: account_link = "".join(("http://",request.META['HTTP_HOST'],reverse("manage_account"))) cocreate_section_link = "".join(("http://", request.META['HTTP_HOST'], reverse("cocreate", args=(cocreate.pk,)))) message = render_to_string("email/cocreate_invite_screenbird.txt", { 'username':new_username, 'password':new_password, 'email': email, 'user':request.user.username, 'account_link':account_link, 'cocreate_section_link': cocreate_section_link, }) html_message = render_to_string("email/cocreate_invite_screenbird.html", { 'username': new_username, 'password': new_password, 'email': email, 'user': request.user.username, 'account_link': account_link, 'cocreate_section_link': cocreate_section_link }) mail = EmailMultiAlternatives(subject, message, from_mail, [email]) mail.attach_alternative(html_message, "text/html") mail.send() else: message = render_to_string("email/cocreate_collaborate.txt", { #'name':name, 'email': email, 'user':request.user.username, 'reg_link':reg_link, }) send_mail(subject, message, from_mail, [email], fail_silently=False) """ context = {"success":True} messages.success(request, 'An invitation has been sent to %s.' % email) if request.is_ajax(): return HttpResponse(simplejson.dumps({ 'status': 'OK', 'pk': invited_user.pk, 'username': invited_user.username }), mimetype='application/json') else: messages.error(request, 'Failed to send invitation.') if request.is_ajax(): return HttpResponse(simplejson.dumps({ 'status': 'ERROR', 'errors': form.errors }), mimetype='application/json') return redirect(reverse('cocreate', args=[cocreate.id,]))
def update_channel(request, channel_id, invitation_form=None): """ Allows the user to edit channel name and invite channel members. Note: This is visible only to the owner of the channel. """ message = "" context = {} channel = get_object_or_404(Channel, id=channel_id) channel_members = list(channel.get_channel_members()) form = UpdateChannelForm(instance=channel) invite_form = InvitationForm() # Get all invitations invitations = ConfirmationInfo.objects.filter(channel=channel) for channel_member in channel_members: # Do not allow my own permissions to be set if channel_member != request.user: channel_member.perm_form = ChannelOptionForm( channel=channel, user_profile=channel_member.userprofile, prefix='channel-%s' % channel_member.pk) status = 'pending' try: invitation = invitations.get(username=channel_member.username) status = invitation.status except: pass channel_member.status = status if (request.method == 'POST' and request.POST.get('action', 'default') == 'delete'): videos = Video.objects.filter(id__in=request.POST.getlist('item')) videos.update(channel=None) video_names = [video.title for video in videos] if videos: message = " ".join([ "The following video/s have been removed from this channel:", ", ".join(video_names), "." ]) """ deleted, for_approval = [], [] for video in videos: name = video.title if video.delete() is False: for_approval.append(name) else: deleted.append(name) if for_approval: message = " ".join(["Note that the removal of videos within a", "channel needs the admin's approval first.\n"]) if deleted: message = message + " ".join(["The following video/s have been deleted:", ", ".join(deleted), "."]) if for_approval: message = message + " ".join(["The following video/s are sent for approval to the admin:", ", ".join(for_approval), "."]) """ elif request.method == 'POST': if not invitation_form: form = ChannelForm(request.POST, instance=channel) if form.is_valid(): form.save() messages.success(request, 'Group name has been updated.') else: messages.error(request, 'Failed updating channel details.') else: # this one has been returned by the add_channel_member view if not invitation_form.is_valid(): invite_form = invitation_form videos = [v for v in channel.video_set.all()] videos_to_add = Video.objects.filter(uploader=request.user).filter( channel=None).order_by('-updated') context = { 'channel': channel, 'channel_form': form, 'invite_form': invite_form, 'channel_members': channel_members, 'videos': videos, 'videos_to_add': videos_to_add, 'message': message, } return render(request, 'update_channel.html', context)
def add_channel_member(request, channel_id): """ Invite channel members. """ context = {} channel = get_object_or_404(Channel, id=channel_id, owner=request.user) form = InvitationForm() if request.method == "POST": form = InvitationForm(request.POST) if form.is_valid(): # Save confirmation data email = form.cleaned_data['email'] status = 'pending' # Automatically adds the invited user to the channel; invited user still receives confirmation link # Update: Create the user when it does not exist. user_created = False try: invited_user = User.objects.get(email=email) new_username = '' new_password = '' except User.DoesNotExist: new_username = generate_username(email) new_password = generate_random_string(6) invited_user = User.objects.create_user(username=new_username, password=new_password, email=email) user_created = True invited_user_profile = invited_user.userprofile invited_user_profile.channels.add(channel) invited_user_profile.save() Message.objects.create( user=invited_user, from_user=request.user, level=persistent_messages.INFO, message= "Congratulations! You are now a contributor on the %s channel. You can \ create videos and save them to this channel after they are created. Now \ you can enjoy the features of a premium account at no cost to you." % channel.name) confirmation_info, created = ConfirmationInfo.objects.get_or_create( email=email, channel=channel) # Send email to the new user confirmation_info.status = 'pending' confirmation_info.save() subject = "Invitation from Screenbird" from_mail = settings.DEFAULT_FROM_EMAIL link = "".join(("http://", request.META['HTTP_HOST'], reverse("confirm_channel_member"), "?key=" + confirmation_info.key)) reg_link = "".join( ("http://", request.META['HTTP_HOST'], reverse("register"))) if user_created: account_link = "".join(("http://", request.META['HTTP_HOST'], reverse("manage_account"))) message = render_to_string( "email/collaborate_user_created.txt", { 'username': new_username, 'password': new_password, 'email': email, 'user': request.user.username, 'account_link': account_link, 'confirm_link': link }) else: message = render_to_string( "email/collaborate.txt", { 'email': email, 'user': request.user.username, 'reg_link': reg_link, 'confirm_link': link }) send_mail(subject, message, from_mail, [email], fail_silently=False) context = {"success": True} messages.success(request, 'An invitation has been sent to %s.' % email) else: messages.error(request, 'Failed to send invitation.') context['form'] = form context['channel'] = channel return redirect(reverse('update_channel', args=[ channel.id, ]))
def add_cocreate_member(request, cocreate_id): """ Invite cocreate members. """ context = {} cocreate = get_object_or_404(CoCreate, id=cocreate_id, owner=request.user) form = InvitationForm() if request.method == "POST": form = InvitationForm(request.POST) if form.is_valid(): # Save confirmation data email = form.cleaned_data['email'] status = 'pending' # Automatically adds the invited user to the channel; invited user still receives confirmation link # Update: Create the user when it does not exist. created = False try: invited_user = User.objects.get(email=email) new_username = '' new_password = '' except User.DoesNotExist: new_username = generate_username(email) new_password = generate_random_string(6) invited_user = User.objects.create_user(username=new_username, password=new_password, email=email) created = True option = CocreateOption(user=invited_user, cocreate=cocreate) option.save() Message.objects.create( user=invited_user, from_user=request.user, level=persistent_messages.INFO, message= "Congratulations! You are now a contributor on %s co-create videos. You can \ create videos and save them as a part of one big video project" % request.user.username) """ # Send email to the new user subject = "Invitation from Screenbird" from_mail = settings.DEFAULT_FROM_EMAIL reg_link = "".join(("http://",request.META['HTTP_HOST'],reverse("register"))) # TODO: Move mailing part to after create cocreate section # TODO: use date_joined and last_login to determine whether user is new or not if created: account_link = "".join(("http://",request.META['HTTP_HOST'],reverse("manage_account"))) cocreate_section_link = "".join(("http://", request.META['HTTP_HOST'], reverse("cocreate", args=(cocreate.pk,)))) message = render_to_string("email/cocreate_invite_screenbird.txt", { 'username':new_username, 'password':new_password, 'email': email, 'user':request.user.username, 'account_link':account_link, 'cocreate_section_link': cocreate_section_link, }) html_message = render_to_string("email/cocreate_invite_screenbird.html", { 'username': new_username, 'password': new_password, 'email': email, 'user': request.user.username, 'account_link': account_link, 'cocreate_section_link': cocreate_section_link }) mail = EmailMultiAlternatives(subject, message, from_mail, [email]) mail.attach_alternative(html_message, "text/html") mail.send() else: message = render_to_string("email/cocreate_collaborate.txt", { #'name':name, 'email': email, 'user':request.user.username, 'reg_link':reg_link, }) send_mail(subject, message, from_mail, [email], fail_silently=False) """ context = {"success": True} messages.success(request, 'An invitation has been sent to %s.' % email) if request.is_ajax(): return HttpResponse(simplejson.dumps({ 'status': 'OK', 'pk': invited_user.pk, 'username': invited_user.username }), mimetype='application/json') else: messages.error(request, 'Failed to send invitation.') if request.is_ajax(): return HttpResponse(simplejson.dumps({ 'status': 'ERROR', 'errors': form.errors }), mimetype='application/json') return redirect(reverse('cocreate', args=[ cocreate.id, ]))