Implementation of Sponsors API in Open Event Organizer Android App

New contributors to this project are sometimes not experienced with the set of libraries and MVP pattern which this app uses. This blog post is an attempt to walk a new contributor through some parts of the code of the app by implementing an operation for an endpoint of the API. We’ll be implementing the sponsor endpoint. Open Event Organizer Android app uses a robust architecture. It is presently using the MVP (Model-View-Presenter) architecture. Therefore, this blog post aims at giving some brief insights to the app architecture through the implementation Sponsor endpoint. This blog post will focus only on one operation of the endpoint - the list operation - so as to make the post short enough. This blog post relates to Pull Request #901 of Open Event Organizer App. Project structure: These are the parts of the project structure where major updates will be made for the implementation of Sponsor endpoint: core data Setting up elements in the data module for the respective endpoint Sponsor.java @Data @Builder @Type("sponsor") @AllArgsConstructor @JsonNaming(PropertyNamingStrategy.KebabCaseStrategy.class) @EqualsAndHashCode(callSuper = false, exclude = { "sponsorDelegate", "checking" }) @Table(database = OrgaDatabase.class) public class Sponsor extends AbstractItem<Sponsor, SponsorsViewHolder> implements Comparable<Sponsor>, HeaderProvider {    @Delegate(types = SponsorDelegate.class)    private final SponsorDelegateImpl sponsorDelegate = new         SponsorDelegateImpl(this); This class uses Lombok, Jackson, RaizLabs-DbFlow, extends AbstractItem class (from Fast Adapter) and implements Comparable and HeaderProvider. All the annotations processor help us reduce boilerplate code. From the Lombok plugin, we are using: Lombok has annotations to generate Getters, Setters, Constructors, toString(), Equal() and hashCode() methods. Thus, it is very efficient in reducing boilerplate code @Getter,  @Setter, @ToString, @EqualsAndHashCode @Data is a shortcut annotation that bundles the features of @Getter, @Setter, @ToString and @EqualsAndHashCode The @Delegate is used for direct calls to the methods that are annotated with it, to the specified delegate. It basically separates the model class from other methods which do not pertain to data. Jackson @JsonNaming - used to choose the naming strategies for properties in serialization, overriding the default. For eg:  KEBAB_CASE, LOWER_CASE, SNAKE_CASE, UPPER_CAMEL_CASE @JsonNaming(PropertyNamingStrategy.KebabCaseStrategy.class) @JsonProperty - used to store the variable from JSON schema as the given variable name. So, "type" from JSON will be stored as sponsorType. @JsonProperty("type") public String sponsorType; RaizLabs-DbFlow DbFlow uses model classes which must be annotated using the annotations provided by the library. The basic annotations are – @Table, @PrimaryKey, @Column, @ForeignKey etc. These will create a table named attendee with the columns and the relationships annotated. SponsorDelegate.java and SponsorDelegateImpl.java The above are required only for method declarations of the classes and interfaces that Sponsor.java extends or implements. These basically separate the required method overrides from the base item class. public class SponsorDelegateImpl extends AbstractItem<Sponsor, SponsorsViewHolder> implements SponsorDelegate { SponsorRepository.java and SponsorRepositoryImpl.java A Repository mediates between the domain and data mapping layers, acting like an in-memory domain object collection. Client objects construct query specifications declaratively and submit them to Repository for satisfaction. Objects can be added to and removed from the Repository, as they can from a simple collection of objects, and the mapping code encapsulated by the…

Continue ReadingImplementation of Sponsors API in Open Event Organizer Android App

Open Event Server – Export Attendees as CSV File

