News Aggregator Tutorial: Chapter 7 (update existing articles)

Hi folks, I’m having a hard time getting things to work in Chapter 6. I prefer to handwrite the code rather than copy-paste, but even after giving up and copy-pasting everything, when I click the delete article button the same thing happens: the alert pops up asking if i want to delete the article (good), but when I click ‘yes’ nothing actually happens. Well, ok, the alert box goes away, but the article remains both on the page and in the database.

The tutorial was working fine through all the chapters so far (I did make a mistake here or there but was able to find and fix it on my own) til this one. I have no idea what I may be doing wrong. Any tips on where I should start?

Here’s my ArticleView page:

from ._anvil_designer import ArticleViewTemplate
from anvil import *
import anvil.server
import anvil.tables as tables
import anvil.tables.query as q
from anvil.tables import app_tables
from ..ArticleEdit import ArticleEdit

class ArticleView(ArticleViewTemplate):
  def __init__(self, **properties):
    # Set Form properties and Data Bindings.
    self.init_components(**properties)

    # Any code you write here will run when the form opens.

  def edit_article_button_click(self, **event_args):
    # Create a copy of the existing article from the Data Table 
    article_copy = dict(list(self.item))
    # Open an alert displaying the 'ArticleEdit' Form
    # set the `self.item` property of the ArticleEdit Form to a copy of the article to be updated
    save_clicked = alert(
      content=ArticleEdit(item=article_copy),
      title="Update Article",
      large=True,
      buttons=[("Save", True), ("Cancel", False)]
    )
    # Update the article if the user clicks save
    if save_clicked:
      anvil.server.call('update_article', self.item, article_copy)

      # Now refresh the page
      self.refresh_data_bindings()

  def delete_article_button_click(self, **event_args):
    """This method is called when the button is clicked"""
    if confirm("Are you sure you want to delete '{}'?".format(self.item['title'])):
      self.parent.raise_event('x-delete-article', article=self.item)

In the parent, is there ‘x-delete-article’?

Hi @Tony.Nguyen, this is what I’ve got going on in the Homepage…

from ._anvil_designer import HomepageTemplate
from anvil import *
import anvil.server
import anvil.tables as tables
import anvil.tables.query as q
from anvil.tables import app_tables
from ..ArticleEdit import ArticleEdit

class Homepage(HomepageTemplate):
  def __init__(self, **properties):
    # Set Form properties and Data Bindings.
    self.init_components(**properties)
    self.refresh_articles()
    self.articles_panel.set_event_handler('x-delete-article', self.delete_article)

    # Any code you write here will run when the form opens.

  def add_article_button_click(self, **event_args):
    """This method is called when the button is clicked"""
    
    new_article = {}
    
    save_clicked = alert(
      content = ArticleEdit(item=new_article), 
      title = "Add Article", 
      large = True,
      buttons = [("Save", True), ("Cancel", False)],
    )
    
    if save_clicked:
      anvil.server.call('add_article', new_article)
      
    self.refresh_articles()  
      
  def refresh_articles(self):
    self.articles_panel.items = anvil.server.call('get_articles')
    
  def delete_article(self, article, **event_args): 
    anvil.server.call('delete_article', article)
    self.refresh_articles()

I think this might be the relevant bit:
self.articles_panel.set_event_handler('x-delete-article', self.delete_article)

AFAIK, that’s pulled straight from the tutorial

Hi @ctinglin, how about the delete_article in the server?

Hi again @Tony.Nguyen (I much appreciate this help btw),

Here’s what my server module looks like (the relevant function at the bottom)…

import anvil.tables as tables
import anvil.tables.query as q
from anvil.tables import app_tables
import anvil.server
from datetime import datetime

@anvil.server.callable
def add_article(article_dict):
  app_tables.articles.add_row(
    created=datetime.now(), **article_dict
  )

@anvil.server.callable
def get_articles():
  return app_tables.articles.search(
  tables.order_by("created", ascending=False)
  )

@anvil.server.callable
def update_article(article, article_dict):
  # check that the article given is really a row in the ‘articles’ table
  if app_tables.articles.has_row(article):
    article_dict['updated'] = datetime.now()
    article.update(**article_dict)
  else:
    raise Exception("Article does not exist")
    
    
@anvil.server.callable
def delete_article(article):
  
  if app_tables.articles.has_row(article):
    article.delete
  else:
    raise Exception("Article does not exist")

Found it. SMH. So, first of all, it was Chapter 7, not 6 where I had the issue. And, specifically, I somehow forgot to add parentheses in my delete_article function.

Instead of

if app_tables.articles.has_row(article):
  article.delete

How about real python and go with

if app_tables.articles.has_row(article):
article.delete()

instead? :smiley:
Sheesh. Works like a charm now.

Thanks, @Tony.Nguyen, I appreciate your efforts. I’ll continue!

1 Like

Hi @ctinglin, I am so glad that you make it work.
Just make sure that every function works well and you will find issue.

Please feel free to post a new topic if you have any problem.

1 Like

2 posts were merged into an existing topic: Delete Article Button in Tutorial Multi-User Applications with Anvil