def post(self, request, **kwargs): event = Event.objects.filter(id=kwargs['id'])[0] event_type = kwargs['type'] event_content = getattr(event, event_type) # check if we are at the beginning of the wizard if not request.POST: form_items = [ FormItem( type=FormItemType.string, name=event_type, description='\n'.join([ 'Current {event_type}: {event_content}'.format( event_type=event_type, event_content='{:%d-%m-%Y %H:%M}'.format( event_content) if isinstance( event_content, datetime.datetime) else event_content), 'Send input to edit' ]), header='edit {event_type}'.format(event_type=event_type), footer='Reply with input/BACK') ] content = Form(body=form_items, method='POST', path=reverse('edit_event', args=(kwargs['id'], event_type)), meta=FormMeta(skip_confirmation=True)) return self.to_response(content) else: try: if event_type in ['start_datetime', 'end_datetime']: user_input = datetime.datetime.strptime( request.POST[event_type], '%d-%m-%Y %H:%M') else: user_input = request.POST[event_type] setattr(event, event_type, user_input) event.save() cache.set('event_edited', True) except Exception: form_items = [ FormItem( type=FormItemType.string, name='edit_event', description='\n'.join([ 'Event not edited. Please check your input format and try again.', 'Example for date and time format: 31-12-2020 12:00' ]), header='edit event', footer='Reply BACK') ] content = Form(body=form_items, method='GET', path=reverse('home'), meta=FormMeta(skip_confirmation=True)) return self.to_response(content) return HttpResponseRedirect(reverse('home'))
def get(self, request): form_items = [ FormItemContent( type=FormItemContentType.string, name='title', description= 'Give your new post a title (maximum 64 characters)', header='add', footer='Reply with post title or BACK'), FormItemContent(type=FormItemContentType.string, name='description', description='Send post content (max 50 words)', header='add', footer='Reply with post content or BACK'), FormItemMenu(body=[ FormItemMenuItem(description='Private (share code)', value='True'), FormItemMenuItem(description='Public (everyone)', value='False') ], name='is_private') ] form = Form(body=form_items, method='POST', path=reverse('add_post'), meta=FormMeta(confirmation_needed=False, completion_status_in_header=False, completion_status_show=False)) return self.to_response(form)
def post(self, request): try: event_create = Event.objects.create( description=request.POST['description'], start_datetime=datetime.datetime.strptime( request.POST['start_datetime'], '%d-%m-%Y %H:%M'), end_datetime=datetime.datetime.strptime( request.POST['end_datetime'], '%d-%m-%Y %H:%M')) event_create.save() cache.set('event_added', True) except Exception: form_items = [ FormItem(type=FormItemType.string, name='add_event', description='Event not added. Please check your input' ' format and try again.', header='add event', footer='Reply BACK') ] content = Form(body=form_items, method='GET', path=reverse('home'), meta=FormMeta(skip_confirmation=True)) return self.to_response(content) return HttpResponseRedirect(reverse('home'))
def post(self, request): keyword = request.POST['keyword'] events = Event.objects.filter(Q(description__icontains=keyword)) if not events: form_items = [ FormItem(type=FormItemType.string, name='keyword', description='No results found. Please try again ' 'with different keywords', header='search', footer='Reply keywords') ] content = Form(body=form_items, method='POST', path=reverse('search_wizard'), meta=FormMeta(skip_confirmation=True)) else: menu_items = [] for event in events: menu_items.append( MenuItem(description=truncatechars(event.description, 30), method='GET', path=event.get_absolute_url())) content = Menu(body=menu_items, header='search: {}'.format(keyword)) return self.to_response(content)
def get(self, request): form_items = [ FormItem(type=FormItemType.string, name='description', description='Send the description', header='description', footer='Reply text'), FormItem(type=FormItemType.string, name='start_datetime', description='\n'.join([ 'Send the starting date and time', 'Example: 31-12-2020 12:00' ]), header='starting date time', footer='Reply with date and time'), FormItem(type=FormItemType.string, name='end_datetime', description='\n'.join([ 'Send the ending date and time', 'Example: 31-12-2020 14:00' ]), header='ending date time', footer='Reply with date and time'), ] content = Form(body=form_items, method='POST', path=reverse('add_event'), meta=FormMeta(skip_confirmation=True)) return self.to_response(content)
def post(self, request): keyword = request.POST['keyword'] response = self.get_page_data(keyword) if response['Response'] == 'False': return self.to_response( Form(body=[ FormItemContent(type=FormItemContentType.string, name='result', description='No results', header='{keyword} SEARCH'.format( keyword=keyword.title()), footer='send BACK and search again') ], method='GET', path=reverse('home'), meta=FormMeta(confirmation_needed=False, completion_status_in_header=False, completion_status_show=False))) body = [] for result in response['Search']: body.append( MenuItem(type=MenuItemType.option, description=u'{title} - {year}'.format( title=result['Title'], year=result['Year']), method='GET', path=reverse('movie_detail', args=[result['imdbID']]))) return self.to_response( Menu(body=body, header='{keyword} SEARCH'.format(keyword=keyword.title()), footer='Select result'))
def get(self, request): form_items = [ FormItem(type=FormItemType.string, name='keyword', description='Send keywords to search', header='search', footer='Reply keywords') ] content = Form(body=form_items, method='POST', path=reverse('search_wizard'), meta=FormMeta(skip_confirmation=True)) return self.to_response(content)
def get(self, request): body = [ FormItemContent(type=FormItemContentType.string, name='keyword', description='Send keywords to search', header='search', footer='send keyword') ] return self.to_response( Form(body=body, method='POST', path=reverse('search_wizard'), meta=FormMeta(confirmation_needed=False, completion_status_in_header=False, completion_status_show=False)))
def get(self, request, id): form_items = [ FormItem( type=FormItemType.string, name='comment_text', description='Send your comment, no more than 200 characters.', header='add comment', footer='Reply with text') ] form = Form(body=form_items, method='POST', path=reverse('add_comment', args=[id]), meta=FormMeta(confirmation_needed=False, completion_status_in_header=False, completion_status_show=False)) return self.to_response(form)
def get(self, request, id): form_items = [ FormItem(type=FormItemType.string, name='message', description='Reply with your message for the post owner.', header='Message', footer='Send code or keyword') ] form = Form(body=form_items, method='POST', path=reverse('send_msg', args=[id]), meta=FormMeta(confirmation_needed=False, completion_status_in_header=False, completion_status_show=False)) return self.to_response(form)
def get(self, request): form_items = [ FormItem(type=FormItemType.string, name='keyword', description='Send code or keyword to search', header='search', footer='Send code or keyword') ] form = Form(body=form_items, method='POST', path=reverse('search_wizard'), meta=FormMeta(confirmation_needed=False, completion_status_in_header=False, completion_status_show=False)) return self.to_response(form)
def get(self, request): if self.get_user().is_admin: form_items = [ FormItem(type=FormItemType.string, name='app_name', description="Let us know your app name.", header='admin', footer="Reply with app name without '#'"), FormItem( type=FormItemType.string, name='company_email', description="Let us know the company's email address.", header='admin', footer='Reply with company email address'), FormItem( type=FormItemType.string, name='email_token', description='Please provide your Postmark email token.', header='admin', footer='Reply with token') ] else: form_items = [ FormItem(type=FormItemType.string, name='user_message', description='What would you like to know/inquire?', header='contact', footer='Reply with your message'), ] if not self.get_user().email: form_items.append( FormItem(type=FormItemType.string, name='user_email', description='Let us know your email address.', header='contact', footer='Reply with email address')) form = Form(body=form_items, method='POST', path=reverse('home'), meta=FormMeta(confirmation_needed=False, completion_status_in_header=False, completion_status_show=False)) return self.to_response(form)
def get(self, request, id, new_rating=None): form_items = [ FormItem(type=FormItemType.string, name='rating_value', description=u'\n'.join([ 'Send your rating from 1 to 5.', '1 is Poor, 5 is Excellent.' ]), header='add rating', footer='Reply "1".."5"') ] form = Form(body=form_items, method='POST', path=reverse('rating', args=[id, new_rating]), meta=FormMeta(confirmation_needed=False, completion_status_in_header=False, completion_status_show=False)) return self.to_response(form)
def get(self, request, post_id=None): user = self.get_user() if user.username == '': form_items = [ FormItemContent(type=FormItemContentType.string, name='username', description='Please choose a username', header='MENU', footer='Send username') ] content = Form(body=form_items, method='POST', path=reverse('home'), meta=FormMeta(confirmation_needed=False, completion_status_in_header=False, completion_status_show=False)) else: menu_items = [ MenuItem(description='Add post', method='GET', path=reverse('add_post')), MenuItem(description='Search', method='GET', path=reverse('search_wizard')), MenuItem(description='My posts', method='GET', path=reverse('my_posts')) ] posts = Post.objects.exclude(user=self.get_user()).exclude( is_private=True).order_by('-created_at').all()[:3] if posts: for post in posts: menu_items.append( MenuItem(description=u'{}..'.format(post.title[:17]), method='GET', path=reverse('post_detail', args=[post.id]))) content = Menu(body=menu_items) return self.to_response(content)
def get(self, request, id): history = History.objects.all() movie_from_history = [ movie for movie in history if movie.omdb_id == id ] if not movie_from_history: response = self.get_page_data(id) if response['Response'] == 'False': return self.to_response( Form(body=[ FormItemContent(type=FormItemContentType.string, name='result', description='Please try again later', header='INFO', footer='send BACK') ], method='GET', path=reverse('home'), meta=FormMeta(confirmation_needed=False, completion_status_in_header=False, completion_status_show=False))) omdb_id = response['imdbID'] title = response['Title'] year = response['Year'] rate = response['Ratings'][0]['Value'] plot = response['Plot'] history_create = History.objects.create( user=self.get_user(), omdb_id=omdb_id, title=title, year=year, rate=rate, plot=plot, datetime=datetime.datetime.now()) history_create.save() else: movie_from_history = movie_from_history[0] omdb_id = movie_from_history.omdb_id title = movie_from_history.title year = movie_from_history.year rate = movie_from_history.rate plot = movie_from_history.plot user = self.get_user() user_history = user.history_set.all() movie_from_user = [ movie for movie in user_history if movie.omdb_id == id ] if movie_from_history and not movie_from_user: history_create = History.objects.create( user=self.get_user(), omdb_id=omdb_id, title=title, year=year, rate=rate, plot=plot, datetime=datetime.datetime.now()) history_create.save() elif movie_from_history and movie_from_user: movie_from_user = movie_from_user[0] movie_from_user.datetime = datetime.datetime.now() movie_from_user.save() body = [ FormItemContent( type=FormItemContentType.string, name='movie', description=u'\n'.join([ u'Title: {movie_title}'.format(movie_title=title), u'Year: {movie_year}'.format(movie_year=year), u'Rate: {movie_rate}'.format(movie_rate=rate), u'Plot: {movie_plot}'.format(movie_plot=plot), ]), header='Movie details', footer='send BACK') ] return self.to_response( Form(body=body, method='GET', path=reverse('home'), meta=FormMeta(confirmation_needed=False, completion_status_in_header=False, completion_status_show=False)))