FOSSASIA‘s Open Event Server is the REST API backend for the event management platform, Open Event. Here, the event organizers can create their events, add tickets for it and manage all aspects from the schedule to the speakers. Also, once he/she makes his event public, others can view it and buy tickets if interested. The organizer can see all the attendees in a very detailed view in the event management dashboard. He can see the statuses of all the attendees. The possible statuses are completed, placed, pending, expired and canceled, checked in and not checked in. He/she can take actions such as checking in the attendee. If the organizer wants to download the list of all the attendees as a CSV file, he or she can do it very easily by simply clicking on the Export As and then on CSV. Let us see how this is done on the server. Server side - generating the Attendees CSV file Here we will be using the csv package provided by python for writing the csv file. import csv We define a method export_attendees_csv which takes the attendees to be exported as a CSV file as the argument. Next, we define the headers of the CSV file. It is the first row of the CSV file. def export_attendees_csv(attendees):   headers = ['Order#', 'Order Date', 'Status', 'First Name', 'Last Name', 'Email',              'Country', 'Payment Type', 'Ticket Name', 'Ticket Price', 'Ticket Type'] A list is defined called rows. This contains the rows of the CSV file. As mentioned earlier, headers is the first row. rows = [headers] We iterate over each attendee in attendees and form a row for that attendee by separating the values of each of the columns by a comma. Here, every row is one attendee. The newly formed row is added to the rows list. for attendee in attendees:   column = [str(attendee.order.get_invoice_number()) if attendee.order else '-',             str(attendee.order.created_at) if attendee.order and attendee.order.created_at else '-',             str(attendee.order.status) if attendee.order and attendee.order.status else '-',             str(attendee.firstname) if attendee.firstname else '',             str(attendee.lastname) if attendee.lastname else '',             str(attendee.email) if attendee.email else '',             str(attendee.country) if attendee.country else '',             str(attendee.order.payment_mode) if attendee.order and attendee.order.payment_mode else '',             str(attendee.ticket.name) if attendee.ticket and attendee.ticket.name else '',             str(attendee.ticket.price) if attendee.ticket and attendee.ticket.price else '0',             str(attendee.ticket.type) if attendee.ticket and attendee.ticket.type else '']   rows.append(column) rows contains the contents of the CSV file and hence it is returned. return rows We iterate over each item of rows and write it to the CSV file using the methods provided by the csv package. writer = csv.writer(temp_file) from app.api.helpers.csv_jobs_util import export_attendees_csv content = export_attendees_csv(attendees) for row in content:   writer.writerow(row) Obtaining the Attendees CSV file: Firstly, we have an API endpoint which starts the task on the server. GET - /v1/events/{event_identifier}/export/attendees/csv Here, event_identifier is the unique ID of the event. This endpoint starts a celery task on the server to export the attendees of the event as a CSV file. It returns the URL of the task to get the status of the export task. A sample response is as follows:…

Continue ReadingOpen Event Server – Export Attendees as CSV File

Open Event Server – Export Orders as CSV File

