Exemplo n.º 1
0
def mirror_model(instance, data, db, table, created, imutable, verbose=False):
    res = {"created": 0, "updated": 0}
    modelname = str(instance._meta)
    # record data
    if created is True:
        res["status"] = R.write(db, table, data)
        res["created"] += 1
        if verbose is True:
            print("[ " + modelname + " ] Document " + str(instance.pk) +
                  " created in table " + table)
    else:
        optype = "write"
        if imutable is False:
            document_exists_in_db, document = document_exists(
                db, table, modelname, instance.pk)
            if document_exists_in_db:
                optype = "update"
        if optype == "update":
            res["status"] = R.update(db, table, data, {})
            res["updated"] += 1
            if verbose is True:
                print("[ " + modelname + " ] Document " + str(instance.pk) +
                      " updated in table " + table)
            return res
        res["status"] = R.write(db, table, data)
        res["created"] += 1
        if verbose is True:
            print("[ " + modelname + " ] Document " + str(instance.pk) +
                  " created in table " + table)
    return res
Exemplo n.º 2
0
 def mirror(self, data):
     #print 'Exists: '+str(self.document_exists())
     if self.document_exists() is True:
         filters = (r.row['domain'] == self.server.domain) & (r.row['uri']
                                                              == self.url)
         res = R.update(self.server.domain, "pages", data, filters)
     else:
         res = R.write(self.server.domain, "pages", data)
     return res
Exemplo n.º 3
0
def model_delete(sender, instance, **kwargs):
    modelname = str(instance._meta)
    soft_delete = get_option(instance, "soft_delete")
    if soft_delete is False:
        document_exists_in_db = document_exists(DB, TABLE, modelname,
                                                instance.pk)
        if document_exists_in_db:
            filters = {"model": modelname, "pk": instance.pk}
            R.delete_filtered(DB, TABLE, filters)
    return
Exemplo n.º 4
0
 def save(self, *args, **kwargs):
     super(SiteTemplate, self).save(*args, **kwargs)
     # save template to Microb server
     filepath = settings.BASE_DIR + "/microb/servers/" + self.server.domain + "/templates/view.html"
     #~ write the file
     filex = open(filepath, "w")
     filex.write(self.content.encode('utf-8'))
     filex.close()
     # send command to the Microb server to reparse the templates
     data = {"Name": "reparse_templates", "Reason": "Template edit"}
     R.write(self.server.domain, "commands", data)
     return
Exemplo n.º 5
0
def delete_model(instance, db, table, imutable, soft_delete):
    modelname = str(instance._meta)
    if imutable == True:
        return
    filters = {"model": modelname, "pk": instance.pk}
    document_exists_in_db, document = document_exists(db, table, modelname,
                                                      instance.pk)
    if soft_delete is False:
        if document_exists_in_db:
            R.delete_filtered(db, table, filters)
    else:
        document["deleted"] = True
        R.update(db, table, document, filters)
    return
Exemplo n.º 6
0
def synchronize_server(server, domain):
    updated = 0
    created = 0
    # create a HttpServer object if not present
    if server is None:
        server = HttpServer.objects.create(domain=domain, name=domain)
        print "Server " + domain + " created in local database"
    pages = Page.objects.filter(server=server)
    # query main db
    filters = {"domain": domain}
    in_db_pages = R.get_filtered(domain, "pages", filters)
    urls = []
    if pages is not None:
        for page in pages:
            urls.append(page.url)
    for db_page in in_db_pages:
        url = db_page["uri"]
        if url in urls:
            page = pages.filter(url=url)[0]
            update_page(db_page, page)
            print "Page " + page.url + " updated"
            updated += 1
        else:
            add_page(db_page, server)
            print "Page " + page.url + " added"
            created += 1
    return updated, created
Exemplo n.º 7
0
 def dispatch(self, request, *args, **kwargs):
     if self.request.user.is_superuser is False or not self.request.method == "POST":
         raise Http404
     q = json.loads(self.request.POST['q'])
     self.time = None
     if "$delete" in q:
         self.results = R.delete(q['$db'], q["$table"], q["$delete"])
     else:
         if not '$table' in q:
             query = r.db(q['$db']).table_list()
             self.results = R.run_query(query)
         else:
             self.results, profile = R.run_json(q, profile=True)
             self.time = profile[0]['duration(ms)']
     if isinstance(self.results, r.Cursor):
         self.results = list(self.results)
     return super(RpostView, self).dispatch(request, *args, **kwargs)
Exemplo n.º 8
0
def document_exists(db, table, modelname, pk):
    q = r.db(db).table(table).filter({
        "pk": pk,
        "model": modelname
    }).order_by("timestamp").pluck("pk", "model")
    existing_documents = R.run_query(q)
    json_document_exists = False
    if len(existing_documents) > 0:
        json_document_exists = True
        return json_document_exists, existing_documents[0]
    return json_document_exists, None
Exemplo n.º 9
0
 def document_exists(self):
     domain = self.server.domain
     q = r.db(self.server.domain).table("pages").get_all(
         [self.url, domain], index="uri").count()
     #existing_documents = order_documents(R.run_query(q))
     #print modelname+" | "+str(pk)+" _> "+str(existing_documents)
     existing_documents = R.run_query(q)
     json_document_exists = False
     if existing_documents > 0:
         json_document_exists = True
     return json_document_exists
Exemplo n.º 10
0
 def get_context_data(self, **kwargs):
     context = super(RView, self).get_context_data(**kwargs)
     if self.request.user.is_superuser is False:
         raise Http404
     context['dbs'] = R.run_query(r.db_list())
     return context
Exemplo n.º 11
0
 def delete(self, *args, **kwargs):
     super(Page, self).delete(*args, **kwargs)
     filters = (r.row['domain'] == self.server.domain) & (r.row['uri']
                                                          == self.url)
     res = R.delete_filtered(self.server.domain, "pages", filters)
     return res