def pins( request ): response = Epoxy( request ) if response.method =='POST': form = AddPinForm( request.REQUEST ) if not form.is_valid(): return response.throw_error( error=form.errors, code=API_EXCEPTION_FORMERRORS).json() if len(form.cleaned_data['page_slug']) > 0: # attacch new pin to a selected page (both languages) response.add('page_slug',form.cleaned_data['page_slug']) try: page_en = Page.objects.get( slug=form.cleaned_data['page_slug'],language='EN') page_fr = Page.objects.get( slug=form.cleaned_data['page_slug'],language='FR') except Page.DoesNotExist: return response.throw_error( error=_("selected page does not exists"), code=API_EXCEPTION_FORMERRORS).json() response.add('page', [ page_en.json(), page_fr.json() ] ) if len(form.cleaned_data['parent_pin_slug']) > 0: # attacch new pin to a selected pin (pin children, usually displayed on the right side, both languages) response.add('parent_pin_slug',form.cleaned_data['parent_pin_slug']) try: pin_en = Pin.objects.get( slug=form.cleaned_data['parent_pin_slug'],language='EN') pin_fr = Pin.objects.get( slug=form.cleaned_data['parent_pin_slug'],language='FR') except Pin.DoesNotExist, e: return response.throw_error( error=_("selected pin does not exists. Exception: %s" % e), code=API_EXCEPTION_FORMERRORS).json() response.add('pin', [ pin_en.json(), pin_fr.json() ] ) #return response.queryset( Pin.objects.filter() ).json() try: p_en = Pin( title=form.cleaned_data['title_en'], language='EN', slug=form.cleaned_data['slug']) p_fr = Pin( title=form.cleaned_data['title_fr'], language='FR', slug=form.cleaned_data['slug']) if len(form.cleaned_data['parent_pin_slug']) > 0: p_en.parent = pin_en p_fr.parent = pin_fr p_en.save() p_fr.save() except IntegrityError, e: return response.throw_error( error={'slug':"Exception %s" % e}, code=API_EXCEPTION_INTEGRITY).json()
return response.throw_error( error=form.errors, code=API_EXCEPTION_FORMERRORS).json() if len(form.cleaned_data['parent_pin_slug']) > 0: # attacch new pin to a selected pin (pin children, usually displayed on the right side, both languages) response.add('parent_pin_slug',form.cleaned_data['parent_pin_slug']) try: pin_en = Pin.objects.get( slug=form.cleaned_data['parent_pin_slug'],language='EN') pin_fr = Pin.objects.get( slug=form.cleaned_data['parent_pin_slug'],language='FR') except Pin.DoesNotExist, e: return response.throw_error( error=_("selected pin does not exists. Exception: %s" % e), code=API_EXCEPTION_DOESNOTEXIST).json() response.add('pin', [ pin_en.json(), pin_fr.json() ] ) try: p_en = Pin( title=form.cleaned_data['title_en'], language='EN', slug=form.cleaned_data['slug']) p_fr = Pin( title=form.cleaned_data['title_fr'], language='FR', slug=form.cleaned_data['slug']) if len(form.cleaned_data['parent_pin_slug']) > 0: p_en.parent = pin_en p_fr.parent = pin_fr p_en.save() p_fr.save() except IntegrityError, e: return response.throw_error( error={'slug':"Exception %s" % e}, code=API_EXCEPTION_INTEGRITY).json() # attach to enquiry enquiry_en.pins.add( p_en ) enquiry_en.save()
filename = os.path.join( absolute_pin_path, f.name) pinup = open( filename , 'w' ) for chunk in f.chunks(): pinup.write( chunk ) pinup.close() # guess mimetype pin_mimetype = mimetypes.guess_type( filename )[0] filetitle, extension = os.path.splitext( f.name ) try: p_en = Pin( title=filetitle, language='EN', slug=slugify( "-".join( filetitle.split("_") ) ), mimetype=pin_mimetype, local=os.path.join( pin_path, os.path.basename( filename ) ) ) p_fr = Pin( title=filetitle, language='FR', slug=slugify( "-".join( filetitle.split("_") ) ), mimetype=pin_mimetype, local=os.path.join( pin_path, os.path.basename( filename ) ) ) p_en.save() p_fr.save() except IntegrityError, e: return response.throw_error( error="%s" % e, code=API_EXCEPTION_INTEGRITY ).json() if page_en and page_fr: page_en.pins.add( p_en ) page_fr.pins.add( p_fr ) page_en.save() page_fr.save() return response.json()
files = os.listdir(upload_path) for f in files: print f # name, mimetype and binary content filepath = os.path.join(upload_path, f) filename = os.path.basename(filepath) slug = re.sub(r'[^a-zA-Z\d-]', '', os.path.basename(filename)) mimetype = mimetypes.guess_type(filepath)[0] content = open(filepath, 'rb').read() # save english try: p = Pin(slug=slug, title=filename, mimetype=mimetype, language='EN') p.local.save(filename, ContentFile(content)) p.save() # for each language, create a pin clone pc = Pin(slug=p.slug, title=p.title, mimetype=p.mimetype, language='FR', local=p.local) pc.save() except IntegrityError, e: print "Cannot save '%s'. Delete the file first, because an IntegrityError exception occurred: %s" % ( filename, e)
try: pin_en = Pin.objects.get( slug=form.cleaned_data['parent_pin_slug'], language='EN') pin_fr = Pin.objects.get( slug=form.cleaned_data['parent_pin_slug'], language='FR') except Pin.DoesNotExist, e: return response.throw_error( error=_("selected pin does not exists. Exception: %s" % e), code=API_EXCEPTION_DOESNOTEXIST).json() response.add('pin', [pin_en.json(), pin_fr.json()]) try: p_en = Pin(title=form.cleaned_data['title_en'], language='EN', slug=form.cleaned_data['slug']) p_fr = Pin(title=form.cleaned_data['title_fr'], language='FR', slug=form.cleaned_data['slug']) if len(form.cleaned_data['parent_pin_slug']) > 0: p_en.parent = pin_en p_fr.parent = pin_fr p_en.save() p_fr.save() except IntegrityError, e: return response.throw_error(error={ 'slug': "Exception %s" % e },
files = os.listdir( upload_path ) for f in files: print f # name, mimetype and binary content filepath = os.path.join( upload_path, f ) filename = os.path.basename( filepath ) slug = re.sub(r'[^a-zA-Z\d-]','', os.path.basename( filename ) ) mimetype = mimetypes.guess_type( filepath )[0] content = open( filepath, 'rb').read() # save english try: p = Pin( slug=slug, title=filename, mimetype=mimetype, language='EN' ) p.local.save( filename, ContentFile( content ) ) p.save() # for each language, create a pin clone pc = Pin( slug=p.slug, title=p.title, mimetype=p.mimetype, language='FR', local=p.local ) pc.save() except IntegrityError, e: print "Cannot save '%s'. Delete the file first, because an IntegrityError exception occurred: %s" % ( filename, e) continue if __name__ == '__main__': # get corpus name
def pins(request): response = Epoxy(request) if response.method == 'POST': form = AddPinForm(request.REQUEST) if not form.is_valid(): return response.throw_error(error=form.errors, code=API_EXCEPTION_FORMERRORS).json() if len(form.cleaned_data['page_slug']) > 0: # attacch new pin to a selected page (both languages) response.add('page_slug', form.cleaned_data['page_slug']) try: page_en = Page.objects.get(slug=form.cleaned_data['page_slug'], language='EN') page_fr = Page.objects.get(slug=form.cleaned_data['page_slug'], language='FR') except Page.DoesNotExist: return response.throw_error( error=_("selected page does not exists"), code=API_EXCEPTION_FORMERRORS).json() response.add('page', [page_en.json(), page_fr.json()]) if len(form.cleaned_data['parent_pin_slug']) > 0: # attacch new pin to a selected pin (pin children, usually displayed on the right side, both languages) response.add('parent_pin_slug', form.cleaned_data['parent_pin_slug']) try: pin_en = Pin.objects.get( slug=form.cleaned_data['parent_pin_slug'], language='EN') pin_fr = Pin.objects.get( slug=form.cleaned_data['parent_pin_slug'], language='FR') except Pin.DoesNotExist, e: return response.throw_error( error=_("selected pin does not exists. Exception: %s" % e), code=API_EXCEPTION_FORMERRORS).json() response.add('pin', [pin_en.json(), pin_fr.json()]) #return response.queryset( Pin.objects.filter() ).json() try: p_en = Pin(title=form.cleaned_data['title_en'], language='EN', slug=form.cleaned_data['slug']) p_fr = Pin(title=form.cleaned_data['title_fr'], language='FR', slug=form.cleaned_data['slug']) if len(form.cleaned_data['parent_pin_slug']) > 0: p_en.parent = pin_en p_fr.parent = pin_fr p_en.save() p_fr.save() except IntegrityError, e: return response.throw_error(error={ 'slug': "Exception %s" % e }, code=API_EXCEPTION_INTEGRITY).json()
pinup = open(filename, 'w') for chunk in f.chunks(): pinup.write(chunk) pinup.close() # guess mimetype pin_mimetype = mimetypes.guess_type(filename)[0] filetitle, extension = os.path.splitext(f.name) try: p_en = Pin(title=filetitle, language='EN', slug=slugify("-".join(filetitle.split("_"))), mimetype=pin_mimetype, local=os.path.join(pin_path, os.path.basename(filename))) p_fr = Pin(title=filetitle, language='FR', slug=slugify("-".join(filetitle.split("_"))), mimetype=pin_mimetype, local=os.path.join(pin_path, os.path.basename(filename))) p_en.save() p_fr.save() except IntegrityError, e: return response.throw_error(error="%s" % e, code=API_EXCEPTION_INTEGRITY).json()