FOSSASIA‘s Open Event Server is the REST API backend for the event management platform, Open Event. Here, the event organizers can create their events, add tickets for it and manage all aspects from the schedule to the speakers. Also, once he/she makes his event public, others can view it and buy tickets if interested. The organizer can see all the orders in a very detailed view in the event management dashboard. He can see the statuses of all the orders. The possible statuses are completed, placed, pending, expired and canceled. If the organizer wants to download the list of all the orders as a CSV file, he or she can do it very easily by simply clicking on the Export As and then on CSV. Let us see how this is done on the server. Server side - generating the Orders CSV file Here we will be using the csv package provided by python for writing the csv file. import csv We define a method export_orders_csv which takes the orders to be exported as a CSV file as the argument. Next, we define the headers of the CSV file. It is the first row of the CSV file. def export_orders_csv(orders):   headers = ['Order#', 'Order Date', 'Status', 'Payment Type', 'Total Amount', 'Quantity',              'Discount Code', 'First Name', 'Last Name', 'Email'] A list is defined called rows. This contains the rows of the CSV file. As mentioned earlier, headers is the first row. rows = [headers] We iterate over each order in orders and form a row for that order by separating the values of each of the columns by a comma. Here, every row is one order. The newly formed row is added to the rows list. for order in orders:   if order.status != "deleted":       column = [str(order.get_invoice_number()), str(order.created_at) if order.created_at else '',                 str(order.status) if order.status else '', str(order.paid_via) if order.paid_via else '',                 str(order.amount) if order.amount else '', str(order.get_tickets_count()),                 str(order.discount_code.code) if order.discount_code else '',                 str(order.user.first_name)                 if order.user and order.user.first_name else '',                 str(order.user.last_name)                 if order.user and order.user.last_name else '',                 str(order.user.email) if order.user and order.user.email else '']       rows.append(column) rows contains the contents of the CSV file and hence it is returned. return rows We iterate over each item of rows and write it to the CSV file using the methods provided by the csv package. writer = csv.writer(temp_file) from app.api.helpers.csv_jobs_util import export_orders_csv content = export_orders_csv(orders) for row in content:   writer.writerow(row) Obtaining the Orders CSV file: Firstly, we have an API endpoint which starts the task on the server. GET - /v1/events/{event_identifier}/export/orders/csv Here, event_identifier is the unique ID of the event. This endpoint starts a celery task on the server to export the orders of the event as a CSV file. It returns the URL of the task to get the status of the export task. A sample response is as follows: {  "task_url": "/v1/tasks/b7ca7088-876e-4c29-a0ee-b8029a64849a" }</span The user can go to the above-returned URL and check the status of his/her Celery task. If the task completed successfully he/she will get the download URL. The endpoint…

Continue ReadingOpen Event Server – Export Orders as CSV File

Handling No internet cases in Open Event Android

It’s pretty common to face connectivity issues and when the user has no Internet connection he should be shown an appropriate response rather than allowing him to send requests to the server. Let’s have a look how we are handling such cases in Open Event Android Firstly we need to add the required permission in the manifest. We need the permission to access the user’s WiFi state and network state. <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />   We use this function to check if the user is connected to the Internet. This function return a Boolean which is true if the user is connected to the Internet otherwise it is false private fun isNetworkConnected(): Boolean { val connectivityManager = context?.getSystemService(Context.CONNECTIVITY_SERVICE) as? ConnectivityManager return connectivityManager?.activeNetworkInfo != null }   This function is used to decide which screen should be shown to the user. If the user has an active Internet connection he will see events fragment but if there is no Internet he will see the no Internet card. private fun showNoInternetScreen(show: Boolean) { rootView.homeScreenLL.visibility = if (show) View.VISIBLE else View.GONE rootView.noInternetCard.visibility = if (!show) View.VISIBLE else View.GONE }   Let’s see how the above two functions are used in the events fragment. When the app starts we check if there is a need to show the no Internet screen. If the user is not connected to the Internet, the no Internet card will be shown. Then when the user clicks on retry, the events fragment is shown again if the user is connected to the Internet. showNoInternetScreen(isNetworkConnected()) rootView.retry.setOnClickListener { showNoInternetScreen(isNetworkConnected()) }   Let’s have a look a how the XML code looks, here we are only seeing a part of the code as the rest is pretty obvious. We have cardView and inside it all the views ie ImageView,TextView are inside a LinearLayout which has a vertical orientation so that all these views appear below each other. <android.support.v7.widget.CardView android:id="@+id/noInternetCard" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="@dimen/layout_margin_medium" app:cardBackgroundColor="@color/white" app:cardCornerRadius="@dimen/card_corner_radius" app:cardElevation="@dimen/card_elevation"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="@dimen/layout_margin_extra_large" android:orientation="vertical"> <ImageView android:id="@+id/noInternetImageView" android:layout_width="@dimen/item_image_view_large" android:layout_height="@dimen/item_image_view_large" android:layout_gravity="center_horizontal" android:layout_marginTop="@dimen/layout_margin_large" android:scaleType="centerCrop" app:srcCompat="@drawable/ic_no_internet" /> <TextView android:id="@+id/noInternetTextview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginTop="@dimen/layout_margin_large" android:text="@string/no_internet_message" android:textSize="@dimen/text_size_medium" tools:text="No Internet" />   References AndroidHive tutorial - https://www.androidhive.info/2012/07/android-detect-internet-connection-status/ Official Android Documentation - https://developer.android.com/training/monitoring-device-state/connectivity-monitoring StackOverflow - https://stackoverflow.com/questions/9570237/android-check-internet-connection

