Exemplo n.º 1
0
def application_details(request, slug=None, cmb_id=None, imv_id=0, imv_guid=None, secret_hash="", membership_id=0, template_name="memberships/applications/details.html"):
    """
    Display a built membership application and handle submission.
    """
    if not slug: raise Http404
    user = request.user
    
    app = get_object_or_404(App, slug=slug)
    if not app.allow_view_by(user):
        raise Http403
    
    
    # if this app is for corporation individuals, redirect them to corp-pre page if
    # they have not passed the security check.
    is_corp_ind = False
    corporate_membership = None
    if hasattr(app, 'corp_app') and app.corp_app:
        if not cmb_id:
            # redirect them to the corp_pre page
            return redirect(reverse('membership.application_details_corp_pre', args=[app.slug]))
        
        is_corp_ind = True
        corporate_membership = get_object_or_404(CorporateMembership, id=cmb_id)
        # check if they have verified their email or entered the secret code
        is_verified = False
        if is_admin(request.user) or app.corp_app.authentication_method == 'admin':
            is_verified = True
        elif app.corp_app.authentication_method == 'email':
            try:
                indiv_veri = IndivMembEmailVeri8n.objects.get(pk=imv_id,
                                                              guid=imv_guid)
                if indiv_veri.verified:
                    is_verified = True
            except IndivMembEmailVeri8n.DoesNotExist:
                pass 
                                                              
        elif app.corp_app.authentication_method == 'secret_code':
            tmp_secret_hash = md5('%s%s' % (corporate_membership.secret_code, 
                                    request.session.get('corp_hash_random_string', ''))).hexdigest()
            if secret_hash == tmp_secret_hash:
                is_verified = True
                                        
        
        if not is_verified:
            return redirect(reverse('membership.application_details_corp_pre', args=[slug]))       

    # log application details view
    EventLog.objects.log(**{
        'event_id' : 655000,
        'event_data': '%s (%d) viewed by %s' % (app._meta.object_name, app.pk, user),
        'description': '%s viewed' % app._meta.object_name,
        'user': user,
        'request': request,
        'instance': app,
    })
        

    initial_dict = {}
    if hasattr(user, 'memberships'):
        membership = user.memberships.get_membership()
        is_only_a_member = [
            is_developer(user) == False,
            is_admin(user) == False,
            is_member(user) == True,
        ]
        
        if corporate_membership:
            # exclude corp. reps, creator and owner - they should be able to add new
            is_only_a_member.append(corporate_membership.allow_edit_by(user)==False)

        # deny access to renew memberships
        if all(is_only_a_member):
            initial_dict = membership.get_app_initial(app)
            if not membership.can_renew():
                return render_to_response("memberships/applications/no-renew.html", {
                    "app": app, "user":user, "membership": membership}, 
                    context_instance=RequestContext(request))

    pending_entries = []

    if hasattr(user, 'appentry_set'):
        pending_entries = user.appentry_set.filter(
            is_approved__isnull = True,  # pending   
        )

        # if an application entry was submitted
        # after your current membership was created
        if user.memberships.get_membership():
            pending_entries.filter(
                entry_time__gte = user.memberships.get_membership().subscribe_dt
            )

    app_entry_form = AppEntryForm(
            app, 
            request.POST or None, 
            request.FILES or None, 
            user=user, 
            corporate_membership=corporate_membership,
            initial=initial_dict,
        )

    if request.method == "POST":
        if app_entry_form.is_valid():

            entry = app_entry_form.save(commit=False)
            entry_invoice = entry.save_invoice()

            if user.is_authenticated():
                entry.user = user
                entry.is_renewal = all(is_only_a_member)

            # add all permissions and save the model
            entry = update_perms_and_save(request, app_entry_form, entry)

            # administrators go to approve/disapprove page
            if is_admin(user):
                return redirect(reverse('membership.application_entries', args=[entry.pk]))

            # send "joined" notification
            Notice.send_notice(
                entry=entry,
                request = request,
                emails=entry.email,
                notice_type='join',
                membership_type=entry.membership_type,
            )
            
            if entry_invoice.total == 0:
                if not entry_invoice.is_tendered:
                    entry_invoice.tender(request.user) 

            # online payment
            if entry_invoice.total>0 and entry.payment_method and entry.payment_method.is_online:

                return HttpResponseRedirect(reverse(
                    'payments.views.pay_online',
                    args=[entry_invoice.pk, entry_invoice.guid]
                ))

            if not entry.approval_required():

                entry.approve()

                # silence old memberships within renewal period
                Membership.objects.silence_old_memberships(entry.user)

                # get user from the membership since it's null in the entry
                entry.user = entry.membership.user

                membership_total = Membership.objects.filter(status=True, status_detail='active').count()
    
                # send "approved" notification
                Notice.send_notice(
                    request = request,
                    emails=entry.email,
                    notice_type='approve',
                    membership=entry.membership,
                    membership_type=entry.membership_type,
                )
    
                if not user.is_authenticated():
                    from django.core.mail import send_mail
                    from django.utils.http import int_to_base36
                    from django.contrib.auth.tokens import default_token_generator
                    from site_settings.utils import get_setting
                    token_generator = default_token_generator
    
                    site_url = get_setting('site', 'global', 'siteurl')
                    site_name = get_setting('site', 'global', 'sitedisplayname')
    
                    # send new user account welcome email (notification)
                    notification.send_emails([entry.user.email],'user_welcome', {
                        'site_url': site_url,
                        'site_name': site_name,
                        'uid': int_to_base36(entry.user.id),
                        'user': entry.user,
                        'username': entry.user.username,
                        'token': token_generator.make_token(entry.user),
                    })
    
                # log - entry approval
                EventLog.objects.log(**{
                    'event_id' : 1082101,
                    'event_data': '%s (%d) approved by %s' % (entry._meta.object_name, entry.pk, entry.judge),
                    'description': '%s viewed' % entry._meta.object_name,
                    'user': user,
                    'request': request,
                    'instance': entry,
                })

            # log - entry submission
            EventLog.objects.log(**{
                'event_id' : 1081000,
                'event_data': '%s (%d) submitted by %s' % (entry._meta.object_name, entry.pk, request.user),
                'description': '%s viewed' % entry._meta.object_name,
                'user': user,
                'request': request,
                'instance': entry,
            })

            return redirect(entry.confirmation_url)

    return render_to_response(template_name, {
            'app': app, 
            'app_entry_form': app_entry_form, 
            'pending_entries': pending_entries,
            }, context_instance=RequestContext(request))
