-1

When the DB is empty it should hit the first if condition and just return the error but for some reason, it executes the rest. Even when I comment out all the code after the if statement it executes all the code and returns an ICS file.

Somehow the rest of the code after the if is being saved elsewhere or the ICS file is stored somewhere and being returned every time.

from flask import Flask, request, Response, send_file, make_response
from flask_restx import Api, Resource, fields, reqparse
from ics import Calendar, Event

@api.route('/calendar')
class ICSCalendar(Resource):

    @api.response(200, 'Successfully Retrieved Calendar')
    @api.response(404, 'No Events Found')
    @api.doc(description="Get all events in an ``ICS`` calendar format")
    def get(self):
        '''Get all events in an ICS calendar format'''
        events = execute_query("SELECT * FROM events")
        print(events)
        if not events:
            return {"Error": "No events found"}, 404
        cal = Calendar()
        for row in events:
            event = Event()
            event.name = row[1]
            event.begin = pytz.timezone('Australia/Sydney').localize(datetime.combine(datetime.strptime(row[2], '%Y-%m-%d').date(), datetime.strptime(row[3], '%H:%M:%S').time()))
            event.end = pytz.timezone('Australia/Sydney').localize(datetime.combine(datetime.strptime(row[2], '%Y-%m-%d').date(), datetime.strptime(row[4], '%H:%M:%S').time()))
            event.location = f"{row[5]}, {row[6]}, {row[7]} {row[8]}"
            event.description = row[9]
            event.last_modified = pytz.utc.localize(datetime.strptime(row[10], '%Y-%m-%d %H:%M:%S'))
            cal.events.add(event)

        # Write the ICS data to an in-memory buffer
        buffer = StringIO(cal.serialize(), newline=None)
        buffer.seek(0)
        # Create a response with the ICS data as an attachment
        response = make_response(send_file(buffer, as_attachment=True, attachment_filename='calendar.ics', mimetype='text/calendar'))
        response.headers['Content-Disposition'] = 'attachment; filename=calendar.ics'
        buffer.truncate(0)
        return response`

@api.route('/calendar')
class ICSCalendar(Resource):

    @api.response(200, 'Successfully Retrieved Calendar')
    @api.response(404, 'No Events Found')
    @api.doc(description="Get all events in an ``ICS`` calendar format")
    def get(self):
        '''Get all events in an ICS calendar format'''
        events = execute_query("SELECT * FROM events")
        print(events)
        if not events:
            return {"Error": "No events found"}, 404

I've set the DB as empty and only used the above code but it returns an ICS file.

I only want it to return an ICS file when there is content in events.

Does anyone have any ideas why this is returning ICS data when it shouldn't?

M Z
  • 4,571
  • 2
  • 13
  • 27

1 Answers1

0

1- events is an object so you can't use if not events: statement. Since this object actually exists. You should have used something like below for example;

if events.get("...") is None: # if it is dict type object

Please look at what type object it is and adapt to it. For example, if it is kinda .json object, then it can have keys but values can be "" or None then below code block won't have an affect, so that an ICS file will return.

if not events:
    return {"Error": "No events found"}, 404

2- When you commented out all code just after if statement, it mustn't be returning an ICS file actually. It might have been cached somehow, this is why it might be returning.

Thanks.

Eren Sakarya
  • 150
  • 8