Continue ReadingHandling No internet cases in Open Event Android

Open Event Server – Export Event as a Pentabarf XML File

FOSSASIA‘s Open Event Server is the REST API backend for the event management platform, Open Event. Here, the event organizers can create their events, add tickets for it and manage all aspects from the schedule to the speakers. Also, once he makes his event public, others can view it and buy tickets if interested. To make event promotion easier, we also provide the event organizer to export his event as a Pentabarf XML file. Pentabarf XML is used to store events/conferences in a format which most of the scheduling applications can read and add that particular event/conference to the user’s schedule. Server side - generating the Pentabarf XML file Here we will be using the pentabarf package for Python for parsing and creating the file. from pentabarf.Conference import Conference from pentabarf.Day import Day from pentabarf.Event import Event from pentabarf.Person import Person from pentabarf.Room import Room We define a class PentabarfExporter which has a static method export(event_id). Query the event using the event_id passed and start forming the event in the required format: event = EventModel.query.get(event_id) diff = (event.ends_at - event.starts_at) conference = Conference(title=event.name, start=event.starts_at, end=event.ends_at,                       days=diff.days if diff.days > 0 else 1,                       day_change="00:00", timeslot_duration="00:15",                       venue=event.location_name) dates = (db.session.query(cast(Session.starts_at, DATE))        .filter_by(event_id=event_id)        .filter_by(state='accepted')        .filter(Session.deleted_at.is_(None))        .order_by(asc(Session.starts_at)).distinct().all()) We have queried for the dates of the event and saved it in dates. We will now iterate over each date and query the microlocations who have a session on that particular date. for date in dates:   date = date[0]   day = Day(date=date)   microlocation_ids = list(db.session.query(Session.microlocation_id)                            .filter(func.date(Session.starts_at) == date)                            .filter_by(state='accepted')                            .filter(Session.deleted_at.is_(None))                            .order_by(asc(Session.microlocation_id)).distinct()) For each microlocation thus obtained, we will query for accepted sessions to be held at those microlocations. We will also initialize a Room for each microlocation. for microlocation_id in microlocation_ids:   microlocation_id = microlocation_id[0]   microlocation = Microlocation.query.get(microlocation_id)   sessions = Session.query.filter_by(microlocation_id=microlocation_id) \       .filter(func.date(Session.starts_at) == date) \       .filter_by(state='accepted') \       .filter(Session.deleted_at.is_(None)) \       .order_by(asc(Session.starts_at)).all()   room = Room(name=microlocation.name) We will now iterate over the aabove-obtained sessions and instantiate an Event for each session. Then we will iterate over all the speakers of that session and instantiate a Person for each speaker. Finally, we will add that Event to the Room we created earlier. for session in sessions:   session_event = Event(id=session.id,                         date=session.starts_at,                         start=session.starts_at,                         duration=str(session.ends_at - session.starts_at) + "00:00",                         track=session.track.name,                         abstract=session.short_abstract,                         title=session.title,                         type='Talk',                         description=session.long_abstract,                         conf_url=url_for('event_detail.display_event_detail_home',                                          identifier=event.identifier),                         full_conf_url=url_for('event_detail.display_event_detail_home',                                               identifier=event.identifier, _external=True),                         released="True" if event.schedule_published_on else "False")   for speaker in session.speakers:       person = Person(id=speaker.id, name=speaker.name)       session_event.add_person(person)   room.add_event(session_event) Then we will add the room to the day and then add each day to the conference. day.add_room(room) conference.add_day(day) Finally, we will call the generate method of the conference to generate the XML file. This can be directly written to the file. return conference.generate("Generated by " + get_settings()['app_name']) Obtaining the Pentabarf XML file: Firstly, we have an API endpoint which starts the task on the server. GET - /v1/events/{event_identifier}/export/pentabarf Here, event_identifier is the unique ID of the event. This endpoint starts a celery task on the server to export the event as a Pentabarf XML file. It returns the task of the URL to get the status of the export task.…

