def edit_post(request, pk): post = Post.objects.get(pk=pk) if post.approved or request.user != post.author: return redirect('/board/') tags = Tag.objects.all() role = get_role(request.user) posttags = post.tags.split(",") menu_type = 'general' form = PostForm(instance=post) if request.method == 'POST': form = PostForm(instance=post, data=request.POST, files=request.FILES) if form.is_valid(): post = form.save(commit=False) if role == "coordinator" or role == "admin": post.status = 1 else: post.status = 3 post.save() short_title = (post.title[:25] + '..') if len(post.title) > 25 else post.title details = (post.content[:100] + '..') if len(post.content) > 100 else post.content description = u'Board updated - {}'.format(short_title) notification = create_notification_from(post) notification.type = 4 notification.creator = request.user notification.description = description notification.details = details notification.save() coordinators_ids = (ConsultantProfile.objects.prefetch_related( 'consultant').get_coordinator_by_market( post.market).is_active().values_list('consultant__id', flat=True)) bulk_receiver_creation(notification, coordinators_ids) if role == "coordinator" or role == "admin": messages.success(request, 'Post successfully edited.') else: messages.success( request, 'Post successfully edited. An administrator will review it shortly.' ) return redirect('board:board') else: messages.error( request, 'There was a problem editing the post. Please correct the errors and try again.' ) edit = True return render(request, 'board/create.html', locals())
def notify_aps(request, registration_id): registration = Registration.objects.prefetch_related('costumer').filter(pk=registration_id).first() if not registration: return JsonResponse({'registration': 'not found'}, status=404) users_ids = get_user_model().objects.filter( programprofile__programs_base__in=registration.prospect_programs.values_list('program_base_id', flat=True) ).values_list('id', flat=True) description = u'New Lead' details = u'New Lead {}'.format( registration.costumer.get_full_name() ) if len(users_ids) > 0: notification = create_notification_from(registration) notification.creator = request.user notification.description = description notification.details = details notification.type = 7 notification.save() bulk_receiver_creation(notification, users_ids) return JsonResponse({}, status=200)
def payment_create(request): request_data = json.loads(request.body) registration_id = request_data.get('registration_id', '') statementApproved = request_data.get('statementApproved', '') registration = Registration.objects.filter(pk=registration_id).first() if not registration: return JsonResponse({}, status=400) payments_amount = RegistrationPayment.objects.filter( registration=registration).count() # if payments_amount > 7: # return JsonResponse({}, status=400) if statementApproved: payment = RegistrationPayment.objects.create(registration=registration, notification_send=True) student = registration.costumer notification = create_notification_from(registration) notification.type = 15 notification.creator = request.user notification.description = 'Payment applied to ' + student + ' account.' notification.details = 'Please verify the statement in the students profile.' notification.save() coordinators_ids = ( ConsultantProfile.objects.prefetch_related('consultant').filter( consultant=registration.consultant).is_active().values_list( 'consultant__id', flat=True)) bulk_receiver_creation(notification, coordinators_ids) for user_id in coordinators_ids: consultant = get_user_model().objects.filter(pk=user_id).get() mail_context = { 'common_name': consultant.person.first_name + ' ' + consultant.person.last_name, 'student': student } email_consultant = consultant.person.email subject = 'New notification in your ACS profile' send_mail_wrapper(subject, 'notifications/emails/new_notification.html', mail_context, [email_consultant]) else: payment = RegistrationPayment.objects.create(registration=registration) response = { 'id': payment.pk, 'deposit_amount': payment.deposit_amount, 'deposit_date': payment.deposit_date, 'details': payment.details, } return JsonResponse(response, status=200)
def create_post(request): role = get_role(request.user) tags = Tag.objects.all() form = PostForm() if request.method == 'POST': form = PostForm(data=request.POST, files=request.FILES) if form.is_valid(): market = get_user_market_or_main_market(request.user) post = form.save(commit=False) post.author = request.user post.market = market if role == "coordinator" or role == "admin": post.status = 1 post.save() short_title = (post.title[:25] + '..') if len(post.title) > 25 else post.title details = (post.content[:100] + '..') if len(post.content) > 100 else post.content description = u'New board created - {}'.format(short_title) notification = create_notification_from(post) notification.type = 3 notification.creator = request.user notification.description = description notification.details = details notification.save() coordinators_ids = ( ConsultantProfile.objects.prefetch_related('consultant'). get_coordinator_by_market(market).is_active().values_list( 'consultant__id', flat=True)) bulk_receiver_creation(notification, coordinators_ids) if role == "coordinator" or role == "admin": messages.success(request, 'Post successfully created.') else: messages.success( request, 'Post successfully created. An administrator will review it shortly.' ) return redirect('/board/') else: messages.error( request, 'There was a problem creating the post. Please correct the errors and try again.' ) if role == 'corporate': menu_type = 'general' return render(request, 'board/create.html', locals())
def new_visas_start(context): details = u"The visa process has been accepted for {} registration".format( context['registration'].costumer.get_full_name()) description = u"New visas process" notification = create_notification_from(context['registration']) notification.type = 6 notification.creator = context['creator'] notification.description = description notification.details = details notification.save() bulk_receiver_creation(notification, context['receiver_ids'])
def reimbursements_review(request): request_data = json.loads(request.body) pk = request_data.get('id', 0) reimbursement = Reimbursement.objects.prefetch_related('items').filter( pk=pk).first() if not reimbursement: return JsonResponse({}, status=400) items = reimbursement.items.all() validator = Validator() reimbursement_form = ReimbursementStrictForm({ 'account_id': reimbursement.account_id, 'location': reimbursement.location, 'spending_justification': reimbursement.spending_justification, 'date_from': reimbursement.date_from, 'date_to': reimbursement.date_to, }) if not reimbursement_form.is_valid(): validator.errors.errors = get_form_errors(reimbursement_form, {}) if len(items) == 0: validator.errors.add('no_field_errors', 'Must be at least one item') else: item_errors = [] for item in items: item_form = ItemStrictForm(item.as_dict()) errors = {'id': item.pk} if not item_form.is_valid(): errors = get_form_errors(item_form, errors) if not item.attachment: errors['file'] = ['File required.'] if len(errors) > 1: item_errors.append(errors) if len(item_errors) > 0: validator.errors.errors['items'] = item_errors if validator.has_errors(): return JsonResponse(validator.get_errors(), status=400) main_currency = Currency.objects.filter(main=True).first() for item in reimbursement.items.all(): if item.currency != main_currency: exchange = ExchangeRate.objects.filter( start_date__lte=item.date, end_date__gte=item.date, target=item.currency).first() exchange_rate = exchange.rate if exchange else Decimal('0.00') item.exchange_rate = exchange_rate item.total = (item.amount * exchange_rate).quantize( Decimal("0.01"), decimal.ROUND_HALF_DOWN) else: item.exchange_rate = Decimal('1.00') item.total = item.amount item.save() reimbursement.reject_reason = '' reimbursement.date_sent = dt.now() reimbursement.status = 3 reimbursement.save() notification = create_notification_from(reimbursement) notification.creator = request.user notification.description = u'New reimbursement to review' notification.details = u'New reimbursement from {}'.format( request.user.person.get_full_name()) notification.type = 12 notification.save() user_ids = get_user_model().objects.filter( Q(groups__name="corporate admin") | Q(groups__name="finances")).values_list('id', flat=True) bulk_receiver_creation(notification, user_ids) return JsonResponse({})
def notification_create(request): if request.method == 'POST': try: user_pk = request.POST.get('user_pk', None) market_id = request.POST.get('market_id', None) details = request.POST.get('details', None) description = request.POST.get('description', None) comment = request.POST.get('comment', None) kind = request.POST.get('kind', None) notification_type = int(request.POST.get('type', 0)) valid_types = [6, 7, 8, 10] if notification_type not in valid_types: return JsonResponse({'message': 'invalid type'}, status=400) user = request.user role = get_role(user) if role not in ['corporate', 'finance'] or (role in ['corporate', 'finance'] and kind == 'consultant'): receiver = get_user_model().objects.get(pk=user_pk) elif kind == 'market': market = Market.objects.get(pk=market_id, is_active=True) else: return JsonResponse({'message': 'invalid kind'}, status=400) if notification_type == 6: """ Direct notification """ details = "From {}".format(user.person.get_full_name()) user = get_user_model().objects.get(pk=user_pk) mail_context = { 'details': details, 'comment': comment } subject = 'New notification' send_mail_wrapper( subject, 'notifications/emails/new_notification_email.html', mail_context, [user.person.email]) elif notification_type == 7: post_id = request.POST['post_id'] post = Post.objects.get(pk=post_id) details = "From {} about the bulletin {} ".format( user.person.get_full_name(), post.title ) elif notification_type == 8: registration_id = request.POST['registration_id'] registration = Registration.objects.prefetch_related('costumer').get(pk=registration_id) details = "From {} about the {} registration notes".format( user.person.get_full_name(), registration.costumer.get_full_name() ) elif notification_type == 10: details = "From corporate" notification = Notification() notification.creator = request.user notification.type = notification_type notification.description = description notification.details = details notification.comment = comment notification.save() if role not in ['corporate', 'finance'] or (role in ['corporate', 'finance'] and kind == 'consultant'): notification_receiver = NotificationReceiver() notification_receiver.receiver = receiver notification_receiver.notification = notification notification_receiver.save() elif kind == 'market': groups = ['supervisors', 'free agents', 'consultants', 'new markets coordinator'] excluded_groups = Group.objects.exclude(name__in=groups) user_ids = get_user_model().objects.filter( groups__name__in=groups, is_active=True, consultantprofile__market=market ).exclude( groups__in=excluded_groups ).distinct().values_list('id', flat=True) bulk_receiver_creation(notification, user_ids) return JsonResponse({}, status=200) except get_user_model().DoesNotExist: return JsonResponse({'user': '******'}, status=400) except Exception as e: return JsonResponse({}, status=500)