def event_create(request): if request.method == 'GET': context = {} context["form"] = EventForm(request_user=request.user) if request.user.role == 'ADMIN' or request.user.is_superuser: context['teams'] = Teams.objects.all() return render(request, 'event_create.html', context) if request.method == 'POST': form = EventForm(request.POST, request_user=request.user) if form.is_valid(): start_date = form.cleaned_data.get('start_date') end_date = form.cleaned_data.get('end_date') # recurring_days recurring_days = request.POST.getlist('recurring_days') if form.cleaned_data.get('event_type') == 'Non-Recurring': event = form.save(commit=False) event.date_of_meeting = start_date event.created_by = request.user event.save() form.save_m2m() if request.POST.getlist('teams', []): user_ids = Teams.objects.filter( id__in=request.POST.getlist('teams')).values_list( 'users', flat=True) assinged_to_users_ids = event.assigned_to.all( ).values_list('id', flat=True) for user_id in user_ids: if user_id not in assinged_to_users_ids: event.assigned_to.add(user_id) if request.POST.getlist('teams', []): event.teams.add(*request.POST.getlist('teams')) assigned_to_list = list(event.assigned_to.all().values_list( 'id', flat=True)) send_email.delay(event.id, assigned_to_list, domain=request.get_host(), protocol=request.scheme) if form.cleaned_data.get('event_type') == 'Recurring': delta = end_date - start_date all_dates = [] required_dates = [] for day in range(delta.days + 1): each_date = (start_date + timedelta(days=day)) if each_date.strftime("%A") in recurring_days: required_dates.append(each_date) for each in required_dates: each = datetime.strptime(str(each), '%Y-%m-%d').date() data = form.cleaned_data event = Event.objects.create( created_by=request.user, start_date=start_date, end_date=end_date, name=data['name'], event_type=data['event_type'], description=data['description'], start_time=data['start_time'], end_time=data['end_time'], date_of_meeting=each) event.contacts.add(*request.POST.getlist('contacts')) event.assigned_to.add(*request.POST.getlist('assigned_to')) if request.POST.getlist('teams', []): user_ids = Teams.objects.filter( id__in=request.POST.getlist('teams')).values_list( 'users', flat=True) assinged_to_users_ids = event.assigned_to.all( ).values_list('id', flat=True) for user_id in user_ids: if user_id not in assinged_to_users_ids: event.assigned_to.add(user_id) if request.POST.getlist('teams', []): event.teams.add(*request.POST.getlist('teams')) assigned_to_list = list( event.assigned_to.all().values_list('id', flat=True)) send_email.delay(event.id, assigned_to_list, domain=request.get_host(), protocol=request.scheme) return JsonResponse({ 'error': False, 'success_url': reverse('events:events_list') }) else: return JsonResponse({ 'error': True, 'errors': form.errors, })
def event_create(request): if request.method == "GET": context = {} context["form"] = EventForm(request_user=request.user, request_obj=request) context["users"] = User.objects.filter(is_active=True, company=request.company) if request.user.role == "ADMIN" or request.user.is_superuser: context["teams"] = Teams.objects.filter(company=request.company) return render(request, "event_create.html", context) if request.method == "POST": form = EventForm(request.POST, request_user=request.user, request_obj=request) if form.is_valid(): start_date = form.cleaned_data.get("start_date") end_date = form.cleaned_data.get("end_date") # recurring_days recurring_days = request.POST.getlist("recurring_days") if form.cleaned_data.get("event_type") == "Non-Recurring": event = form.save(commit=False) event.date_of_meeting = start_date event.created_by = request.user event.company = request.company event.save() form.save_m2m() if request.POST.getlist("teams", []): user_ids = Teams.objects.filter( id__in=request.POST.getlist("teams") ).values_list("users", flat=True) assinged_to_users_ids = event.assigned_to.all().values_list( "id", flat=True ) for user_id in user_ids: if user_id not in assinged_to_users_ids: event.assigned_to.add(user_id) if request.POST.getlist("teams", []): event.teams.add(*request.POST.getlist("teams")) assigned_to_list = list( event.assigned_to.all().values_list("id", flat=True) ) send_email.delay( event.id, assigned_to_list, domain=request.get_host(), protocol=request.scheme, ) if form.cleaned_data.get("event_type") == "Recurring": delta = end_date - start_date all_dates = [] required_dates = [] for day in range(delta.days + 1): each_date = start_date + timedelta(days=day) if each_date.strftime("%A") in recurring_days: required_dates.append(each_date) for each in required_dates: each = datetime.strptime(str(each), "%Y-%m-%d").date() data = form.cleaned_data event = Event.objects.create( created_by=request.user, start_date=start_date, end_date=end_date, name=data["name"], event_type=data["event_type"], description=data["description"], start_time=data["start_time"], end_time=data["end_time"], date_of_meeting=each, ) event.contacts.add(*request.POST.getlist("contacts")) event.assigned_to.add(*request.POST.getlist("assigned_to")) if request.POST.getlist("teams", []): user_ids = Teams.objects.filter( id__in=request.POST.getlist("teams") ).values_list("users", flat=True) assinged_to_users_ids = event.assigned_to.all().values_list( "id", flat=True ) for user_id in user_ids: if user_id not in assinged_to_users_ids: event.assigned_to.add(user_id) if request.POST.getlist("teams", []): event.teams.add(*request.POST.getlist("teams")) assigned_to_list = list( event.assigned_to.all().values_list("id", flat=True) ) send_email.delay( event.id, assigned_to_list, domain=request.get_host(), protocol=request.scheme, ) return JsonResponse( {"error": False, "success_url": reverse("events:events_list")} ) else: return JsonResponse( { "error": True, "errors": form.errors, } )
def event_update(request, event_id): event_obj = get_object_or_404(Event, pk=event_id) if not (request.user.role == 'ADMIN' or request.user.is_superuser or event_obj.created_by == request.user or request.user in event_obj.assigned_to.all()): raise PermissionDenied if request.method == 'GET': context = {} context["event_obj"] = event_obj context["form"] = EventForm(instance=event_obj, request_user=request.user) selected_recurring_days = Event.objects.filter( name=event_obj.name).values_list('date_of_meeting', flat=True) selected_recurring_days = [ day.strftime('%A') for day in selected_recurring_days ] context['selected_recurring_days'] = selected_recurring_days if request.user.role == 'ADMIN' or request.user.is_superuser: context['teams'] = Teams.objects.all() return render(request, 'event_create.html', context) if request.method == 'POST': form = EventForm(request.POST, instance=event_obj, request_user=request.user) if form.is_valid(): start_date = form.cleaned_data.get('start_date') end_date = form.cleaned_data.get('end_date') previous_assigned_to_users = list( event_obj.assigned_to.all().values_list('id', flat=True)) # recurring_days # recurring_days = request.POST.getlist('days') if form.data.get('event_type') == 'Non-Recurring': event = form.save(commit=False) event.date_of_meeting = start_date # event.created_by = request.user event.save() form.save_m2m() if request.POST.getlist('teams', []): user_ids = Teams.objects.filter( id__in=request.POST.getlist('teams')).values_list( 'users', flat=True) assinged_to_users_ids = event.assigned_to.all( ).values_list('id', flat=True) for user_id in user_ids: if user_id not in assinged_to_users_ids: event.assigned_to.add(user_id) if request.POST.getlist('teams', []): event.teams.clear() event.teams.add(*request.POST.getlist('teams')) else: event.teams.clear() assigned_to_list = list(event.assigned_to.all().values_list( 'id', flat=True)) recipients = list( set(assigned_to_list) - set(previous_assigned_to_users)) send_email.delay(event.id, recipients, domain=request.get_host(), protocol=request.scheme) if form.data.get('event_type') == 'Recurring': event = form.save(commit=False) event.save() form.save_m2m() if request.POST.getlist('teams', []): user_ids = Teams.objects.filter( id__in=request.POST.getlist('teams')).values_list( 'users', flat=True) assinged_to_users_ids = event.assigned_to.all( ).values_list('id', flat=True) for user_id in user_ids: if user_id not in assinged_to_users_ids: event.assigned_to.add(user_id) if request.POST.getlist('teams', []): event.teams.clear() event.teams.add(*request.POST.getlist('teams')) else: event.teams.clear() assigned_to_list = list(event.assigned_to.all().values_list( 'id', flat=True)) recipients = list( set(assigned_to_list) - set(previous_assigned_to_users)) send_email.delay(event.id, recipients, domain=request.get_host(), protocol=request.scheme) # event.contacts.add(*request.POST.getlist('contacts')) # event.assigned_to.add(*request.POST.getlist('assigned_to')) return JsonResponse({ 'error': False, 'success_url': reverse('events:events_list') }) else: return JsonResponse({ 'error': True, 'errors': form.errors, })