Continue ReadingOpen Event Server – Export Event as a Pentabarf XML File

Open Event Server – Export Event as xCalendar File

FOSSASIA‘s Open Event Server is the REST API backend for the event management platform, Open Event. Here, the event organizers can create their events, add tickets for it and manage all aspects from the schedule to the speakers. Also, once he makes his event public, others can view it and buy tickets if interested. To make event promotion easier, we also provide the event organizer to export his event as an xCalendar file. xCal is an XML representation of the iCalendar standard. xCal is not an alternative nor next generation of iCalendar. xCal represents iCalendar components, properties, and parameters as defined in iCalendar. This format was selected to ease its translation back to the iCalendar format using an XSLT transform. Server side - generating the xCal file Here we will be using the xml.etree.ElementTree package for Python for parsing and creating XML data. from xml.etree.ElementTree import Element, SubElement, tostring We define a class XCalExporter which has a static method export(event_id). Query the event using the event_id passed and start forming the calendar: event = Event.query.get(event_id) tz = event.timezone or 'UTC' tz = pytz.timezone(tz) i_calendar_node = Element('iCalendar') i_calendar_node.set('xmlns:xCal', 'urn:ietf:params:xml:ns:xcal') v_calendar_node = SubElement(i_calendar_node, 'vcalendar') version_node = SubElement(v_calendar_node, 'version') version_node.text = '2.0' prod_id_node = SubElement(v_calendar_node, 'prodid') prod_id_node.text = '-//fossasia//open-event//EN' cal_desc_node = SubElement(v_calendar_node, 'x-wr-caldesc') cal_desc_node.text = "Schedule for sessions at " + event.name cal_name_node = SubElement(v_calendar_node, 'x-wr-calname') cal_name_node.text = event.name We query for the accepted sessions of the event and store it in sessions sessions = Session.query \   .filter_by(event_id=event_id) \   .filter_by(state='accepted') \   .filter(Session.deleted_at.is_(None)) \   .order_by(asc(Session.starts_at)).all() We then iterate through all the sessions in sessions. If it is a valid session, we instantiate a SubElement and store required details v_event_node = SubElement(v_calendar_node, 'vevent') method_node = SubElement(v_event_node, 'method') method_node.text = 'PUBLISH' uid_node = SubElement(v_event_node, 'uid') uid_node.text = str(session.id) + "-" + event.identifier dtstart_node = SubElement(v_event_node, 'dtstart') dtstart_node.text = tz.localize(session.starts_at).isoformat() …. So on We then loop through all the speakers in that particular session and add it to the xCal calendar node object as well. for speaker in session.speakers:   attendee_node = SubElement(v_event_node, 'attendee')   attendee_node.text = speaker.name And finally, the string of the calendar node is returned. This is the xCalendar file contents. This can be directly written to a file. return tostring(i_calendar_node) Obtaining the xCal file: Firstly, we have an API endpoint which starts the task on the server. GET - /v1/events/{event_identifier}/export/xcal Here, event_identifier is the unique ID of the event. This endpoint starts a celery task on the server to export the event as an xCal file. It returns the URL of the task to get the status of the export task. A sample response is as follows: {  "task_url": "/v1/tasks/b7ca7088-876e-4c29-a0ee-b8029a64849a" } The user can go to the above-returned URL and check the status of his Celery task. If the task completed successfully he will get the download URL. The endpoint to check the status of the task is: and the corresponding response from the server - {  "result": {    "download_url": "/v1/events/1/exports/http://localhost/static/media/exports/1/zip/OGpMM0w2RH/event1.zip"  },  "state": "SUCCESS" } The file can be downloaded from the above mentioned URL.…

