def update_pages(): logging.info('-> updateing pages') from importing import get_docs_data docs = get_docs_data(Var.get_value('admin'),Var.get_value('password')) docs_by_keys = dict((doc['res_id'],doc) for doc in docs) updated_or_deleted = set() updated_cnt = deleted_cnt = created_cnt = 0 # updateing for page in Page.all(): # delete if not page.res_id in docs_by_keys: page.delete() deleted_cnt+=1 logging.info('page %s deleted'%page.res_id) updated_or_deleted.add( page.res_id ) else: doc = docs_by_keys[page.res_id] # update existing page.slug = doc['slug'] page.lang = doc['lang'] page.title = doc['title'] page.etag = doc['etag'] page.updated = doc['updated'] page.content = get_doc_content(doc['src']) page.src = doc['src'] page.edit_url = doc['edit_url'] page.put() logging.info('page %s updated'%doc['res_id']) updated_cnt+=1 updated_or_deleted.add( page.res_id ) # new pages new_pages_ids = set(docs_by_keys) - updated_or_deleted for new_page_id in new_pages_ids: doc = docs_by_keys[new_page_id] # create new page page = Page(key_name=doc['res_id'], slug = doc['slug'], lang = doc['lang'], title = doc['title'], etag = doc['etag'], updated = doc['updated'], src = doc['src'], edit_url = doc['edit_url'], content = get_doc_content(doc['src']), ) page.put() logging.info('page %s created'%doc['res_id']) created_cnt+=1 logging.info('<- updateing pages updated=%s created=%s deleted=%s'%(updated_cnt, created_cnt, deleted_cnt))
def pre_dispatch_admin(self): """ sprawdzenie dostepu do panelu admina """ if self._is_admin_request(): if self.app.local: class User(): def nickname(self): return 'test' def email(self): return '*****@*****.**' self.user = user = User() is_admin = True else: self.user = user = users.get_current_user() is_admin = users.is_current_user_admin() can_edit = is_admin or (user is not None and (user.email()==Var.get_value('admin'))) logging.debug('current_user: admin=%s can_edit=%s', users.is_current_user_admin(), can_edit) if not can_edit: if self.request.method=='POST': self.abort(403) # just 403 - for POSTs else: if user: # not admin return self.render('admin/login.html') else: # not authorized user so go to login return self.redirect(users.create_login_url(self.request.path)) # login and redirect assert can_edit and user is not None
def get_url(b58id): try: url = Url.get(b58id) if url is not None: ret = url.real else: ret = None except ObjectDoesNotExist: ret = Var.get('default_redirect') return ret
def vars(self): """ strona edycji ustawien portalu """ if self.request.method=='POST': name = self.request.params['name'] forms_and_vars = [] for var in Var.all(): if name == var.name: form = VarForm(self.request.POST, obj=var) if form.validate(): new_value = yaml.load(form.data['raw']) Var.set_value(var.name, new_value) self.set_flash(u'Zmienna "%s" zapisana.'%var.desc) return self.redirect_to('vars') else: form = VarForm(obj=var) forms_and_vars.append((form,var)) else: # default forms_and_vars = [(VarForm(obj=var),var) for var in Var.all()] return self.render('admin/vars.html', forms_and_vars=forms_and_vars)
def post(request): if request.method != 'POST': return HttpResponse('', status=400) if 'HTTP_X_SU_APIKEY' not in request.META: return HttpResponse('Invalid Header', status=401) if request.META['HTTP_X_SU_APIKEY'] != Var.get('apikey'): return HttpResponse('header value', status=401) body = json.loads(request.body) if 'url' not in body: return HttpResponse('', status=422) try: nw = Url.new_url(Var.get('base_url'), body['url']) except Exception as e: return HttpResponse(json.dumps({'error': str(e)}), status=500) ret = {} ret['short'] = nw ret['real'] = body['url'] return HttpResponse(json.dumps(ret), status=201)
def update_photos(): logging.info('-> updateing pages') from importing import get_docs_data, get_albums_data albums = get_albums_data(Var.get_value('admin'),Var.get_value('password')) docs_by_keys = dict((album['res_id'],album) for album in albums) updated_or_deleted = set() updated_cnt = deleted_cnt = created_cnt = 0 pupdated_cnt = pdeleted_cnt = pcreated_cnt = 0 # updateing for album in Album.all(): # delete if not album.res_id in docs_by_keys: for photo in album.photos: photo.delete() logging.info('photo %s deleted (in album=%s)', photo.res_id, album.res_id) pdeleted_cnt+=1 album.delete() deleted_cnt+=1 logging.info('album %s deleted'%album.res_id) updated_or_deleted.add( album.res_id ) else: # update existing album doc = docs_by_keys[album.res_id] album.title = doc['title'] album.put() logging.info('album %s updated'%doc['res_id']) updated_or_deleted.add( album.res_id ) ###################### # update/delete his photos pupdated_or_deleted=set() pdocs_by_keys = dict( (pdoc['res_id'],pdoc) for pdoc in doc['photos']) #add/remove/update for photo in album.photos: if not photo.res_id in pdocs_by_keys: photo.delete() pdeleted_cnt+=1 logging.info('photo %s deleted (in album=%s)', photo.res_id, album.res_id) else: pdoc = pdocs_by_keys[photo.res_id] photo.title = pdoc['title'] photo.src = pdoc['src'] photo.mimetype = pdoc['mimetype'] photo.height = pdoc['height'] photo.width = pdoc['width'] photo.order = doc['photos'].index(pdoc) photo.put() pupdated_cnt+=1 logging.info('photo %s updated (in album=%s)', photo.res_id, album.res_id) pupdated_or_deleted.add(photo.res_id) ###################### ## create new photos new_photos_ids = set(pdocs_by_keys) - pupdated_or_deleted for new_photo_id in new_photos_ids: pdoc = pdocs_by_keys[new_photo_id] Photo( album=album, key_name=pdoc['res_id'], src=pdoc['src'], title=pdoc['title'], mimetype=pdoc['mimetype'], height=pdoc['height'], width=pdoc['width'], order=doc['photos'].index(pdoc) ).put() logging.info('photo %s created (in album=%s)', photo.res_id, album.res_id) pcreated_cnt+=1 updated_cnt+=1 # new pages new_albums_ids = set(docs_by_keys) - updated_or_deleted for new_album_id in new_albums_ids: doc = docs_by_keys[new_album_id] # create new album album = Album(key_name=doc['res_id'], title = doc['title']) album.put() for pdoc in doc['photos']: # create new photo Photo( album=album, key_name=pdoc['res_id'], src=pdoc['src'], title=pdoc['title'], mimetype=pdoc['mimetype'], height=pdoc['height'], width=pdoc['width'], order=doc['photos'].index(pdoc) ).put() pcreated_cnt+=1 logging.info('photo %s created (in album=%s)', pdoc['res_id'], album.res_id ) logging.info('album %s created'%doc['res_id']) created_cnt+=1 logging.info('<- updateing album/photos updated=%s created=%s deleted=%s pupdated=%s pcreated=%s pdeleted=%s', updated_cnt, created_cnt, deleted_cnt, pupdated_cnt, pcreated_cnt, pdeleted_cnt)
def test_vars_models(self): langs = settings.VARS['langs'][1] # desc/value eq_(Var.all().count(), 0) # jeszcze nie ma w cache eq_(memcache.get('Var-langs'), None) #@UndefinedVariable # zapytujemy - dociaga z config i wklada do cache eq_(Var.get_value('langs'), langs) # juz jest w cache eq_(memcache.get('Var-langs'), langs) #@UndefinedVariable # ale jszcze nic nie mamy w datastore eq_(Var.all().count(), 0) # still zero # ladujemy z config Var.update_from_settings() eq_(Var.all().count(), 4) # wciaz 4 Var.update_from_settings() eq_(Var.all().count(), 4) # aktulizuemy zmienna ale ze zlym typem (nie zgadza sie z poprzednim) assert_raises(ValueError, Var.set_value, 'langs', 1) # aktualizujemy zmienna newvalue = ['foo', 'bar'] Var.set_value('langs', newvalue) eq_(memcache.get('Var-langs'), newvalue) #@UndefinedVariable memcache.delete('Var-langs') #@UndefinedVariable eq_(Var.get_value('langs'), newvalue) eq_(memcache.get('Var-langs'), newvalue) #@UndefinedVariable
def update_vars(self): """ wczytanie zmiennych z settings """ Var.update_from_settings() self.set_flash('Zmienne zaktualizowane.') return self.redirect_to('vars')