def install(): logging.error(">> installing post") params = { "name":"Hello world!", "slug":"hello-world", "plain_description":"Welcome to ContentQ CMS. This is your first post. Edit or delete it, then start blogging!", "description":"Welcome to ContentQ CMS. This is your first post. Edit or delete it, then start blogging!", "status":"published", "tags":[], "meta_desc":"hello world", "category":"uncategorized", "body":"<p>this is an example content</p>" } post_ref = PostItem(**params) post_ref.put() category_ref = PostCategory(name='Uncategorized', slug='uncategorized') category_ref.put() latestpost_ref = Block.add_model('latestpost', 'blog.blocks.LatestPostBlock') postcategories_ref = Block.add_model('postcategories', 'blog.blocks.PostCategoriesBlock') block = Block(name='Post Categories', slug='post-categories', position='rightsidebar', model='blog.blocks.PostCategoriesBlock', args={}) block.save()
def install(): logging.info("> common.config.install") staticblock_ref = Block.add_model('staticblock', 'common.blocks.StaticBlock') block = Block(name='Main Menu', slug='mainmenu', position='mainmenu', model='common.blocks.MenuBlock', args={'name':'mainmenu'}, active=True) block.save() block = Block(name='Administration', slug='admin-sidebar', position='adminsidebar', model='common.blocks.AdminMenuBlock', args={}, active=True) block.save() block = Block(name='Usermenu', slug='usermenu', position='header', model='common.blocks.UserMenuBlock', args={}, active=True) block.save() logging.info(" creating mainmenu") menu = Menu(name='mainmenu', slug='mainmenu', position='mainmenu', block=block.uuid) menu.save() menuitem = MenuItem(name='Home', slug='home', link='/', menu=menu.slug) menuitem.save() anonymous_ref = Role(name='anonymous', core=True, order=0) anonymous_ref.save() authenticated_ref = Role(name='authenticated', core=True, order=1) authenticated_ref.save() anonymous_ref = Role(name='administrator', core=True, order=5) anonymous_ref.save()
def blocks_config(request, uuid): block_ref = Block.get(uuid=uuid) model = Block form_class = block_ref.get_block_form() form = form_class(instance=block_ref) if request.method == 'POST': form = form_class(request.POST, instance=block_ref) if form.is_valid(): block_ref = form.save() util.success(request, "Config saved successfully") return http.HttpResponseRedirect(Block.admin_url()) c = template.RequestContext(request, locals()) return render_to_response('blocks_config.html', c)
def __init__(self, *args, **kwargs): super(BlockNewForm, self).__init__(*args, **kwargs) self.fields['menus'].widget = forms.RadioSelect(choices=settings.BLOCK_MENUS_OPTION.items(), attrs={'class':'radioselect'}) attrs={'disabled':'disabled'} self.fields['visibility'].widget = SelectMultiple(choices=MenuItem.get_choices(), attrs=attrs) self.fields['model'].widget = forms.Select(choices=Block.get_models_choices())
def add_genesis_block(self): addresses = settings.FIRST_ADDRESSES addresses_size = len(addresses) block = Block() for i in range(5): # create the first 5 transactions destination_address = addresses[ i % addresses_size] # if there's less than 5 new_transaction = self.wallet.create_transaction( destination_address, settings.FIRST_BALANCE) block.add_transaction(new_transaction) transactions_string = block.get_string_of_transactions() nonce = 0 found = False while not found: # hashing hash_object = hashlib.sha256( str.encode( transactions_string + str(nonce))) # b allows to concert string to binary block_header = hash_object.hexdigest() if block_header[:settings. DIFFICULTY] == "0" * settings.DIFFICULTY: found = True else: nonce += 1 block.header = block_header block.nonce = nonce self.add_block(block)
def save_block(self): logging.info("****** MenuForm.save_block") params = {'name':self.cleaned_data['name'], 'slug':unicode(slugify(self.cleaned_data['name'])), 'position':self.cleaned_data['position'], 'model':'common.blocks.MenuBlock', 'args':{}} logging.info(" params: %s" % params) if self.instance: block = Block.get(uuid=self.instance.block) else: block = Block(**params) logging.info(" block: %s" % block) logging.info(" block.uuid: %s" % block.uuid) block.position = self.cleaned_data['position'] block.put() return block
def render(self, context): request = self.request.resolve(context) blocks = Block.get_by_position(self.position, request) res = '' try: for block in blocks: res = res + self.render_block(block, context) except: logging.error("error rendering blocks from: %s" % self.position) return res
def post(self, request): # TODO make sure that only master node can request this try: block = Block.parse(request.data) except ParseException as e: return Response({'errors': str(e)}, status=status.HTTP_406_NOT_ACCEPTABLE) logger.debug("Adding block '%s' to blockchain" % block.header) updated = self.server.update_blockchain(block) # TODO test when the blockchain is not updated if not updated: logger.debug("Blockchain not up to date, requesting part of.") response = client.get( settings.MASTER_IP, 'blockchain?start=%d' % (self.server.blockchain_size - 1)) blockchain = Blockchain.parse(response.data) self.server.add_blocks(blockchain.blocks) return Response(status=status.HTTP_201_CREATED)
def blocks_edit(request, uuid): return content_edit(request, uuid, 'blocks', Block, BlockForm, tpl='blocks_edit.html', redirect_to=Block.admin_url())
def blocks_unpublish(request, uuid): block_ref = Block.get(uuid=uuid) block_ref.unpublish() return http.HttpResponseRedirect(Block.admin_url())
def blocks_new(request): return content_new(request, 'blocks', BlockNewForm, redirect_to=Block.admin_url(), model=Block, tpl='blocks_new.html',)
def render(cls, context, name=""): logging.info(">> StaticBlock.render") block_ref = BlockModel.get(slug=name) context.update({"params": block_ref.params}) return cls.render_block(template_name="block_static.html", block_title=_(block_ref.name), context=context)
def hardcoded_genesis_block(self): log.debug("Initializing genesis block (from file %s)" % settings.GENESIS_BLOCK_FILE) with open(settings.GENESIS_BLOCK_FILE, 'r') as f: data = json.load(f) self.add_block(Block.parse(data))
def post(self, request): """ Manage the POST request, the Master Node will receive a block and it will check wether it is accepted or not and will add it to the rest of the blockchain accordingly only if the one requesting it is a Relay Node (see user and password above). If the block is rejected because of bad transactions, those transactions are returned to the relay node that made the request. If the block is accepted, the new block is sent to all relay nodes. """ try: # request contains the block, and the address of the miner logger.debug("Block received from %s" % request.data['miner_address']) block_data = request.data['block'] block = Block.parse(block_data) except KeyError: logger.debug("No block given.") return Response({"errors": "No block given."}, status=status.HTTP_406_NOT_ACCEPTABLE) except ParseException as e: logger.debug("Parsing block error.") return Response({"errors": "%s" % e}, status=status.HTTP_406_NOT_ACCEPTABLE) (hash_verify, bad_transactions) = self.server.update_blockchain(block) if hash_verify and len(bad_transactions) == 0: # block is valid logger.debug("Block '%s' successfully added" % block.header) data = {'transactions': []} for transaction in block.transactions: data['transactions'].append(Transaction.serialize(transaction)) for relay_ip in settings.RELAY_IP: logger.debug("Sending block '%s' to relay %s" % (block.header, relay_ip)) client.post(relay_ip, 'blockchain', block_data) client.delete(relay_ip, 'transactions', data) if self.server.balance >= settings.REWARD: self.server.balance -= settings.REWARD miner_transaction = self.server.wallet.create_transaction( request.data['miner_address'], settings.REWARD) client.post(relay_ip, 'transactions', Transaction.serialize(miner_transaction)) response = Response({"detail": "Block successfully added!"}, status=status.HTTP_201_CREATED) else: logger.debug("Block '%s' can NOT be added (bad header or bad TXs)." % block.header) data = {'transactions': []} if len(bad_transactions) > 0: for transaction in bad_transactions: logger.debug("Bad TX '%s'" % transaction.hash) data['transactions'].append(Transaction.serialize(transaction)) # Send to all relays bad TXs, since the miner can request # transactions from any relay for relay_ip in settings.RELAY_IP: logger.debug("Sending bad TXs to relay %s" % relay_ip) client.delete(relay_ip, 'transactions', data) response = Response({"errors": "Bad transactions where found in new block.", "data": data}, status=status.HTTP_406_NOT_ACCEPTABLE) else: response = Response({"errors": "Bad header.", "data": block_data}, status=status.HTTP_406_NOT_ACCEPTABLE) return response