Continue ReadingOpen Event Server – Export Event as xCalendar File

Integrating Stripe OAuth in Open Event Frontend

Why is Stripe Oauth needed in frontend? Open event allows organizers to add tickets and accepts payments for tickets through various modes for example, Credit card, Debit card, Netbanking and offline payments. Stripe allows users to accept payments into their linked accounts on various online platforms after they provide client secret and publishable key. So to enable online payments in open event, organizers were required to authenticate their stripe account. This is done through Stripe OAuth. Flow of OAuth To allow organizers to link their stripe account admin has to enable stripe under payment gateway in admin settings. Admin provides his client ID and secret key. Admin also sets the redirect URL for his app on the stripe dashboard. After enabling these settings organizer will see an option to link their stripe account to open event when they are creating an event with paid tickets. Here is what open event frontend does when we click connect to stripe button: Opens a popup to allow organizer to fill his stripe credentials and authorize open event app to access their secret and publishable key. Once the organizer fills his credentials and authorizes open event app, open event frontend fetches organizers auth code and saves it to server. Server on receiving auth code from frontend makes a request to stripe using the auth code to retrieve the publishable key and secret key. Once these are fetched server saves this information against the event so that all payments for that event can go to the linked stripe account. Implementing the Frontend portion: Choosing the library: After looking at various libraries that support OAuth for Ember applications we decided to use Torii. Torii is the library that allows the addition of OAuth for various social apps such as Facebook, Google and Stripe too. It allows writing a custom provider for OAuth in case we do not want to use clients for which torii provides supports by default. Implementing Stripe Provider: Default provider for stripe given by torii fetched the client ID and redirect URL from environment.js file. But since in open event we have already saved client id of admin in our database so we will extend default stripe provider and modify its client Id so that it fetches client id from server. Code for extending default provider is given here: import stripeConnect from 'torii/providers/stripe-connect'; import { alias } from '@ember/object/computed'; import { inject } from '@ember/service'; import { configurable } from 'torii/configuration'; function currentUrl() { let url = [window.location.protocol, '//', window.location.host].join(''); if (url.substr(-1) !== '/') { url += '/'; } return url; } export default stripeConnect.extend({ settings: inject(), clientId: alias('settings.stripeClientId'), redirectUri: configurable('redirectUri', function() { return `${currentUrl()}torii/redirect.html`; }) });   We have fetched clientId from our settings service as alias('settings.stripeClientId'). We have already defined settings in our services so we just need to inject the service here to be able to use it. By default torii provides redirect url as {currentUrl}/torii/redirect.html. But in open event frontend we allow organizers to edit information on two routes…

Continue ReadingIntegrating Stripe OAuth in Open Event Frontend

Open Event Server – Export Event as an iCalendar File

