def handle(self, *args, **kwargs): print('Loading data into database....') try: p = Page.objects.get(name_slug='index') except: p = Page() p.name_slug = 'index' p.title = 'Welcome to the WEZOM support desktop!' p.content = 'You can send your message here.' p.save() print('Creating main page!') try: u = User.objects.get(username='******') except: u = User() u.username = '******' u.is_active = True u.is_superuser = True u.is_staff = True u.save() u.set_password('admin') u.save() print('Creating superuser login admin password admin.') load_cat()
def update_from_archive(self, archive, obj): old_pks = list(Page.objects.filter(book=obj).values_list("id", flat=True)) archive = ZipFile(archive) toc = archive.read("toc.py") toc = toc.replace("(", "[").replace(")", "]").replace("'", '"') obj.toc = toc obj.save() pics = [name for name in archive.namelist() if name.startswith("pics/") and not name == "pics/"] archive.extractall(settings.MEDIA_ROOT, pics) appendix_pattern = re.compile(r"^ap(?P<section>[a-z])\.html$") ch_pattern = re.compile(r"^ch(?P<ch>\d+)\.html$") chs_pattern = re.compile(r"^ch(?P<ch>\d+)s(?P<s>\d+)\.html$") for filename in archive.namelist(): if not filename.split(".")[-1] == "html": continue slug = filename[:-5] try: page = Page.objects.get(slug=slug, book=obj) old_pks.remove(page.pk) except Page.DoesNotExist: page = Page(slug=slug, book=obj) # create name if page is new if filename == "index.html": name = u"Первая страница" elif chs_pattern.match(filename): r = chs_pattern.match(filename) name = u"Глава %s, раздел %s" % (int(r.group("ch")), int(r.group("s"))) page.chapter = r.group("ch") page.section = r.group("s") elif ch_pattern.match(filename): r = ch_pattern.match(filename) name = u"Глава %s" % int(r.group("ch")) page.chapter = r.group("ch") elif appendix_pattern.match(filename): r = appendix_pattern.match(filename) name = u"Приложение %s" % r.group("section").upper() page.chapter = u"ap" page.section = r.group("section") else: name = filename page.name = name page.content = archive.read(filename) page.save() Page.objects.filter(pk__in=old_pks).delete() archive.close()
def handle(self, *args, **options): print 'Start' Page.objects.all().delete() p = Page() p.title = 'логин admin пароль 123' p.content = 'Conteentttt ttttt t tt ' p.alias = 'main' p.meta_keywords = 'main' p.meta_description = 'main' p.meta_title = 'main' p.save() print 'Page main was created'
def handle(self, *args, **options): print('Start loading...') Page.objects.all().delete() Slider.objects.all().delete() Gallery.objects.all().delete() # главная path = os.path.join(BASE_DIR, '..', 'data', 'index.txt') with open(path, 'r') as f: txt = f.read() print(txt) p = Page() p.title = 'О нас' p.content = txt p.alias = 'home' p.save() # расписание path = os.path.join(BASE_DIR, '..', 'data', 'schedule.txt') with open(path, 'r') as f: txt = f.read() print(txt) p = Page() p.title = 'Расписание' p.content = txt p.alias = 'schedule' p.save() # импорт картинок слайдера for i in range(1, 5): img = '%s.jpg' % i img_abspath = os.path.join(BASE_DIR, '..', 'data', img) print('Importing... %s' % img_abspath) s = Slider() s.title = img s.desc = 'Description of %s' % img s.save() with open(img_abspath, 'rb') as doc_file: s.image.save(img, File(doc_file), save=True) # импорт галереи for i in range(1, 22): img = '%s.jpg' % i img_abspath = os.path.join(BASE_DIR, '..', 'data', 'gallery', img) g = Gallery() g.title = img g.save() with open(img_abspath, 'rb') as doc_file: g.image.save(img, File(doc_file), save=True)
def story_meta(filename, path): print "story_meta" notebook_name = path.split("/")[-1][:-9] dom = parse(path+"/"+filename) resource = dom.getElementsByTagName("resource")[0] story_title = resource.getElementsByTagName("dc:title")[0].firstChild.nodeValue raw_contrib = [x.firstChild.nodeValue for x in resource.getElementsByTagName("dcterms:contributor") ] contrib = json.dumps(raw_contrib) required = resource.getElementsByTagName("dcterms:requires") pages = len(required) try: date = resource.getElementsByTagName("dcterms:created")[0].firstChild.nodeValue except AttributeError: date = "" try: description = resource.getElementsByTagName("dc:description")[0].firstChild.nodeValue except AttributeError: description = "" try: comments = resource.getElementsByTagName("bl:comments")[0].firstChild.nodeValue except AttributeError: comments = "" raw_subject = [sub.firstChild.nodeValue for sub in resource.getElementsByTagName("dc:subject")] subject = json.dumps(raw_subject) raw_keywords = [] try: temp = dom.getElementsByTagName("bl:keywords")[0] for word in temp.getElementsByTagName("bl:keyword"): k = word.getElementsByTagName("bl:kw")[0].firstChild.nodeValue subkw = word.getElementsByTagName("bl:subkeywords")[0] v = ", ".join(sub.firstChild.nodeValue for sub in subkw.getElementsByTagName("bl:subkw")) raw_keywords.append((k, v)) except AttributeError: pass keyword = json.dumps(raw_keywords) notebook = Notebook.objects.get(short_title=notebook_name) story = Story(notebook=notebook, title=story_title, created=date, description=description, comment=comments, contributor=contrib, subject=subject, keyword=keyword, pages=pages ) story.save() i=1 for page in required: page_path = page.firstChild.nodeValue page_uuid = uuid.uuid4() page_entry = Page(story = story, filename = page_path, uuid = page_uuid,number=i) i+=1 page_entry.save()
def save_page(request): title = request.REQUEST["title"] url = request.REQUEST["url"] keywords = request.REQUEST["keywords"] desc = request.REQUEST["desc"] content = request.REQUEST["content"] template = request.REQUEST["template"] id = request.REQUEST["id"] page = Page() if id == "-1" else Page.objects.get(pk=int(id)) logging.info("save") if page: page.title = title page.url = url page.keywords = keywords page.desc = desc page.content = content page.template = template page.save() return HttpResponse("{}")
def handle(self, *args, **options): print('loading pages....') Page.objects.all().delete() p = Page() p.title = "Main page" p.alias = "main" p.content = "Content" p.save() p = Page() p.title = "Contact page" p.content = "Content" p.alias = "contact" p.save() p = Page() p.title = "Shop page" p.alias = "shop" p.content = "Content" p.save()
def add_page(request): """Page with a form for creating an article.""" if no_access(request, 'pages'): return HttpResponseRedirect(reverse('no_access')) grp = get_info(request) error = '' p = { 'url': '', 'title': '', 'text': '', 'pic': '', 'date': '', 'paragraphs': [], 'access': '', 'status': '', 'order': '', 'tags': '' } if request.method == 'POST': try: url = request.POST['url'] title = request.POST['title'] pic = request.POST['pic'] order = int(request.POST['order']) access = request.POST['access'] status = request.POST['status'] text = request.POST['text'] tags = request.POST['tags'] p['url'] = url p['title'] = title p['text'] = text p['pic'] = pic p['access'] = access p['status'] = status p['order'] = order p['tags'] = tags parags = [] taglist = [x.strip() for x in tags.split(',')] i = 1 while 'section_title_' + str(i) in request.POST: parags.append({ 'title': request.POST['section_title_' + str(i)], 'text': request.POST['section_text_' + str(i)], 'order': i }) i += 1 p['paragraphs'] = parags if title == '': raise Exception('Enter page title.') if url == '': raise Exception('Enter page URL') if order < 0: raise Exception('Order must be a positive number.') if len(Page.objects.filter(order=order)) > 0: raise Exception('Order number must be unique') for par in parags: if par['title'] == '': raise Exception('Sections must have titles') except Exception as ex: error = ex else: pag = Page(title=title, url=url, pic=pic, order=order, access=access, status=status, text=text, add_date=datetime.now().date(), added_by=request.user.username) pag.save() for tag in taglist: if len(Tag.objects.filter(name=tag)) == 1: pag.tags.add(Tag.objects.get(name=tag)) else: tg = Tag(name=tag) tg.save() pag.tags.add(tg) pag.save() for parag in parags: paragraph = Paragraph(title=parag['title'], text=parag['text'], order=parag['order'], page=pag) paragraph.save() return HttpResponseRedirect(reverse('admin_pages')) template = loader.get_template('main/admin_edit_page.html') context = RequestContext(request, { 'page': p, 'error_message': error, 'edit': 'no', 'group': grp }) return HttpResponse(template.render(context))
def add_page(request): """Page with a form for creating an article.""" if no_access(request, 'pages'): return HttpResponseRedirect(reverse('no_access')) grp = get_info(request) error = '' p = {'url':'', 'title':'', 'text':'', 'pic':'', 'date':'', 'paragraphs':[], 'access':'', 'status':'', 'order':'', 'tags':''} if request.method == 'POST': try: url = request.POST['url'] title = request.POST['title'] pic = request.POST['pic'] order = int(request.POST['order']) access = request.POST['access'] status = request.POST['status'] text = request.POST['text'] tags = request.POST['tags'] p['url'] = url p['title'] = title p['text'] = text p['pic'] = pic p['access'] = access p['status'] = status p['order'] = order p['tags'] = tags parags = [] taglist = [x.strip() for x in tags.split(',')] i = 1 while 'section_title_'+str(i) in request.POST: parags.append({'title':request.POST['section_title_'+str(i)], 'text':request.POST['section_text_'+str(i)], 'order':i}) i += 1 p['paragraphs'] = parags if title == '': raise Exception('Enter page title.') if url == '': raise Exception('Enter page URL') if order < 0: raise Exception('Order must be a positive number.') if len(Page.objects.filter(order=order)) > 0: raise Exception('Order number must be unique') for par in parags: if par['title'] == '': raise Exception('Sections must have titles') except Exception as ex: error = ex else: pag = Page(title=title, url=url, pic=pic, order=order, access=access, status=status, text=text, add_date=datetime.now().date(), added_by=request.user.username) pag.save() for tag in taglist: if len(Tag.objects.filter(name=tag)) == 1: pag.tags.add(Tag.objects.get(name=tag)) else: tg = Tag(name=tag) tg.save() pag.tags.add(tg) pag.save() for parag in parags: paragraph = Paragraph(title=parag['title'], text=parag['text'], order=parag['order'], page=pag) paragraph.save() return HttpResponseRedirect(reverse('admin_pages')) template = loader.get_template('main/admin_edit_page.html') context = RequestContext(request, { 'page':p, 'error_message':error, 'edit':'no', 'group':grp }) return HttpResponse(template.render(context))