def flag_flowgram(request, enc, flowgram): from flowgram.core.mail import announce_flag announce_flag(request, flowgram) helpers.add_good_message(request, "Thanks for your help. An administrator will review this Flowgram.") return HttpResponseRedirect(flowgram.url())
def reset_password_makenew(request, enc, user, key): from flowgram.core.mail import validate_password_reset_hash if request.method == 'POST': if not validate_password_reset_hash(user, key): raise Http404 new_password = request.POST['new_password'] user.set_password(new_password) user.save() helpers.add_good_message(request, "Your new password was set.") return HttpResponseRedirect('/you/') if not user or not key: helpers.add_bad_message(request, "Please make sure you copied the whole URL from your email.") return HttpResponseRedirect('/reset/') if not validate_password_reset_hash(user, key): helpers.add_bad_message( request, 'Please make sure you copied the whole URL from your email. If the email is more than one day old, request a new email on this page.') return HttpResponseRedirect('/reset/') return helpers.req_render_to_response(request, 'user/enter_new_password.html', {'user': user, 'key': key})
def view_subscriptions(request): u = request.user p = u.get_profile() if request.method == 'POST': f = NotifyForm(request.POST) if f.is_valid(): cleaned = f.cleaned_data p.notification_freq = cleaned['notification_freq'] p.notify_by_email = cleaned['notify_by_email'] p.has_public_news_feed = cleaned['has_public_news_feed'] p.save() url = "/subscriptions/" add_good_message(request, "Your settings have been successfully updated.") return HttpResponseRedirect(url) initial = { 'notification_freq': p.notification_freq, 'notify_by_email': p.notify_by_email, 'has_public_news_feed': p.has_public_news_feed, } f = NotifyForm(initial=initial) # Get User profiles items from list of subscriptions user_subscriptions = controller.get_user_subscriptions(u) subscription_profiles = [] for sub in user_subscriptions: subscription_user = UserProfile.objects.get(user=sub["user"]) avatar_url= UserProfile.avatar_100(subscription_user) subscription_profiles.append({'username':subscription_user.user,'avatar_url':avatar_url}) # Get User profiles items from list of subscribers subscribers = controller.get_user_subscribers(u) subscriber_profiles = [] for sub in subscribers: subscriber_user = UserProfile.objects.get(user=sub["subscriber"]) avatar_url= UserProfile.avatar_100(subscriber_user) subscriber_profiles.append({'username':subscriber_user.user,'avatar_url':avatar_url}) # For secondary nav include activesecondary = 'subscriptions' show_unsubscribe = True return req_render_to_response(request, 'user/view_subscriptions.html', { 'subscription_profiles':subscription_profiles, 'activesecondary': activesecondary, 'subs_active':subs_active, 'profile':p, 'form': f, 'u':u, 'show_unsubscribe': show_unsubscribe, 'subscriber_profiles':subscriber_profiles, 'mostviewed': cached_sets.most_viewed()[:6], })
def delete_press(request, enc, current_press_id): if request.method == 'GET': return helpers.req_render_to_response( request, 'dialogs/delete_press.html', {'current_press_item': models.FlowgramPress.objects.get(id=current_press_id)}) else: models.FlowgramPress.objects.get(id=current_press_id).delete() helpers.add_good_message(request, "You deleted a press item. Congratulations.") return HttpResponseRedirect('/adminpress')
def delete_tutorial(request, enc, tutorial_id): tutorial = Tutorial.objects.get(id=tutorial_id) if request.method == 'GET': return helpers.req_render_to_response( request, 'dialogs/delete_tutorial.html', {'tutorial': tutorial,}) else: Tutorial.objects.get(id=tutorial_id).delete() helpers.add_good_message(request, "You deleted a tutorial. Congratulations.") return HttpResponseRedirect('/admintutorials')
def remove_subscription(request, subscribed_to_username): url = '/subscriptions/' subscribed_user = models.User.objects.get(username=subscribed_to_username) models.Subscription.objects.filter(user=subscribed_user, subscriber=request.user).delete() # UserHistory. controller.store_subscription_event( {'user': request.user, 'target_user': subscribed_user, 'eventCode': 'UNSUB'}) helpers.add_good_message(request, 'You successfully unsubscribed from %s.' % subscribed_to_username) return HttpResponseRedirect(url)
def inviter(request, enc, recipients, email_body): from django.template import Context, Template from flowgram.queueprocessors.sendemailrequestprocessor import add_to_mail_queue if request.method == 'GET': context = {'from_email' : request.user.email, 'default_email' : DEFAULT_INVITATION + request.user.username} return helpers.req_render_to_response(request, 'admin/inviter.html', context) for recipient in helpers.get_email_addresses_from_comma_separated_string(recipients): context = {} if localsettings.FEATURE['use_regcode']: context['regcode'] = models.Regcode.objects.create(sender=request.user).code # TODO(westphal): Figure out and remove or document: Why are we using unicode and then a # non-unicode string. body = '%s' % unicode(Template(email_body).render(Context(context))) add_to_mail_queue(request.user.email, recipient, "Invitation to Flowgram.com", body) helpers.add_good_message(request, 'Emails added to queue.') return HttpResponseRedirect('/a/inviter/')
def reset_password_post(request, enc, user, email): from flowgram.core.mail import reset_password if email: try: user = auth_models.User.objects.get(email=email) except auth_models.User.DoesNotExist: helpers.add_bad_message(request, "No account was found for that email address.") return helpers.req_render_to_response(request, 'user/reset_password.html') if not user: helpers.add_bad_message(request, "You must enter your username or email.") return helpers.req_render_to_response(request, 'user/reset_password.html') if reset_password(user): helpers.add_good_message(request, "Your password reset email has been sent. Follow the emailed instructions.") return helpers.req_render_to_response(request, 'user/reset_password.html') else: helpers.add_bad_message(request, "We were unable to reach your account email address.") return helpers.req_render_to_response(request, 'user/reset_password.html')
def edit_profile(request): u = request.user p = u.get_profile() if request.method == 'GET': initial = { 'gender': p.gender, 'newsletter_optin': p.newsletter_optin, 'birthdate': p.birthdate, 'homepage': p.homepage, 'email': u.email, 'description': p.description, #'subscribe_fg_on_comment': p.subscribe_fg_on_comment, #'subscribed_to_own_fgs': p.subscribed_to_own_fgs, } f = ProfileForm(initial=initial) return req_render_to_response(request, 'user/edit_profile.html', { 'profile': p, 'form': f, 'subs_active': subs_active, }) elif request.method == 'POST': f = ProfileForm(request.POST) if f.is_valid(): cleaned = f.cleaned_data p.gender = cleaned['gender'] p.newsletter_optin = cleaned['newsletter_optin'] p.birthdate = cleaned['birthdate'] p.homepage = cleaned['homepage'] p.description = cleaned['description'] #p.subscribe_fg_on_comment = cleaned['subscribe_fg_on_comment'] #p.subscribed_to_own_fgs = cleaned['subscribed_to_own_fgs'] p.save() u.email = cleaned['email'] u.save() add_good_message(request, "Your profile was updated.") controller.record_stat(request, 'edit_profile_website', '0', '') EmailSubscriptionRequest.objects.create(should_subscribe=p.newsletter_optin, email_address=u.email) return HttpResponseRedirect(p.url())
def create_subscription(request, subscribe_to_username): url = '/%s/' % subscribe_to_username subscribe_to_user = models.User.objects.get(username=subscribe_to_username) try: # If the subscription alredy exists, do nothing. models.Subscription.objects.get(user=subscribe_to_user, subscriber=request.user) except models.Subscription.DoesNotExist: # If the subscription does not alredy exist. models.Subscription.objects.create(user=subscribe_to_user, subscriber=request.user) # UserHistory. controller.store_subscription_event( {'user': request.user, 'target_user': subscribe_to_user, 'eventCode': 'SUB'}) helpers.add_good_message(request, 'You successfully subscribed to %s.' % subscribe_to_username) return HttpResponseRedirect(url)
def admin_tutorials(request, enc, tutorial_id): if tutorial_id: # edit existing tutorial tutorial = Tutorial.objects.get(id=tutorial_id) video_path = '' if request.method == 'POST': form = TutorialForm(request.POST) if form.is_valid(): cleaned = form.cleaned_data tutorial.title = cleaned['title'] tutorial.position = cleaned['position'] if cleaned['content']: tutorial.content = cleaned['content'] else: tutorial.content = '' tutorial.active = cleaned['active'] tutorial.save() tutorialhelpers.save_uploaded_video(request, tutorial, form) helpers.add_good_message( request, "You modified a tutorial. Congratulations.") else: print 'Some kind of problem with your form entry, try again.' if tutorial.video_path: video_path = tutorialhelpers.get_tutorial_video_path(tutorial) return helpers.req_render_to_response( request, "admin/admin_tutorials.html", { 'form': form, 'tutorial_id': tutorial_id, 'tutorial': tutorial, 'video_path': video_path, }) else: # request.method == 'GET' form = TutorialForm(initial={ 'position': tutorial.position, 'title': tutorial.title, 'content': tutorial.content, 'active': tutorial.active, }) if tutorial.video_path: video_path = tutorialhelpers.get_tutorial_video_path(tutorial) return helpers.req_render_to_response( request, "admin/admin_tutorials.html", { 'form': form, 'tutorial_id': tutorial_id, 'tutorial': tutorial, 'video_path': video_path, }) else: # create new tutorial / list existing tutorials if request.method == 'POST': # create new tutorial form = TutorialForm(request.POST) if form.is_valid(): cleaned = form.cleaned_data if cleaned['content']: newcontent = cleaned['content'] else: newcontent = '' current_tutorial = Tutorial.objects.create(position=cleaned['position'], title=cleaned['title'], content=newcontent, active=cleaned['active']) tutorialhelpers.save_uploaded_video(request, current_tutorial, form) helpers.add_good_message( request, "You created a new tutorial. Good for you, cheeto-fingers.") else: print 'Some kind of problem with your form entry, try again.' form = TutorialForm() tutorials = Tutorial.objects.filter().order_by('position') return helpers.req_render_to_response( request, "admin/admin_tutorials.html", { 'form': form, 'tutorials': tutorials, }) else: # request.method == 'GET' form = TutorialForm() tutorials = Tutorial.objects.filter().order_by('position') return helpers.req_render_to_response( request, "admin/admin_tutorials.html", { 'form': form, 'tutorials': tutorials, })
def admin_press(request, enc, current_press_id): press_list = models.FlowgramPress.objects.filter().order_by('-link_date') fgpr_press_list = press_list.filter(press_category='R') featured_press_list = press_list.filter(press_category='F') more_press_list = press_list.filter(press_category='M') if request.method == 'GET': if not current_press_id == '': current_press_item = models.FlowgramPress.objects.get(id=current_press_id) fpf = forms.FlowgramPressForm(initial={ 'link_date': current_press_item.link_date, 'link_text': current_press_item.link_text, 'link_url': current_press_item.link_url, 'lede_text': current_press_item.lede_text, 'full_text': current_press_item.full_text, 'press_category': current_press_item.press_category, }) return helpers.req_render_to_response( request, 'admin/admin_press.html', { 'current_press_item': current_press_item, 'fpf': fpf, }) elif request.method == 'POST': form = request.POST['form'] f = forms.FlowgramPressForm(request.POST) if form == 'create_new': if f.is_valid(): cleaned = f.cleaned_data models.FlowgramPress.objects.create(link_date=cleaned['link_date'], link_text=cleaned['link_text'], link_url=cleaned['link_url'], lede_text=cleaned['lede_text'], full_text=cleaned['full_text'], press_category=cleaned['press_category']) helpers.add_good_message( request, "You created a new press item. Good for you, cheeto-fingers.") elif form == 'modify': current_press_item = models.FlowgramPress.objects.get(id=request.POST['press_id']) f = forms.FlowgramPressForm(request.POST) if f.is_valid(): cleaned = f.cleaned_data current_press_item.link_date = cleaned['link_date'] current_press_item.link_text = cleaned['link_text'] current_press_item.link_url = cleaned['link_url'] current_press_item.lede_text = cleaned['lede_text'] current_press_item.full_text = cleaned['full_text'] current_press_item.press_category = cleaned['press_category'] current_press_item.save() helpers.add_good_message( request, "You modified a press item. Good for you, cheeto-fingers.") fpf = forms.FlowgramPressForm() return helpers.req_render_to_response( request, 'admin/admin_press.html', { 'press_list': press_list, 'fgpr_press_list': fgpr_press_list, 'featured_press_list': featured_press_list, 'more_press_list': more_press_list, 'fpf': fpf, })
def admin_files(request, enc, file_to_delete): form = forms.AdminFilesForm() root_url = localsettings.INTERNAL_MEDIA_URL dirlist = os.listdir(localsettings.INTERNAL_MEDIA_ROOT) if request.method == 'POST': form = forms.AdminFilesForm(request.POST, request.FILES) if form.is_valid(): filename_prefix = form.cleaned_data['filename_prefix'] file = request.FILES['image'] content = file['content'] content_type = file['content-type'] file_extension = '' if content_type == 'image/png': file_extension = '.png' elif content_type == 'image/jpeg': file_extension = '.jpg' elif content_type == 'image/gif': file_extension = '.gif' filename = filename_prefix + '_' + str(int(time.mktime(datetime.datetime.now().timetuple()))) + file_extension f = open(localsettings.INTERNAL_MEDIA_ROOT + filename, 'wb') f.write(content) f.close() file_path = localsettings.INTERNAL_MEDIA_URL + filename helpers.add_good_message(request, "Image upload successful. Filepath is: %s" % file_path) else: helpers.add_bad_message(request, "There was a problem. sadtrombone.com") # refresh directory listing dirlist = os.listdir(localsettings.INTERNAL_MEDIA_ROOT) return helpers.req_render_to_response( request, "admin/admin_files.html", { 'form': form, 'dirlist': dirlist, 'root_url': root_url, }) # request.method == 'GET' counter = 0 if file_to_delete != '': # deleting file file_to_delete = int(file_to_delete) for filename in dirlist: if file_to_delete == counter: os.remove(localsettings.INTERNAL_MEDIA_ROOT + filename) # refresh directory listing dirlist = os.listdir(localsettings.INTERNAL_MEDIA_ROOT) helpers.add_good_message(request, "You deleted a file: %s" % filename) return HttpResponseRedirect('/adminfiles/') counter += 1 # refresh directory listing dirlist = os.listdir(localsettings.INTERNAL_MEDIA_ROOT) return helpers.req_render_to_response( request, "admin/admin_files.html", { 'form': form, 'dirlist': dirlist, 'root_url': root_url, })
def admin_polls(request, enc, current_poll_id): polls = models.FlowgramPoll.objects.filter().order_by('-date_created') if request.method == 'GET': if not current_poll_id == '': current_poll = models.FlowgramPoll.objects.get(id=current_poll_id) (fg1, fg2, fg3) = models.Flowgram.objects.filter(id__in=[current_poll.candidate_id_1, current_poll.candidate_id_2, current_poll.candidate_id_3]) return helpers.req_render_to_response( request, 'admin/admin_polls.html', { 'current_poll': current_poll, 'current_poll_id': current_poll_id, 'fg1': fg1, 'fg2': fg2, 'fg3': fg3, }) else: return helpers.req_render_to_response(request, 'admin/admin_polls.html', {'polls': polls}) elif request.method == 'POST': form = request.POST['form'] if form == 'create_new': poll_short_name = request.POST['poll_short_name'] poll_title = request.POST['poll_title'] candidate_id_1 = request.POST['candidate_id_1'] candidate_id_2 = request.POST['candidate_id_2'] candidate_id_3 = request.POST['candidate_id_3'] had_exception = False try: fg = models.Flowgram.objects.get(id=candidate_id_1) except models.Flowgram.DoesNotExist: had_exception = True models.add_bad_message( request, "Candidate ID 1 is invalid. Please try again, cheeto-breath.") try: fg = models.Flowgram.objects.get(id=candidate_id_2) except models.Flowgram.DoesNotExist: had_exception = True models.add_bad_message( request, "Candidate ID 2 is invalid. Please try again, cheeto-breath.") try: fg = models.Flowgram.objects.get(id=candidate_id_3) except models.Flowgram.DoesNotExist: had_exception = True models.add_bad_message( request, "Candidate ID 3 is invalid. Please try again, cheeto-breath.") if had_exception: return helpers.req_render_to_response( request, 'admin/admin_polls.html', { 'polls': polls, 'poll_short_name': poll_short_name, 'poll_title': poll_title, 'candidate_id_1': candidate_id_1, 'candidate_id_2': candidate_id_2, 'candidate_id_3': candidate_id_3, }) poll_active = request.POST['poll_active_hidden'].lower() == 'true' models.FlowgramPoll.objects.create(poll_short_name=poll_short_name, poll_title=poll_title, candidate_id_1=candidate_id_1, candidate_id_2=candidate_id_2, candidate_id_3=candidate_id_3, poll_active=poll_active) helpers.add_good_message(request, "You created a new poll called %s." % poll_title) return helpers.req_render_to_response(request, 'admin/admin_polls.html', {'polls': polls}) elif form == 'modify': current_poll = models.FlowgramPoll.objects.get(id=request.POST['poll_id']) current_poll.poll_short_name = request.POST['poll_short_name'] current_poll.poll_title = request.POST['poll_title'] current_poll.candidate_id_1 = request.POST['candidate_id_1'] current_poll.candidate_id_2 = request.POST['candidate_id_2'] current_poll.candidate_id_3 = request.POST['candidate_id_3'] current_poll_id = request.POST['current_poll_id'] had_exception = False try: fg = models.Flowgram.objects.get(id=current_poll.candidate_id_1) except models.Flowgram.DoesNotExist: had_exception = True models.add_bad_message(request, "Candidate ID 1 is invalid.") try: fg = models.Flowgram.objects.get(id=current_poll.candidate_id_2) except models.Flowgram.DoesNotExist: had_exception = True models.add_bad_message(request, "Candidate ID 2 is invalid.") try: fg = models.Flowgram.objects.get(id=current_poll.candidate_id_3) except models.Flowgram.DoesNotExist: had_exception = True models.add_bad_message(request, "Candidate ID 3 is invalid.") if had_exception: return HttpResponseRedirect('/adminpolls/%s' % current_poll_id) current_poll.poll_active = request.POST['poll_active_hidden'].lower() == 'true' current_poll.save() helpers.add_good_message(request, "You modified the poll called %s." % current_poll.poll_title) return helpers.req_render_to_response(request, 'admin/admin_polls.html', {'polls': polls})