FOSSASIA‘s Open Event Server is the REST API backend for the event management platform, Open Event. Here, the event organizers can create their events, add tickets for it and manage all aspects from the schedule to the speakers. Also, once he makes his event public, others can view it and buy tickets if interested. To make event promotion easier, we also provide the event organizer to export his event as an iCalendar file. Going by the Wikipedia definition, iCalendar is a computer file format which allows Internet users to send meeting requests and tasks to other Internet users by sharing or sending files in this format through various methods. The files usually have an extension of .ics. With supporting software, such as an email reader or calendar application, recipients of an iCalendar data file can respond to the sender easily or counter propose another meeting date/time. The file format is specified in a proposed internet standard (RFC 5545) for calendar data exchange. Server side - generating the iCal file Here we will be using the icalendar package for Python as the file writer. from icalendar import Calendar, vCalAddress, vText We define a class ICalExporter which has a static method export(event_id). Query the event using the event_id passed and start forming the calendar: event = EventModel.query.get(event_id) cal = Calendar() cal.add('prodid', '-//fossasia//open-event//EN') cal.add('version', '2.0') cal.add('x-wr-calname', event.name) cal.add('x-wr-caldesc', "Schedule for sessions at " + event.name) We query for the accepted sessions of the event and store it in sessions. sessions = Session.query \   .filter_by(event_id=event_id) \   .filter_by(state='accepted') \   .filter(Session.deleted_at.is_(None)) \   .order_by(asc(Session.starts_at)).all() We then iterate through all the sessions in sessions. If it is a valid session, we instantiate an icalendar event and store required details. event_component = icalendar.Event() event_component.add('summary', session.title) event_component.add('uid', str(session.id) + "-" + event.identifier) event_component.add('geo', (event.latitude, event.longitude)) event_component.add('location', session.microlocation.name or '' + " " + event.location_name) event_component.add('dtstart', tz.localize(session.starts_at)) event_component.add('dtend', tz.localize(session.ends_at)) event_component.add('email', event.email) event_component.add('description', session.short_abstract) event_component.add('url', url_for('event_detail.display_event_detail_home',                                  identifier=event.identifier, _external=True)) We then loop through all the speakers in that particular session and add it to the iCal Event object as well. for speaker in session.speakers:   # Ref: http://icalendar.readthedocs.io/en/latest/usage.html#file-structure   # can use speaker.email below but privacy reasons   attendee = vCalAddress('MAILTO:' + event.email if event.email else 'undefined@email.com')   attendee.params['cn'] = vText(speaker.name)   event_component.add('attendee', attendee) This event_component is then added to the cal object that we created in the beginning. cal.add_component(event_component) And finally, the cal.to_ical() is returned. This is the iCalendar file contents. This can be directly written to a file. return cal.to_ical() Obtaining the iCal file: Firstly, we have an API endpoint which starts the task on the server. GET - /v1/events/{event_identifier}/export/ical Here, event_identifier is the unique ID of the event. This endpoint starts a celery task on the server to export the event as an iCal file. It returns the task of the URL to get the status of the export task. A sample response is as follows: {  "task_url": "/v1/tasks/b7ca7088-876e-4c29-a0ee-b8029a64849a" } The user can go to the above returned URL and check the status of his Celery task. If the task completed successfully he will get…

Continue ReadingOpen Event Server – Export Event as an iCalendar File

Open Event Frontend – Implement Access Event API via REST API

FOSSASIA‘s Open Event Frontend uses the Open Event Server as the REST API backend. The user can create an event using the Frontend. He can add sessions, tickets speakers etc. and all this updates the database tables in Open Event Server. The server provides certain endpoints for the user to access and/or update the information. It is important that the user is aware of the expected response from the server for his API request. Let’s see how this is displayed in the frontend. In the event-view page of the frontend, which is accessible to the organizers, there is an Export tab, along with Overview, Tickets, Scheduler, Sessions, Speakers. This tab has an Access Event Information via REST API section which displays the URL to be used by the user and the expected response. It looks as follows : The user can choose between various options which he can include or exclude. The GET URL is modified accordingly and the appropriate response is shown to the user. Example of this - How is this implemented in Code? We maintain two variables baseUrl and displayUrl to display the URL. baseUrl is the URL which is common in all requests, ie, till the include tag. baseUrl: computed('eventId', function() { return `${`${ENV.APP.apiHost}/${ENV.APP.apiNamespace}/events/`}${this.get('eventId')}`; }) displayUrl is the variable which stores the URL being displayed on the webpage. It is initialized to the same as baseUrl. displayUrl: computed('eventId', function() { return `${`${ENV.APP.apiHost}/${ENV.APP.apiNamespace}/events/`}${this.get('eventId')}`; }) To store the value of the toggle switches we use toggleSwitches as follows: toggleSwitches: { sessions       : false, microlocations : false, tracks         : false, speakers       : false, sponsors       : false, tickets        : false } Whenever any of the switches are toggled, an action checkBox is called. This method updates the value of toggleSwitches, calls the method to update the displayUrl and make the corresponding API request to update the displayed response. The code looks like this : makeRequest() { this.set('isLoading', true); this.get('loader')   .load(this.get('displayUrl'), { isExternal: true })   .then(json => {     json = JSON.stringify(json, null, 2);     this.set('json', htmlSafe(syntaxHighlight(json)));   })   .catch(() => {     this.get('notify').error(this.get('l10n').t('Could not fetch from the server'));     this.set('json', 'Could not fetch from the server');   })   .finally(() => {     this.set('isLoading', false);   }); }, buildDisplayUrl() { let newUrl = this.get('baseUrl'); const include = []; for (const key in this.get('toggleSwitches')) {   if (this.get('toggleSwitches').hasOwnProperty(key)) {     this.get('toggleSwitches')[key] && include.push(key);   } } this.set('displayUrl', buildUrl(newUrl, {   include: include.length > 0 ? include : undefined }, true)); }, actions: { checkboxChange(data) {   this.set(`toggleSwitches.${data}`, !this.get(`toggleSwitches.${data}`));   this.buildDisplayUrl();   this.makeRequest(); } } The above code uses some utility methods such as buildUrl and this.get(‘loager’).load(). The complete codebase is available here -> Open Event Frontend Repository. References Open Event Frontend Open Event Server JavaScript Ember

