예제 #1
0
def add(request):

    if not require_login_or_key(request):
        return redirect('/')

    ssn = request.GET.get('ssn')
    username = request.GET.get('username')
    name = request.GET.get('name')
    email = request.GET.get('email')
    added = request.GET.get('added', '')
    groups = request.GET.getlist('group', [])

    member = Member()
    member.ssn = ssn
    member.username = username
    member.name = name
    member.email = email
    member.added = added

    try:
        member.save()
        member = Member.objects.get(id=member.id)
        for group in groups:
            member.groups.add(Group.objects.get(techname=group))        
    except IntegrityError as e:
        return json_error(e)

    response_data = {
        'success': True,
        'data': member_to_dict(member)
    }

    return HttpResponse(json.dumps(response_data), content_type='application/json')
    def register_member(self, reg):
        random_string = generate_random_string()
        while Member.objects.filter(temporary_web_id=random_string).count() > 0:
            # Preventing duplication errors
            random_string = generate_random_string()

        # Make the 'added' date timezone-aware.
        added_date = datetime.strptime(reg['date'], '%Y-%m-%d %H:%M:%S')

        # Create and configure member
        member = Member()
        member.added = pytz.timezone(settings.TIME_ZONE).localize(added_date)
        member.ssn = reg['ssn']
        member.name = reg['name']
        member.email = reg['email']
        merge_national_registry_info(member, reg['national'], timezone.now())
        member.temporary_web_id = random_string
        member.temporary_web_id_timing = timezone.now()

        # Save member to database
        stdout.write('* Registering member...')
        stdout.flush()
        member.save()
        stdout.write(' done\n')

        self.process_groups(reg, member)

        # Send confirmation message
        message = InteractiveMessage.objects.get(interactive_type='registration_received', active=True)
        body = message.produce_links(member.temporary_web_id)

        # Save the start of the delivery attempt
        delivery = InteractiveMessageDelivery()
        delivery.interactive_message = message
        delivery.member = member
        delivery.email = member.email
        delivery.timing_start = timezone.now()
        delivery.save()

        # Actually send the message
        stdout.write('* Sending confirmation email...')
        stdout.flush()
        quick_mail(member.email, message.subject, body)
        stdout.write(' done\n')

        # Update the delivery
        delivery.timing_end = timezone.now()
        delivery.save()