def _event_process(request, form, event): """Generate and clean associated event data for an event request or event edit: timezone application, approvals update and notifications, creator and modifier.""" if not event.creator: event.creator = request.user event.modified_user = request.user tz = pytz.timezone(request.POST['timezone']) event.start_time = tz_apply(event.start_time, tz) if event.archive_time: event.archive_time = tz_apply(event.archive_time, tz) if 'approvals' in form.cleaned_data: event.save() approvals_old = [app.group for app in event.approval_set.all()] approvals_new = form.cleaned_data['approvals'] approvals_add = set(approvals_new).difference(approvals_old) approvals_remove = set(approvals_old).difference(approvals_new) for approval in approvals_add: group = Group.objects.get(name=approval) app = Approval(group=group, event=event) app.save() emails = [u.email for u in group.user_set.all()] subject = ('[Air Mozilla] Approval requested: "%s"' % event.title) message = render_to_string( 'manage/_email_approval.html', { 'group': group.name, 'manage_url': request.build_absolute_uri(reverse('manage:approvals')), 'title': event.title, 'creator': event.creator.email, 'datetime': event.start_time, 'description': event.description }) email = EmailMessage(subject, message, settings.EMAIL_FROM_ADDRESS, emails) email.send() for approval in approvals_remove: app = Approval.objects.get(group=approval, event=event) app.delete()
def _event_process(request, form, event): """Generate and clean associated event data for an event request or event edit: timezone application, approvals update and notifications, creator and modifier.""" if not event.creator: event.creator = request.user event.modified_user = request.user if event.location: tz = pytz.timezone(event.location.timezone) event.start_time = tz_apply(event.start_time, tz) if 'approvals' in form.cleaned_data: event.save() approvals_old = [app.group for app in event.approval_set.all()] approvals_new = form.cleaned_data['approvals'] approvals_add = set(approvals_new).difference(approvals_old) for approval in approvals_add: group = Group.objects.get(name=approval) app = Approval.objects.create(group=group, event=event) sending.email_about_approval_requested( event, group, request ) # Note! we currently do not allow approvals # to be "un-requested". That's because the email has already # gone out and it's too late now. if 'curated_groups' in form.cleaned_data: # because this form field acts like "tags", # we split them by , names = [ x.strip() for x in form.cleaned_data['curated_groups'].split(',') if x.strip() ] if names: all = mozillians.get_all_groups_cached() for name in names: group, __ = CuratedGroup.objects.get_or_create( event=event, name=name ) found = [x for x in all if x['name'] == name] if found and found[0]['url'] != group.url: group.url = found[0]['url'] group.save() # delete any we had before that aren't submitted any more ( CuratedGroup.objects .filter(event=event) .exclude(name__in=names) .delete() )
def _event_process(request, form, event): """Generate and clean associated event data for an event request or event edit: timezone application, approvals update and notifications, creator and modifier.""" if not event.creator: event.creator = request.user event.modified_user = request.user tz = pytz.timezone(request.POST['timezone']) event.start_time = tz_apply(event.start_time, tz) if event.archive_time: event.archive_time = tz_apply(event.archive_time, tz) if 'approvals' in form.cleaned_data: event.save() approvals_old = [app.group for app in event.approval_set.all()] approvals_new = form.cleaned_data['approvals'] approvals_add = set(approvals_new).difference(approvals_old) approvals_remove = set(approvals_old).difference(approvals_new) for approval in approvals_add: group = Group.objects.get(name=approval) app = Approval(group=group, event=event) app.save() emails = [u.email for u in group.user_set.all()] subject = ('[Air Mozilla] Approval requested: "%s"' % event.title) message = render_to_string( 'manage/_email_approval.html', { 'group': group.name, 'manage_url': request.build_absolute_uri( reverse('manage:approvals') ), 'title': event.title, 'creator': event.creator.email, 'datetime': event.start_time, 'description': event.description } ) email = EmailMessage(subject, message, settings.EMAIL_FROM_ADDRESS, emails) email.send() for approval in approvals_remove: app = Approval.objects.get(group=approval, event=event) app.delete()
def new_event_tweet(request, id): data = {} event = get_object_or_404(Event, id=id) if request.method == 'POST': form = forms.EventTweetForm(event, data=request.POST) if form.is_valid(): event_tweet = form.save(commit=False) if event_tweet.send_date: assert event.location, "event must have a location" tz = pytz.timezone(event.location.timezone) event_tweet.send_date = tz_apply(event_tweet.send_date, tz) else: now = timezone.now() event_tweet.send_date = now event_tweet.event = event event_tweet.creator = request.user event_tweet.save() messages.info(request, 'Tweet saved') url = reverse('manage:event_edit', args=(event.pk, )) return redirect(url) else: initial = {} event_url = reverse('main:event', args=(event.slug, )) base_url = ('%s://%s' % (request.is_secure() and 'https' or 'http', RequestSite(request).domain)) abs_url = urlparse.urljoin(base_url, event_url) try: abs_url = shorten_url(abs_url) data['shortener_error'] = None except (ImproperlyConfigured, ValueError) as err: data['shortener_error'] = str(err) # except OtherHttpRelatedErrors? # data['shortener_error'] = "Network error trying to shorten URL" initial['text'] = unhtml('%s\n%s' % (short_desc(event), abs_url)) initial['include_placeholder'] = bool(event.placeholder_img) initial['send_date'] = '' form = forms.EventTweetForm(initial=initial, event=event) data['event'] = event data['form'] = form data['tweets'] = EventTweet.objects.filter(event=event) return render(request, 'manage/new_event_tweet.html', data)
def new_event_tweet(request, id): data = {} event = get_object_or_404(Event, id=id) if request.method == 'POST': form = forms.EventTweetForm(event, data=request.POST) if form.is_valid(): event_tweet = form.save(commit=False) if event_tweet.send_date: assert event.location, "event must have a location" tz = pytz.timezone(event.location.timezone) event_tweet.send_date = tz_apply(event_tweet.send_date, tz) else: now = timezone.now() event_tweet.send_date = now event_tweet.event = event event_tweet.creator = request.user event_tweet.save() messages.info(request, 'Tweet saved') url = reverse('manage:event_edit', args=(event.pk,)) return redirect(url) else: initial = {} event_url = reverse('main:event', args=(event.slug,)) base_url = get_base_url(request) abs_url = urlparse.urljoin(base_url, event_url) try: abs_url = shorten_url(abs_url) data['shortener_error'] = None except (ImproperlyConfigured, ValueError) as err: data['shortener_error'] = str(err) # except OtherHttpRelatedErrors? # data['shortener_error'] = "Network error trying to shorten URL" initial['text'] = unhtml('%s\n%s' % (short_desc(event), abs_url)) initial['include_placeholder'] = bool(event.placeholder_img) initial['send_date'] = '' form = forms.EventTweetForm(initial=initial, event=event) data['event'] = event data['form'] = form data['tweets'] = EventTweet.objects.filter(event=event) return render(request, 'manage/new_event_tweet.html', data)
def details(request, id): event = get_object_or_404(SuggestedEvent, pk=id) if event.user != request.user: return http.HttpResponseBadRequest('Not your event') if request.method == 'POST': form = forms.DetailsForm(request.POST, instance=event) if form.is_valid(): event = form.save() # the start_time comes to us as a string, e.g. '2014-01-01 # 12:00:00' and that'll be converted into '2014-01-01 # 12:00:00 tzinfo=UTC' automatically. But that's not what we want # so we change it first. event.start_time = tz_apply( event.start_time, pytz.timezone(event.location.timezone) ) event.save() url = reverse('suggest:placeholder', args=(event.pk,)) return redirect(url) else: if event.location and event.start_time: # Because the modelform is going present our user # without input widgets' that are datetimes in # naive format, when it does this is does so using the # settings.TIME_ZONE and when saved it applies the # settings.TIME_ZONE back again. # Normally in Django templates, this is solved with # {% timezone "Europe/Paris" %} # {{ form.as_p }} # {% endtimezone %} # But that's not going to work when working with jinja # so we do it manually from the view code. event.start_time = make_naive( event.start_time, pytz.timezone(event.location.timezone) ) form = forms.DetailsForm(instance=event) data = {'form': form, 'event': event} return render(request, 'suggest/details.html', data)
def details(request, id): event = get_object_or_404(SuggestedEvent, pk=id) if event.user != request.user: return http.HttpResponseBadRequest('Not your event') if request.method == 'POST': form = forms.DetailsForm(request.POST, instance=event) if form.is_valid(): event = form.save() # the start_time comes to us as a string, e.g. '2014-01-01 # 12:00:00' and that'll be converted into '2014-01-01 # 12:00:00 tzinfo=UTC' automatically. But that's not what we want # so we change it first. event.start_time = tz_apply(event.start_time, pytz.timezone(event.location.timezone)) event.save() url = reverse('suggest:placeholder', args=(event.pk, )) return redirect(url) else: if event.location and event.start_time: # Because the modelform is going present our user # without input widgets' that are datetimes in # naive format, when it does this is does so using the # settings.TIME_ZONE and when saved it applies the # settings.TIME_ZONE back again. # Normally in Django templates, this is solved with # {% timezone "Europe/Paris" %} # {{ form.as_p }} # {% endtimezone %} # But that's not going to work when working with jinja # so we do it manually from the view code. event.start_time = make_naive( event.start_time, pytz.timezone(event.location.timezone)) form = forms.DetailsForm(instance=event) data = {'form': form, 'event': event} return render(request, 'suggest/details.html', data)
def details(request, id): event = get_object_or_404(SuggestedEvent, pk=id) if event.user != request.user: return http.HttpResponseBadRequest('Not your event') try: discussion = SuggestedDiscussion.objects.get(event=event) except SuggestedDiscussion.DoesNotExist: discussion = None if request.method == 'POST': form = forms.DetailsForm(request.POST, instance=event) if form.is_valid(): event = form.save() # the start_time comes to us as a string, e.g. '2014-01-01 # 12:00:00' and that'll be converted into '2014-01-01 # 12:00:00 tzinfo=UTC' automatically. But that's not what we want # so we change it first. event.start_time = tz_apply(event.start_time, pytz.timezone(event.location.timezone)) event.save() next_url = reverse('suggest:placeholder', args=(event.pk, )) if form.cleaned_data['enable_discussion']: if discussion: # make sure it's enabled discussion.enabled = True # discussion.moderate_all = ( # event.privacy != Event.PRIVACY_COMPANY # ) discussion.save() else: discussion = SuggestedDiscussion.objects.create( event=event, enabled=True, notify_all=True, # moderate_all=event.privacy != Event.PRIVACY_COMPANY ) if request.user not in discussion.moderators.all(): discussion.moderators.add(request.user) next_url = reverse('suggest:discussion', args=(event.pk, )) elif SuggestedDiscussion.objects.filter(event=event): discussion = SuggestedDiscussion.objects.get(event=event) discussion.enabled = False discussion.save() return redirect(next_url) else: if event.location and event.start_time: # Because the modelform is going present our user # without input widgets' that are datetimes in # naive format, when it does this is does so using the # settings.TIME_ZONE and when saved it applies the # settings.TIME_ZONE back again. # Normally in Django templates, this is solved with # {% timezone "Europe/Paris" %} # {{ form.as_p }} # {% endtimezone %} # But that's not going to work when working with jinja # so we do it manually from the view code. event.start_time = make_naive( event.start_time, pytz.timezone(event.location.timezone)) initial = {'enable_discussion': not (event and not discussion)} form = forms.DetailsForm(instance=event, initial=initial) data = {'form': form, 'event': event} return render(request, 'suggest/details.html', data)
def details(request, id): event = get_object_or_404(SuggestedEvent, pk=id) if event.user != request.user: return http.HttpResponseBadRequest('Not your event') try: discussion = SuggestedDiscussion.objects.get(event=event) except SuggestedDiscussion.DoesNotExist: discussion = None if request.method == 'POST': form = forms.DetailsForm(request.POST, instance=event) if form.is_valid(): event = form.save() # the start_time comes to us as a string, e.g. '2014-01-01 # 12:00:00' and that'll be converted into '2014-01-01 # 12:00:00 tzinfo=UTC' automatically. But that's not what we want # so we change it first. event.start_time = tz_apply( event.start_time, pytz.timezone(event.location.timezone) ) event.save() next_url = reverse('suggest:placeholder', args=(event.pk,)) if form.cleaned_data['enable_discussion']: if discussion: # make sure it's enabled discussion.enabled = True discussion.moderate_all = ( event.privacy != Event.PRIVACY_COMPANY ) discussion.save() else: discussion = SuggestedDiscussion.objects.create( event=event, enabled=True, notify_all=True, moderate_all=event.privacy != Event.PRIVACY_COMPANY ) if request.user not in discussion.moderators.all(): discussion.moderators.add(request.user) next_url = reverse('suggest:discussion', args=(event.pk,)) elif SuggestedDiscussion.objects.filter(event=event): discussion = SuggestedDiscussion.objects.get(event=event) discussion.enabled = False discussion.save() return redirect(next_url) else: if event.location and event.start_time: # Because the modelform is going present our user # without input widgets' that are datetimes in # naive format, when it does this is does so using the # settings.TIME_ZONE and when saved it applies the # settings.TIME_ZONE back again. # Normally in Django templates, this is solved with # {% timezone "Europe/Paris" %} # {{ form.as_p }} # {% endtimezone %} # But that's not going to work when working with jinja # so we do it manually from the view code. event.start_time = make_naive( event.start_time, pytz.timezone(event.location.timezone) ) initial = {'enable_discussion': not (event and not discussion)} form = forms.DetailsForm(instance=event, initial=initial) data = {'form': form, 'event': event} return render(request, 'suggest/details.html', data)