Esempio n. 1
0
def wordlist_list(request):
    if "wordlist_id" in request.POST:
        # populate or create WordismsUser
        try:
            wordisms_user = WordismsUser.objects.get(user=request.user)
        except WordismsUser.DoesNotExist:
            wordisms_user = WordismsUser(user=request.user)
            wordisms_user.save()

        try:
            #TODO make sure you can't subscribe to the same word list twice
            wordlist_id = request.POST['wordlist_id']
            wordlist = Wordlist.objects.get(id=wordlist_id)
            subscription = Subscription(wordisms_user=wordisms_user,wordlist=wordlist)
            subscription.save()
            request.user.message_set.create(message=("Subscribed to '%s'" % wordlist.name))
            return HttpResponseRedirect("/account")
        except Wordlist.DoesNotExist:
            request.user.message_set.create(message=("Internal error, could not find wordlist id %s" % wordlist_id))
            return HttpResponseRedirect("/wordlist/list")

    return render_to_response(  'wordlist_list.html', 
                              { 'wordlists': Wordlist.objects.order_by('name') },
                              context_instance=RequestContext(request) )
Esempio n. 2
0
def account(request):
    try:
        wordisms_user = WordismsUser.objects.get(user=request.user)
    except:
        request.user.message_set.create(message="You have no interfaces linked to your account.")
        return render_to_response('account/index.html', {}, context_instance=RequestContext(request))

    if 'contact_id' in request.POST:
        #TODO make sure this will only delete contact IDs for the current account
        try:
            contact = Contact.objects.get(id=request.POST['contact_id'])

            if contact.wordisms_user != wordisms_user:
                raise Exception
            contact.wordisms_user = None
            contact.save()
            request.user.message_set.create(message=("Unlinked device %s (id %s)" % (contact.address,contact.id)))
        except Contact.DoesNotExist:
            request.user.message_set.create(message=("Internal error, could not find id %s" % request.POST['contact_id']))
        except:
            request.user.message_set.create(message=("Internal error, %s was not linked to your account" % contact.address))
        # redirect so that a refresh won't repost the word list
        return HttpResponseRedirect("/account")


    if 'subscription_id' in request.POST:
        #TODO make sure this will only delete contact IDs for the current account
        try: 
            subscription_id = request.POST['subscription_id']
            subscription = Subscription.objects.get(id=subscription_id,wordisms_user=wordisms_user)
            subscription.delete()

            request.user.message_set.create(message=("Unlinked wordlist (id %s)" % subscription.wordlist.id))
            if not Subscription.objects.filter(wordisms_user=wordisms_user):
                request.user.message_set.create(message=("There are no more word lists linked to your account"))
        except Subscription.DoesNotExist:
            request.user.message_set.create(message=("Internal error, could not find subscription_id %s" % subscription_id))
#        except:
#            request.user.message_set.create(message=("Internal error, %s was not linked to your account" % subscription.wordlist.name))
        # redirect so that a refresh won't repost the word list
        return HttpResponseRedirect("/account")

    if 'wordlist_id' in request.POST:
        try:
            wordlist = Wordlist.objects.get(id=request.POST['wordlist_id'])
            if wordlist.creator != request.user:
                raise Exception 
            request.user.message_set.create(message=("Deleted wordlist '%s' (id %s)" % (wordlist.name, wordlist.id)))
            wordlist.delete()
        except Wordlist.DoesNotExist:
            request.user.message_set.create(message=("Internal error, could not find wordlist_id %s" % request.POST['wordlist_id']))
        except Exception:
            request.user.message_set.create(message=("You didn't create '%s' so you can't delete it!" % wordlist.name))

        # redirect so that a refresh won't repost the word list
        return HttpResponseRedirect("/account")

    if 'file' in request.FILES:
        print "entering this block"
        form = UploadFileForm(request.POST, request.FILES)
#        if form.is_valid():
        file = request.FILES['file']
#        print "file %\n" % file
        rows = csv.reader(file)
        wordlist = Wordlist(name=request.POST['title'], creator=request.user)
        wordlist.save()
        request.user.message_set.create(message=("Created wordlist '%s'" % wordlist.name))

        subscription = Subscription(wordlist=wordlist,wordisms_user=wordisms_user)
        subscription.save()
        request.user.message_set.create(message=("You are now subscribed to '%s'" % wordlist.name))

        for row in rows:
            print "%s,%s" % (row[0], row[1])
            dictionary_entry = DictionaryEntry(word=row[0], definition=row[1], wordlist=wordlist)
            dictionary_entry.save()

        request.user.message_set.create(message=("Populated new wordlist '%s'" % wordlist.name))
        # redirect so that a refresh won't repost the word list
        return HttpResponseRedirect("/account")

    contacts = Contact.objects.filter(wordisms_user=wordisms_user)
    responses, subscriptions = {} , {}
    for contact in contacts:
        responses[contact.id] = Responses.objects.filter(contact=contact)

    subscriptions = Subscription.objects.order_by('id').filter(wordisms_user=wordisms_user)
    return render_to_response('account/index.html', { 'user': request.user,
                                                      'wordisms_user': wordisms_user,
                                                      'contacts': contacts,
                                                      'responses': responses,
                                                      'subscriptions': subscriptions,
                                                      'file_upload_form': UploadFileForm()},
                              context_instance=RequestContext(request))