def post(self): channel = self.request.get('channel') recipient = self.request.get('recipient') variables = {} if not channel: variables = {'error': 'Channel Name is required'} elif not recipient: variables = {'error': 'Recipient is required'} else: existing_channel = Channel.query(Channel.name == channel).get() if existing_channel: variables = {'error': 'Channel already exists'} else: # Create channel and success message new_channel = Channel(name=channel, recipient=recipient) new_channel.put() variables = {'success': 'Channel added!'} template = JINJA_ENVIRONMENT.get_template('create.html') self.response.write(template.render(variables))
def create_channel(request): if request.method == 'POST': name = request.POST.get('channel_name', '') user = request.user channel = Channel(name=name, user=user) channel.save() return redirect('home') return render(request, 'forms/create_channel.html')
def post(self, request, format='json'): serializer = UserSerializer(data=request.data) if serializer.is_valid(): user = serializer.save() if user: Channel(user=user, slug=get_uname_slug(user.username)).save() json = serializer.data return Response(json, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def post(self): channel = self.request.get('channel') headline = self.request.get('headline') details = self.request.get('details', '') requested_channel = Channel.query(Channel.name == channel).get() if not headline or not requested_channel: logging.error('Missing headline or channel not found') self.error(500) return email.notify(requested_channel, headline, details)
def post(self, request, *args, **kwargs): if 'excel' in request.FILES: execl_file = request.FILES.get('excel') wb = open_workbook(filename=None, file_contents=request.FILES['excel'].read()) table = wb.sheets()[0] # sheet1 = wb.sheet_by_index(0) rows = table.nrows #获取行数 cols = table.ncols #获取列数 for r in range(1, rows): name = table.cell(r, 0).value bianmaqi = table.cell(r, 1).value jieru = table.cell(r, 2).value type = table.cell(r, 3).value pindao_id = table.cell(r, 4).value beizhu = table.cell(r, 5).value try: a = Channel.objects.filter(name=name) if a: print(name + " 存在") continue elif name == None or name == '': continue else: print(name) channel = Channel() channel.name = name channel.bianmaqi = bianmaqi channel.type = type channel.jieru = jieru channel.save() self.excelmodel(name, bianmaqi, jieru, type, pindao_id, beizhu) except: pass # print(sheet1.rcols) return HttpResponseRedirect( 'http://127.0.0.1:8000/channel/channel/') print(4444444) return super(XadminChannel, self).post(request, args, kwargs)
def show_pool(request, pool_id=None): ''' Show contents of pool <pool_id>. ''' notify = None splits = None missing_prices = None missing_channels = None pool = None if pool_id == 'new' else Pool.objects.get(id=pool_id) if request.method == 'POST' and request.POST.get('action'): daterangeform = DateRangeForm(request.POST) if not daterangeform.is_valid(): notify = Notify('Invalid date(s)') else: startdate = daterangeform.cleaned_data['fromdate'] enddate = daterangeform.cleaned_data['todate'] if request.POST.get('action') == 'Download latest prices': # dl_latest_prices(pool) pool.download_latest_prices() elif request.POST.get('action') == 'Download index': pool.index.download_history(startdate, enddate) elif request.POST.get('action') == 'Calculate index channels': Channel.calculate(pool.index, startdate, enddate) elif request.POST.get('action') == 'Download all stock prices': #CONSIDER: run separate process? # dl_prices(pool, startdate, enddate) pool.download_prices(startdate, enddate) elif request.POST.get('action') == 'Check for missing prices': missing_prices = pool.missing_prices(startdate, enddate) elif request.POST.get('action') == 'Check for stock splits': splits = pool.check_splits() elif request.POST.get('action') == 'Check for missing channels': missing_channels = pool.missing_channels(startdate, enddate) elif request.POST.get('action') == 'Calculate all stock channels': pool.calculate_channels(startdate, enddate) elif pool: enddate = datetime.date.today() if not pool.enddate else pool.enddate daterangeform = DateRangeForm(initial={ 'fromdate':pool.startdate, 'todate': enddate}) else: daterangeform = None if request.method == 'POST' and request.POST.get('save'): poolform = PoolForm(request.POST, instance=pool) memberform = MemberForm(request.POST, prefix='stock') if poolform.is_valid(): pool = poolform.save() if memberform.is_valid(): obj = memberform.save(commit=False) obj.pool = pool obj.save() else: # first entry - no POST data poolform = PoolForm(instance=pool) memberform = MemberForm(prefix='stock') if request.method == 'POST' and request.POST.get('action'): stock_list = request.POST.getlist('id') if request.POST.get('action') == 'Delete': if not len(stock_list): notify = Notify('Delete failed, select stock(s) to delete') else: notify = Notify('Delete stocks(s) {}?'.format(', '.join( StockPoolDates.objects.get(id=s).stock.name for s in stock_list))) notify.set_replies(request, ('Yes', 'No')) request.session['stock_delete_list'] = stock_list elif request.method == 'POST' and request.POST.get('reply'): stock_list = request.session.get('stock_delete_list') if not stock_list: raise AttributeError('session has no valid stock_delete_list') del request.session['stock_delete_list'] if request.POST.get('reply') == 'Yes': stocks = [StockPoolDates.objects.get(id=s).stock.name for s in stock_list] StockPoolDates.objects.filter(id__in=stock_list).delete() notify = Notify('Stock(s) {} deleted'.format(', '.join(stocks))) elif request.POST.get('reply') == 'No': notify = Notify('Delete cancelled') members = StockPoolDates.objects.filter(pool=pool).order_by('stock__name',) stocks_table = MembersTable(members, exclude=['pool',]) RequestConfig(request, paginate={'per_page': 100}).configure(stocks_table) return render(request, 'show_pool.html', { 'pool': pool, 'poolform': poolform, 'memberform': memberform, 'stocks_table': stocks_table, 'daterangeform': daterangeform, 'notify': notify, 'splits': splits, 'missing_prices': missing_prices, 'missing_channels': missing_channels, })
def show_stock(request, stock_id=None, symbol=None): ''' Show stock chart and metrics. ''' #CONSIDER: add indicators that can dynamically be selected/changed if stock_id: stock = Stock.objects.get(id=stock_id) elif symbol: stock = Stock.objects.get(name=symbol.upper()) else: raise SystemExit('Fatal Error: no stock_id or symbol specified.') missing_prices = stock.missing_prices() if not missing_prices: missing_channels = stock.missing_channels() splits = stock.check_splits() else: missing_channels = [] splits = [] #set default date range and lookback period: lookback = request.session.get('channel_lookback_period', Channel.YEAR) enddate = datetime.date.today() startdate = enddate - datetime.timedelta(days=365) if request.method == 'POST': reload_ = False if request.POST.get('todo') == 'Download missing prices': fromdate = min(missing_prices) todate = max(missing_prices) stock.download_history(fromdate, todate) reload_ = True if request.POST.get('todo') == 'Calculate missing channels': fromdate = min(missing_channels) todate = max(missing_channels) Channel.calculate(stock, fromdate, todate) reload_ = True if request.POST.get('todo') == 'Correct prices for split': stock.correct_splits() reload_ = True if reload_: if symbol: return redirect('show_symbol', symbol=symbol) else: return redirect('show_stock', stock_id=stock_id) if request.POST.get('todo') == 'Save settings': stockchartform = StockChartForm(request.POST) if stockchartform.is_valid(): sd = stockchartform.cleaned_data['startdate'] ed = stockchartform.cleaned_data['enddate'] if sd: startdate = sd if ed: enddate = ed lookback = int(stockchartform.cleaned_data['lookback_period']) request.session['channel_lookback_period'] = lookback else: stockchartform = StockChartForm(initial={'startdate': startdate, 'enddate': enddate, 'lookback_period': lookback}) else: stockchartform = StockChartForm(initial={'startdate': startdate, 'enddate': enddate, 'lookback_period': lookback}) stockchart = reverse('show_stock_chart_from_to', kwargs={ 'symbol': stock.name, 'startdate_str': date2datestr(startdate), 'enddate_str': date2datestr(enddate),}) return render(request, 'show_stock.html', {'stock': stock, 'data': stock.data_summary(enddate, lookback), 'missing_prices': missing_prices, 'missing_channels': missing_channels, 'splits': splits, 'stockchart': stockchart, 'stockchartform': stockchartform})