示例#1
0
def save_new_artist(request):
    if request.method == 'POST':
        form = AddArtistForm(request.POST)
        artist = None
        author = request.user
        if form.is_valid():
            artist = form.save()
            artist_id = artist.id
            if 'website_url' in request.POST:
                if 'website_name' in request.POST:
                    website_name = request.POST.get('website_name')
                else:
                    website_name = 'Website for ' + artist.name
                website_url = request.POST.get('website_url')
                website = URL()
                website.name = website_name
                website.link = website_url
                website.save()

                artist.website = website
                artist.author = author
                artist.save()
            return HttpResponseRedirect('/wc_admin/view_artist/?id=' + str(artist_id))
        else:
            error_message = 'The form was not valid. The data was not saved.'
            return render(request, 'blog/error.html', {'error_message': error_message, 'form': form})
    else:
        error_message = 'You followed the wrong procedure to get here, or you are not logged in.'
        return render(request, 'blog/error.html', {'error_message': error_message})
示例#2
0
def save_new_work(request):
    if request.method == 'POST' and 'artist_id' in request.POST:
        form = AddWorkForm(request.POST)
        artist_id = request.POST.get('artist_id')
        artist = Artist.objects.get(pk=artist_id)
        author = request.user
        work = None
        if form.is_valid():
            work = form.save()
            work.creator = artist
            work.save()
            work_id = work.id
            if 'website_url' in request.POST:
                if 'website_name' in request.POST:
                    website_name = request.POST.get('website_name')
                else:
                    website_name = 'Website for ' + work.title
                website_url = request.POST.get('website_url')
                website = URL()
                website.name = website_name
                website.link = website_url
                website.save()

                work.website = website
                work.author = author
                work.save()
            if 'styles' in request.POST:
                styles_string = request.POST.get('styles')
                styles_list = styles_string.split(',')
                styles = []

                for style in styles_list:
                    styles.append(style.strip().lower().replace('-', ' ').replace('_', ' '))

                # A Style is another model, linked to the "work" via a ManyToMany field
                # Here we check to see if the user has entered styles that already exist in the database
                for style in styles:
                    pre_existing_styles = Style.objects.filter(name=style)
                    if len(pre_existing_styles) > 0:
                        pre_existing_style = pre_existing_styles[0]
                        work.styles.add(pre_existing_style)
                        work.save()
                    else:
                        #create a new style, save it to the DB, and add it to the work
                        new_style = Style()
                        new_style.name = style
                        new_style.save()
                        work.styles.add(new_style)
                        work.save()

            return HttpResponseRedirect('/wc_admin/view_work/?id=' + str(work_id))
        else:
            error_message = 'The form was not valid. The data was not saved.'
            return render(request, 'blog/error.html', {'error_message': error_message, 'form': form})
    else:
        error_message = 'The info was not properly posted. The data was not saved.'
        return render(request, 'blog/error.html', {'error_message': error_message})
示例#3
0
def save_new_production_company(request):
    if request.user.is_authenticated() and request.method == 'POST':
        production_company_form = AddProductionCompanyForm(request.POST)
        if production_company_form.is_valid():
            production_company = production_company_form.save()
            production_company.author = request.user
            if 'URL' in request.POST:
                website = URL()
                website.link = request.POST.get('URL')
                if 'website_name' in request.POST:
                    website_name = request.POST.get('website_name')
                    if website_name is not '':
                        website.name = website_name
                    else:
                        website.name = 'Website for ' + production_company.name
                else:
                    website.name = 'Website for ' + production_company.name
                website.save()
                production_company.website = website
            production_company.save()
            return HttpResponseRedirect('/wc_admin/view_production_company/?id=' + str(production_company.id))
        else:
            error_message = 'The form was not valid. The data was not saved.'
            return render(request, 'blog/error.html', {'error_message': error_message, 'form': production_company_form})
    else:
        error_message = 'You followed the wrong procedure to get here, or you are not logged in.'
        return render(request, 'blog/error.html', {'error_message': error_message})