Exemple #1
0
def add_file(request):
    fileFormSets = formset_factory(FileForm)
    tagFormSets = formset_factory(TagForm)
    authorFormSets = formset_factory(AuthorForm)
    folderFormSets = formset_factory(FolderForm)
    if request.method == 'POST':
        fileFormSet = fileFormSets(request.POST)
        tagFormSet = tagFormSets(request.POST)
        authorFormSet = authorFormSets(request.POST)
        folderFormSet = folderFormSets(request.POST)
        if 'add_file' in request.POST:
#            if fileFormSet.is_valid():
               for form in fileFormSet.forms:
                   internal_id = uuid.uuid4()
                   b = Bookmark(
                       type='F',
                       title=form['title'].data,
                       #url=form['url'].data,
                       description=form['description'].data,
                       date=datetime.datetime.now(),
                       pub_date=form['pub_date'].data,
                       notes=form['notes'].data,
                       folder=Folder.objects.get(id=form['folder'].data),
                       internal_id=internal_id
                   )
                   #path = handle_uploaded_file(request.FILES['form-0-internal_url'])
                   #b.internal_url = path
                   #file_list = form['files']
                   b.save()
                   files = request.FILES.getlist('form-0-files[]')             
                   for f in files:
                       path = handle_uploaded_file(f, internal_id)
                       t = BookmarkItem(external_url=form['url'].data, internal_url=path)
                       t.save()
                       b.items.add(t)
                   
                   tag_list = form['tags'].data
                   for tag in tag_list:
                       b.tags.add(tag)
                   author_list =form['authors'].data
                   for author in author_list:
                       b.authors.add(author)
                   return HttpResponseRedirect(settings.BASE_URL)
        if 'add_tag' in request.POST:
            if tagFormSet.is_valid():
                for form in tagFormSet.forms:
                    if not form['tag_name'].data == '':
                        t = Tag(tag_name=form['tag_name'].data)
                        t.save()
        if 'add_author' in request.POST:
            if authorFormSet.is_valid():
                for form in authorFormSet.forms:
                    if not form['author_name'].data == '':
                        a = Author(author_name=form['author_name'].data)
                        a.save()
        if 'add_folder' in request.POST:
            if folderFormSet.is_valid():
                for form in folderFormSet.forms:
                    if not form['folder_name'].data == '':
                        folderList = form['parent'].data
                        for folderId in folderList:
                            parent = Folder.objects.get(id=folderId)
                            f = Folder(folder_name=form['folder_name'].data, parent=parent)
                            f.save()
    else:
        fileFormSet = fileFormSets()
        tagFormSet = tagFormSets()
        authorFormSet = authorFormSets()
        folderFormSet = folderFormSets()
    return render_to_response('marnie/add_file.html', {
        "fileFormSet": fileFormSet,
        "tagFormSet": tagFormSet,
        "authorFormSet": authorFormSet,
        "folderFormSet": folderFormSet,
    }, RequestContext(request))
Exemple #2
0
def add(request):
    c = {}
    c.update(csrf(request))

    bookmarkForm = BookmarkForm(user=request.user.id)
    TagFormSet = formset_factory(TagForm)
    AuthorFormSet = formset_factory(AuthorForm)
    folderForm = FolderForm(user=request.user.id)

    if request.method == 'POST':
        bookmarkForm = BookmarkForm(request.POST, user=request.user.id)
        tagFormSet = TagFormSet(request.POST)
        authorFormSet = AuthorFormSet(request.POST)
        folderForm = FolderForm(request.POST, user=request.user.id)

        if 'add_bookmark' in request.POST:
                form = bookmarkForm
                if form.is_valid() and form.has_changed():
                    internal_id = uuid.uuid4()
                    type = "B"
		    title = form.cleaned_data["title"]
		    urls = form.cleaned_data["urls"]
                    user = request.user
                    description = form.cleaned_data["description"]
                    date = datetime.datetime.now()
                    publication_date = form.cleaned_data["pub_date"]                     
                    folder = form.cleaned_data["folder"]
                    notes = form.cleaned_data["notes"]
                    
                    bookmark = Bookmark(type=type, 
                        internal_id=internal_id, 
                        title=title, 
                        description=description, 
                        date=date, 
                        pub_date=publication_date,
                        folder=folder,
                        notes=notes,
                        user=user)
                    bookmark.save()                     
                   
                    for url in urls.split():
                        path = handle_snapshot(url, internal_id)
                        t = BookmarkItem(external_url=url, internal_url=path)
                        t.user = request.user
                        t.save()
                        bookmark.items.add(t) 
                   
                    tags = form.cleaned_data['tags']
                    for tag in tags:
                        bookmark.tags.add(tag)   
                   
                    authors = form.cleaned_data['authors']
                    for author in authors:
                        bookmark.authors.add(author)

                    return HttpResponseRedirect(settings.BASE_URL + "dashboard")
        if 'add_tag' in request.POST:
            if tagFormSet.is_valid():
                for form in tagFormSet.forms:
                    if not form['tag_name'].data == '':
                        t = Tag(tag_name=form['tag_name'].data)
                        t.user = request.user
                        t.save()
        if 'add_author' in request.POST:
            if authorFormSet.is_valid():
                for form in authorFormSet.forms:
                    if not form['author_name'].data == '':
                        a = Author(author_name=form['author_name'].data)
                        a.user = request.user
                        a.save()
        if 'add_folder' in request.POST:
            if folderForm.is_valid():
                if not folderForm['folder_name'].data == '':
                    folderList = folderForm['parent_folder'].data
                    for folderId in folderList:
                        parent = Folder.objects.get(id=folderId)
                        f = Folder(folder_name=folderForm['folder_name'].data, parent=parent)
                        f.user = request.user
                        f.save()
                    return HttpResponseRedirect(settings.BASE_URL + "dashboard")
    else:
        bookmarkForm = BookmarkForm(user=request.user.id)
        tagFormSet = TagFormSet()
        authorFormSet = AuthorFormSet()
        folderForm = FolderForm(user=request.user.id)

    return render_to_response('marnie/add.html', {
        "bookmarkForm": bookmarkForm, 
        "tagFormSet": tagFormSet, 
        "authorFormSet": authorFormSet, 
        "folderForm": folderForm,
    }, RequestContext(request))