def getcurrentsite(http_post, path_info, query_string): """ Returns the site id and the page cache key based on the request. """ url = u'http://%s/%s' % (smart_unicode(http_post.rstrip('/')), \ smart_unicode(path_info.lstrip('/'))) pagecachekey = '%s?%s' % (smart_unicode(path_info), \ smart_unicode(query_string)) hostdict = hostcache_get() if not hostdict: hostdict = {} if url not in hostdict: default, ret = None, None for site in Site.objects.all(): if url.startswith(site.url): ret = site break if not default or site.default_site: default = site if not ret: if default: ret = default else: # Somebody is requesting something, but the user didn't create # a site yet. Creating a default one... ret = Site(name='Default Feedjack Site/Planet', \ url='www.feedjack.org', \ title='Feedjack Site Title', \ description='Feedjack Site Description. ' \ 'Please change this in the admin interface.') ret.save() hostdict[url] = ret.id hostcache_set(hostdict) return hostdict[url], pagecachekey
def sites(): root_domain = app.config.get("DOMAIN_ROOT") port = app.config.get("PORT", None) if request.method == "POST": domain = request.form["domain"] name = request.form["name"] if domain and name: site = Site() site.name = name.strip() site.domain = domain.strip() conflicting_site = Site.objects.filter(domain=site.domain).first() if not conflicting_site: site.description = _( "<h1>Introductory text</h1><p>This should " "contain simple help about what is changeable and how." "</p>" ) site.save() url = "//%s" % site.domain if root_domain: url += ".%s" % root_domain if port: url += ":%d" % port return redirect(url) return redirect(url_for("sites")) if root_domain == g.site.domain: sites = Site.objects.all() else: sites = Site.objects.filter(domain__endswith=g.site.domain) return render_template("sites.html", sites=sites, root_domain=root_domain, port=port)
def sites(): root_domain = app.config.get("DOMAIN_ROOT") port = app.config.get("PORT", None) if request.method == "POST": domain = request.form["domain"] name = request.form["name"] if domain and name: site = Site() site.name = name.strip() site.domain = domain.strip() conflicting_site = Site.objects.filter(domain=site.domain).first() if not conflicting_site: site.description = _( "<h1>Introductory text</h1><p>This should " "contain simple help about what is changeable and how." "</p>") site.save() url = "//%s" % site.domain if root_domain: url += ".%s" % root_domain if port: url += ":%d" % port return redirect(url) return redirect(url_for("sites")) if root_domain == g.site.domain: sites = Site.objects.all() else: sites = Site.objects.filter(domain__endswith=g.site.domain) return render_template("sites.html", sites=sites, root_domain=root_domain, port=port)
def register(request): if request.method == 'POST': username = request.POST['username'] password = request.POST['password1'] domain = request.POST['domain'] print "=== User: "******" password: ", password, 'domain: ', domain new_user = User.objects.create_user(username, '', password) # create hash for new user hasher = hashlib.md5() hasher.update(username + domain) h = hasher.hexdigest() site = Site(user=new_user, hash=h, domain=domain) site.save() new_user.save() return login_to_page(request, username, password) else: form = RegistrationForm() return render_to_response( 'registration/registration_form.html', {'form' : form}, context_instance=RC(request, {}), )
def purify_html(request): """ 过滤掉多余css,修正图片一些属性,替换超链接 :param request: :return: """ html = request.GET.get('html', None) base_url = request.GET.get('baseUrl', None) css_path = request.GET.get('cssPath', None) # 若果domain已经在数据库了就找到,没有就新建 domain_name = get_top_domain(base_url) site_set = Site.objects.filter(domain_name=domain_name) if site_set.exists(): s = site_set[0] else: s = Site() s.domain_name = domain_name # 保存二级页面的主要显示区域的css if css_path.strip(): if not s.second: second = SecondModifications() second.css_path = css_path.strip() second.save() s.second = second else: second = s.second second.css_path = css_path.strip() second.save() s.save() res = purify(html, base_url) if not res: return HttpResponse(status=404) return HttpResponse(res)
def siteconfig(request): if request.method == 'GET': return render_to_response('admin/siteconfig.html') if request.method == 'POST': sitename = request.POST.get('sitename') keywords = request.POST.get('keywords') descriptions = request.POST.get('descriptions') copyrightinfo = request.POST.get('copyrightinfo') form = PictureForm(request.POST, request.FILES) if form.is_valid(): if 'login' and 'banner' in request.FILES: login = request.FILES["login"] banner = request.FILES["banner"] else: login = None banner = None parser = ImageFile.Parser() for chunk in login.chunks() and banner.chunks(): parser.feed(chunk) site = Site() site.sitename = sitename site.keywords = keywords site.descriptions = descriptions site.login = login site.banner = banner site.copyrightinfo = copyrightinfo site.save() return render_to_response('admin/siteconfig.html')
def _tx(): site = Site.get_by_key_name(loc["id"]) if site is None: site = Site(key_name=loc["id"]) site.location = GeoPt(lat = loc["location"][0], lon = loc["location"][1]) site.name = loc["name"] site.region = loc["region"] site.save()
def api(request): user = request.user if not user: username = request.POST.get("username", "") password = request.POST.get("password", "") user = authenticate(username= username, password=password) if user is not None and user.has_perm("apps.add_task"): method = request.POST.get("method") if method == "create_task": task_data = request.POST.get("task", "") sites_data = request.POST.get("sites", "") try: task = Task(**json.loads(task_data)) task.user = user task.clean_fields() sites = [] sites_data = json.loads(sites_data) for site_data in sites_data: site = Site(**site_data) site.clean_fields(exclude=['task']) sites.append(site) task.save() for site in sites: site.task = task site.save() rule = Rule(**Constants.DEFAULT_RULE) rule.site = site rule.save() controller = TaskController(task) controller.runTask() ret = {"status":"ok", "task":task.id} except Exception, e: ret = {"status":"InputError", "error_details":str(e)} elif method == "run_task": try: id = request.POST.get("id") task = Task.objects.get(id=id) controller = TaskController(task) controller.runTask() ret = {"status":"ok"} except Exception, e: ret = {"status":"RunError", "error_details":str(e)}
def import_sites(modeladmin, request, queryset): with open('D:/Users/mcpherro/PycharmProjects/Sites/sites.csv', 'rb') as f: reader = csv.reader(f) for row in reader: country_name = row[2] for c in countries: if row[2]==c[1]: country_name = c[0] s = Site(id=row[0], site=row[1], country=country_name, data_source=row[3], site_type=row[7], display=row[8]) if row[4] != 'NA': s.latitude = row[4] if row[5] != 'NA': s.longitude = row[5] if row[6] != 'NA': s.altitude = row[6] if row[4] != 'NA' and row[5] != 'NA': pnt = django.contrib.gis.geos.GEOSGeometry('POINT(%s %s)' % (row[5], row[4])) s.map_location = pnt s.save() return None # Return None to display the change list page again.
def crawl(url): ''' Crawl entire site :param url: URL of website :type url: str ''' if not url.startswith('http://') and not url.startswith('https://'): url = 'http://%s/' % url if newurl(url): print "New URL" else: print "URL already in DB" return print url # get correct title, detect language p = Site(title=url, url=url, show=0) p.published = datetime.datetime.today() p.save() thr = threading.Thread(target=__crawl_address, args=[url], kwargs={}) thr.start() return
def populate_blocks(): existing_site = Site.objects.filter(name=SITE) if existing_site.count() > 0: print "Site ", SITE, " already exists in database." existing_site = existing_site[0] else: s = Site(name=SITE) s.save() existing_site = s for block_name in BLOCKS: existing_block = BedSet.objects.filter(name=block_name, site=existing_site) if existing_block.count() > 0: print " Block ", block_name, " already exists in database." existing_block = existing_block[0] else: b = BedSet(name=block_name, site=existing_site) b.save() existing_block = b print " Saved", block_name, "as Block." for bed_name in BLOCKS[block_name]: width_UOM = BLOCKS[block_name][bed_name]["width_UOM"] existing_width_UOM = LengthUOM.objects.filter(unit=width_UOM) if existing_width_UOM.count() > 0: print " WidthUOM ", width_UOM, " already exists in database." existing_width_UOM = existing_width_UOM[0] else: wuom = LengthUOM(unit=width_UOM) wuom.save() existing_width_UOM = wuom print " Saved", width_UOM, "as WidthUOM." length_UOM = BLOCKS[block_name][bed_name]["length_UOM"] existing_length_UOM = LengthUOM.objects.filter(unit=length_UOM) if existing_length_UOM.count() > 0: print " LengthUOM ", length_UOM, " already exists in database." existing_length_UOM = existing_length_UOM[0] else: luom = LengthUOM(unit=length_UOM) luom.save() existing_length_UOM = luom print " Saved", length_UOM, "as LengthUOM." bed_name = bed_name print bed_name width = BLOCKS[block_name][bed_name]["width"] print width length = BLOCKS[block_name][bed_name]["length"] width_UOM = existing_width_UOM length_UOM = existing_length_UOM existing_bed = Bed.objects.filter(name=bed_name, bed_set=existing_block) if existing_bed.count() > 0: print " Bed ", bed_name, "in ", existing_block, " already exists in database." existing_bed = existing_bed[0] else: b = Bed( site=existing_site, bed_set=existing_block, name=bed_name, width=width, width_UOM=width_UOM, length=length, length_UOM=length_UOM, ) b.save() existing_bed = b print " Saved", bed_name, "in", block_name, "as Bed."
def create(site_id, address, site_name, ems_id): site = Site( site_id=site_id, address=address, site_name=site_name, ems_id=ems_id ) return site.save()
def save(request): """ 存储导航项,所选区域的css路径 :param request: :return: """ if request.method == "POST": kind = request.POST.get('type', 'contents') url = request.POST.get('url', None) # 如果抓取二级页面内容 if kind == 'contents': navs_str = request.POST.get('navs', None) if navs_str: navs = json.loads(navs_str, 'utf-8') domain_name = get_top_domain(url) site_set = Site.objects.filter(domain_name=domain_name) if site_set.exists(): s = site_set[0] s.nav_first.clear() else: s = Site() s.domain_name = domain_name.strip() s.save() for nav in navs: if nav['name'].strip() and nav['url'].strip(): n = NavBlock() n.father_id = 0 n.name = nav['name'].strip() n.url = nav['url'].strip() n.save() for second in nav['secondNavs']: sn = NavBlock() sn.father_id = n.id sn.name = second['name'].strip() sn.url = second['url'].strip() sn.save() # 添加二级nav s.nav_first.add(sn) # 添加一级nav s.nav_first.add(n) return HttpResponse(status=200) # 如果抓取图片 elif kind == 'imgs': url_str = request.POST.get('imgUrl', None) img_urls = json.loads(url_str, 'utf-8') domain_name = get_top_domain(url) site_set = Site.objects.filter(domain_name=domain_name) if site_set.exists(): s = site_set[0] if s.sliders: s.sliders.clear() else: s = Site() s.domain_name = domain_name.strip() s.save() for url in img_urls: if url.strip(): slider = Slider() slider.url = url.strip() slider.save() s.sliders.add(slider) return HttpResponse(status=200) elif kind == 'css': domain_name = get_top_domain(url) css_path = request.POST.get('cssPath', None) site_set = Site.objects.filter(domain_name=domain_name) if site_set.exists(): s = site_set[0] else: s = Site() s.domain_name = domain_name # 保存二级页面的主要显示区域的css if css_path.strip(): if not s.second: second = SecondModifications() second.css_path = css_path.strip() second.save() s.second = second else: second = s.second second.css_path = css_path.strip() second.save() s.save() return HttpResponse(status=200) elif kind == 'status': uid = request.POST.get('id', None) if uid: site_set = Site.objects.filter(id=uid) if site_set.exists(): site = site_set[0] site.is_active = not site.is_active site.save() return HttpResponse(status=200) elif request.method == "DELETE": request_delete = QueryDict(request.body) kind = request_delete.get('type', 'imgs') url = request_delete.get('url', None) if kind == 'imgs': domain_name = get_top_domain(url) site_set = Site.objects.filter(domain_name=domain_name) if site_set.exists(): s = site_set[0] s.sliders.clear() return HttpResponse(status=204) else: return HttpResponse(status=404) return HttpResponse(status=403)