Exemplo n.º 2
0
def application_entries(request, id=None, template_name="memberships/entries/details.html"):
    """
    Displays the details of a membership application entry.
    """

    if not id:
        return redirect(reverse('membership.application_entries_search'))
    
    entry = get_object_or_404(AppEntry, id=id)
    if not entry.allow_view_by(request.user):
        raise Http403

    # log entry view
    EventLog.objects.log(**{
        'event_id' : 1085000,
        'event_data': '%s (%d) viewed by %s' % (entry._meta.object_name, entry.pk, request.user),
        'description': '%s viewed' % entry._meta.object_name,
        'user': request.user,
        'request': request,
        'instance': entry,
    })

    if request.method == "POST":
        form = MemberApproveForm(entry, request.POST)
        if form.is_valid():

            membership_total = Membership.objects.filter(status=True, status_detail='active').count()

            status = request.POST.get('status', '')
            approve = (status.lower() == 'approve') or (status.lower() == 'approve renewal')

            entry.judge = request.user

            if approve:

                user_pk = int(form.cleaned_data['users'])
                if user_pk:
                    entry.user = User.objects.get(pk=user_pk)
                else:
                    entry.user = User.objects.create_user(**{
                        'username': entry.spawn_username(entry.first_name, entry.last_name),
                        'email': entry.email,
                        'password': hashlib.sha1(entry.email).hexdigest()[:6]
                    })

                    from django.core.mail import send_mail
                    from django.utils.http import int_to_base36
                    from django.contrib.auth.tokens import default_token_generator
                    from site_settings.utils import get_setting
                    token_generator = default_token_generator

                    site_url = get_setting('site', 'global', 'siteurl')
                    site_name = get_setting('site', 'global', 'sitedisplayname')

                    # send new user account welcome email (notification)
                    notification.send_emails([entry.user.email],'user_welcome', {
                        'site_url': site_url,
                        'site_name': site_name,
                        'uid': int_to_base36(entry.user.id),
                        'user': entry.user,
                        'username': entry.user.username,
                        'token': token_generator.make_token(entry.user),
                    })

                # update application, user, 
                # group, membership, and archive
                entry.approve()

                # silence old memberships within renewal period
                Membership.objects.silence_old_memberships(entry.user)

                # execute field functions (group subscriptions)
                entry.execute_field_functions()

                # send "approved" notification
                Notice.send_notice(
                    request=request,
                    entry=entry,
                    emails=entry.email,
                    notice_type='approve',
                    membership=entry.membership,
                    membership_type=entry.membership_type,
                )

                # log entry approved
                EventLog.objects.log(**{
                    'event_id' : 1085000,
                    'event_data': '%s (%d) approved by %s' % (entry._meta.object_name, entry.pk, request.user),
                    'description': '%s approved' % entry._meta.object_name,
                    'user': request.user,
                    'request': request,
                    'instance': entry,
                })

            else:  # if not approved
                entry.disapprove()

                # send "disapproved" notification
                Notice.send_notice(
                    entry = entry,
                    request = request,
                    emails=entry.email,
                    notice_type='disapprove',
                    membership_type=entry.membership_type,
                )

                # log entry disapproved
                EventLog.objects.log(**{
                    'event_id' : 1082102,
                    'event_data': '%s (%d) disapproved by %s' % (entry._meta.object_name, entry.pk, request.user),
                    'description': '%s disapproved' % entry._meta.object_name,
                    'user': request.user,
                    'request': request,
                    'instance': entry,
                })

            return redirect(reverse('membership.application_entries', args=[entry.pk]))

    else:  # if request != POST
        form = MemberApproveForm(entry)

    return render_to_response(template_name, {
        'entry': entry,
        'form': form,
        }, context_instance=RequestContext(request))