Пример #1
0
def populate_random_guests():
    s = Site.objects.get_current()
    f = open('residents/test_names.txt')
    names = [n.strip().split() for n in f.readlines()]
    residents = Resident.on_site.all()[0:random.randint(5, 15)]
    today = timezone.now().date()
    num_added = 0
    for r in residents:
        for i in range(5):
            first, last = random.choice(names)
            name = "%s %s" % (first, last)
            MIT = (random.randint(0,3) == 0)
            username = last[0:8].lower() if MIT else ""
            g = GuestlistEntry(
                guest_of = r,
                for_dorm = s,
                name = name,
                is_mit_student = MIT,
                username = username,
            )
            if random.randint(0, 4) == 0:
                g.starts_on = today + timedelta(random.randint(1,10))
            if random.randint(0, 4) == 0:
                g.expires_on = today + timedelta(random.randint(-10,10))
            num_added += 1
            g.save()
    print "Added %d guest list entries" % num_added
Пример #2
0
def guestlist_add(request):
    if request.method == "POST":
        form = GuestlistEntryForm(request.POST)
        if form.is_valid():
            g = GuestlistEntry()
            g.guest_of = request.user.resident
            g.for_dorm = Site.objects.get_current()
            g.name = form.cleaned_data["name"]
            g.is_mit_student = form.cleaned_data["is_mit_student"]
            g.username = form.cleaned_data["username"]
            g.starts_on = form.cleaned_data["starts_on"]
            g.expires_on = form.cleaned_data["expires_on"]
            g.save()
            # TODO email the user about their package
            return HttpResponseRedirect(reverse(guestlist))
    else:
        form = GuestlistEntryForm(initial={"is_mit_student": True})

    payload = {"form": form, "prev_url": reverse(guestlist)}
    return render_to_response("guestlist/guestlistentry_form.html", payload, context_instance=RequestContext(request))