Continue ReadingOpen Event Frontend – Implement Access Event API via REST API

Open Event Server – Pages API

This article illustrates how the Pages API has been designed and implemented on the server side, i.e., FOSSASIA‘s Open Event Server. Pages endpoint is used to create static pages such as “About Page” or any other page that doesn’t need to be updated frequently and only a specific content is to be shown. Parameters name - This stores the name of the page. Type - String Required - Yes title - This stores the title of the page. Type - String Required - No url - This stores the url of the page. Type - String Required - Yes description - This stores the description of the page. Type - String Required - Yes language - This stores the language of the page. Type - String Required - No index - This stores the position of the page. Type - Integer Required - No Default - 0 place - Location where the page will be placed. Type - String Required - No Accepted Values - ‘footer’ and ‘event’ These are the allowed parameters for the endpoint. Model Lets see how we model this API. The ORM looks like this : __tablename__ = 'pages' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String, nullable=False) title = db.Column(db.String) url = db.Column(db.String, nullable=False) description = db.Column(db.String) place = db.Column(db.String) language = db.Column(db.String) index = db.Column(db.Integer, default=0) As you can see, we created a table called “pages”. This table has 8 columns, 7 of which are the parameters that I have mentioned above. The column “id” is an Integer column and is the primary key column. This will help to differentiate between the various entries in the table. The visualisation for this table looks as follows : API We support the following operations: GET all the pages in the database POST create a new page GET details of a single page as per id PATCH a single page by id DELETE a single page by id To implement this we first add the routes in our python file as follows : api.route(PageList, 'page_list', '/pages') api.route(PageDetail, 'page_detail', '/pages/<int:id>') Then we define these classes to handle the requests. The first route looks as follows: class PageList(ResourceList):   """   List and create page   """   decorators = (api.has_permission('is_admin', methods="POST"),)   schema = PageSchema   data_layer = {'session': db.session,                 'model': Page} As can be seen above, this request requires the user to be an admin. It uses the Page model described above and handles a POST request. The second route is: class PageDetail(ResourceDetail):   """   Page detail by id   """   schema = PageSchema   decorators = (api.has_permission('is_admin', methods="PATCH,DELETE"),)   data_layer = {'session': db.session,                 'model': Page} This route also requires the user to be an admin. It uses the Page model and handles PATCH, DELETE requests. To summarise our APIs are: GET /v1/pages{?sort,filter} POST /v1/pages{?sort,filter} GET /v1/pages/{page_id} PATCH /v1/pages/{page_id} DELETE /v1/pages/{page_id} References Flask-Marshmallow SQLAlchemy Open Event Server Flask

Continue ReadingOpen Event Server – Pages API