def post_content(request): if request.user.has_perm('POST_CONTENT'): try: # Check to see if we're coming here from a POST. If so, process it. If not, give 'em a fresh form. if request.method == 'POST' : form = PostContentForm(request.POST, content_types=client_get_content_types(request.user.session_id).data, media_types=client_get_media_types(request.user.session_id).data) # Check form validation -- kick the form back out with error messages if failed. valid = form.is_valid() if valid: if (form.cleaned_data['content_type'] == "PODCAST"): valid = False media = Media(media_type=form.cleaned_data['media_type'], name=form.cleaned_data['name'], url=form.cleaned_data['url'], duration=form.cleaned_data['duration']) if (not media.url or not verifyUrl(media.url)): messages.error(request, "Invalid or nonexistent podcast URL specified.", extra_tags='validation') elif (not media.media_type and not media.media_type == 'AUDIO'): messages.error(request, "Attached content needs to be audio.", extra_tags='validation') elif (not media.name): messages.error(request, "Podcast name needs to be specified.", extra_tags='validation') elif (not media.duration): messages.error(request, "Podcast duration needs to be specified.", extra_tags='validation') else: media.size = getUrlDownloadSize(media.url) media.file_type = getUrlMimeType(media.url) valid = True else: media = None if valid: content=Content(content_type=(form.cleaned_data['content_type']), title=purgeHtml(form.cleaned_data['title']), body=(form.cleaned_data['body']), summary=(form.cleaned_data['summary']), tag_csv=purgeHtml(form.cleaned_data['tags']), submitter_id=request.user.reckoner_id, media_items=[media]) # Submit to the API response = client_post_content(content, request.user.session_id) # Check to see if the API submission was a success. If not, clean the error and display. Otherwise, great! if not response.success: logger.error("Error from post attempt: " + response.message) raise Exception else: return HttpResponseRedirect('/blog') else: form = PostContentForm(content_types=client_get_content_types(request.user.session_id).data, media_types=client_get_media_types(request.user.session_id).data) c = RequestContext(request,{'form' : form,}) return render_to_response('post-content.html', c) except Exception: logger.error("Exception when showing and processing the Submit-Content form:") logger.error(traceback.print_exc(8)) raise Exception else: return HttpResponseRedirect("/")
def update_content_ajax(request): site_response = AjaxServiceResponse(success=False, message="whoops", message_description='No go. Try again later.') if (request.user.has_perm('UPDATE_ALL_CONTENT')): try: if request.method == 'POST': content_id = (request.POST.get('content-id', None)) commentary = sanitizeDescriptionHtml(request.POST.get('commentary', None)) if (commentary): commentary_user_id = request.user.reckoner_id else: commentary_user_id = None title = purgeHtml(request.POST.get('title', None)) body = request.POST.get('body', None) summary = request.POST.get('summary', None) tag_csv = purgeHtml(request.POST.get('tags', None)) media_id = (request.POST.get('media-id', None)) media_url = (request.POST.get('url', None)) media_type = (request.POST.get('media-type', None)) name = (request.POST.get('name', None)) url = (request.POST.get('url', None)) duration = (request.POST.get('duration', None)) if ((commentary and len(commentary) > 3000) or (title and len(title) > 300) or (body and len(body) > 50000) or (tag_csv and len(tag_csv) > 200) or (summary and len(summary) > 4000)): site_response = AjaxServiceResponse(success=False, message="too_long", message_description="Saved field is too long.") elif (url is not None) and not verifyUrl(url): site_response = AjaxServiceResponse(success=False, message="bad_media_url", message_description="Specified Media URL is invalid.") elif ((name is not None and name == "") or (duration is not None and duration == "")): site_response = AjaxServiceResponse(success=False, message="missing_required_field", message_description="Missing Required Field.") elif (content_id): if (media_id and url): media_items = [Media(id = media_id, media_type=media_type, name=name, url=url, file_type=getUrlMimeType(url), duration=duration, size=getUrlDownloadSize(url))] else: media_items = None contentUpdate = Content(id=content_id, commentary=commentary, commentary_user_id=commentary_user_id, media_items=media_items, summary=summary, title=title, body=body, tag_csv=tag_csv) service_response = client_update_content(contentUpdate, request.user.session_id) if (service_response.success): site_response = AjaxServiceResponse(success=True, message="success", message_description="Updated!") except Exception: logger.error("Exception when updating content:") logger.error(traceback.print_exc(8)) return HttpResponse(site_response.getXMLString())