def search_everything(request): res = {} if request.method == 'POST': form = SearchForm(request.POST) if form.is_valid(): search_term = form.cleaned_data['find'].rstrip() res['Cv'] = res['User'] = res['Proj'] = res['ActionItem']\ = res['ActionNote'] = res['Note'] = None for word in search_term.split(' '): for key in res.keys(): if res[key] != None: res[key] = res[key] & search(key, word) else: res[key] = search(key, word) for key in res.keys(): res[key] = list(res[key]) else: return render(request,'generic_search.html',{'form':form,'results': False}) else: form = SearchForm() return render(request,'generic_search.html',{'form':form,'results': False}) return render(request,'generic_search.html',{'form':form,'results': len( [ v for k,v in res.items() if v ] ) > 0, 'res_cv': res['Cv'], 'res_user': res['User'], 'res_proj': res['Proj'], 'res_actionitem': res['ActionItem'], 'res_actionnote': res['ActionNote'], 'res_note': res['Note'] })
def timetable_check_context_update(self, num_timetables, timetable_name, context): if num_timetables < 1: context.update({'no_timetable': True}) response = render('menu_response.j2', context) else: error = '{} is not a valid timetable name.'.format(timetable_name) response = render('timetable_response.j2', error=error) return response
def timetable(message): message_text_list = message.body['text'].lower().split() len_msg_text_list = len(message_text_list) if len_msg_text_list == 2 and message_text_list[1] == 'timetable': timetable_res = TimetableAPIUtils() timetable_names = timetable_res.list_timetable_names() context = {'timetable_names': timetable_names} response = render('timetable_response.j2', context) else: response = render('help_response.j2') message.reply(response)
def timetable_check_context_update(self, num_timetables, timetable_name, context): if num_timetables < 1: context.update({'no_timetable': True}) response = render('menu_response.j2', context) else: error = '{} is not a valid timetable name.'.format( timetable_name ) response = render('timetable_response.j2', error=error) return response
def timetable(message): message_text_list = message.body['text'].lower().split() len_msg_text_list = len(message_text_list) if len_msg_text_list == 2 and message_text_list[1] == 'timetable': timetable_res = TimetableAPIUtils() timetable_names = timetable_res.list_timetable_names() context = { 'timetable_names': timetable_names } response = render('timetable_response.j2', context) else: response = render('help_response.j2') message.reply(response)
def render_queue(self, message, method): message_text_list = message.body['text'].lower().split() if len(message_text_list) < 3: error = 'Howdy, please enter a project name' response = render('mr_queue_response.j2', error=error) else: project_name = message_text_list[2:] context = {'mrs': method(project_name)} if context['mrs']: response = render('mr_queue_response.j2', context) else: error = ("Hey there, it is either there is no assigned " "merge requests or you entered a wrong project name.") response = render('mr_queue_response.j2', error=error) return response
def search_news(request): # Compare function used to sort the results array. def cmp_news(x, y): if x.date < y.date: return 1 elif x.date == y.date: return 0 else: return -1 res = None search_term = '' if request.method == 'GET' and request.GET.has_key('q'): form = SearchNewsForm(request.GET) if form.is_valid(): search_term = form.cleaned_data['q'].rstrip() if request.user.has_perm('mainsite.change_news'): queryset = News.objects.all() else: queryset = News.objects.filter(is_published=True) for word in search_term.split(' '): if res == None: res = set(queryset.filter(Q(title__icontains=word)\ | Q(body__icontains=word))) else: res = res & set(queryset.filter(Q(title__icontains=word)\ | Q(body__icontains=word))) res = sorted(list(res), cmp_news) else: form = SearchNewsForm() return render(request,'news_search.html',{'search_term': search_term, 'results': res})
def view_content_with_slug(request, slug): event = get_object_or_404(Event, slug=slug) if (not event.is_published or event.is_short_preview) and not request.user.has_perms('formacao.event.can_change'): raise Http404 else: return render(request,'event_detail.html',{'event':event})
def edit_my_profile(request): u = request.user try: profile = u.get_profile() except UserProfile.DoesNotExist: profile = None if request.method == 'POST': POST = request.POST.copy() POST['user'] = u.id profile_form = UserProfileForm(POST, request.FILES, instance=profile) user_form = UserForm(request.POST, request.FILES, instance=u) if user_form.is_valid() and profile_form.is_valid(): u = user_form.save() profile = profile_form.save() profile.user = u request.user.message_set.create(message="Your Profile was updated") return HttpResponseRedirect(profile.get_absolute_url()) else: user_form = UserForm(instance=u) if profile: profile_form = UserProfileForm(instance=profile) else: profile_form = UserProfileForm(initial={'user':request.user}) return render(request, 'edit_profile.html', {'profile_form':profile_form, 'user_form':user_form})
def project_dashboard(request): my_proj = mergeLists( request.user.projects_working.filter(active=True).order_by("end_date"), request.user.projects_managed.filter(active=True).order_by("end_date") ) my_task = [ [item, dist(item.due_date)] for item in request.user.actionitem_todo.filter(done=False) ] jk_proj = Project.objects.filter(active=True) late_projects = Project.objects.filter(active=True, end_date__lt=date.today()) class MockUserWithCount(object): def __init__(self,user,count): self.user = user self.count = int(count) late_people = [] for u in User.objects.all(): c = u.actionitem_todo.filter(done=False,due_date__lt=date.today()).count() if c > 0: late_people.append(MockUserWithCount(u,c)) late_people.sort(lambda x,y: y.count - x.count) late_tasks = ActionItem.objects.filter(done=False,due_date__lt=date.today()) return render(request,'project_dashboard.html',{ 'my_proj_list':my_proj, 'my_task_list':my_task, 'jk_proj_list':jk_proj, 'late_projects':late_projects, 'late_people':late_people, 'late_tasks':late_tasks, })
def project_detail(request,object_id): p = get_object_or_404(Project,id=object_id) if not p.check_user(request.user): return PermissionDenied(request) return render(request,'project_detail.html',{ 'object':p, 'notes': p.note_set.order_by("-set_date"), 'actionitems': [ [item, dist(item.due_date)] for item in p.actionitem_set.all() ] })
def action_detail(request,object_id): p = get_object_or_404(ActionItem,id=object_id) if not p.project.check_user(request.user): return PermissionDenied(request) add_note_form = ActionNoteForm(initial={'author':request.user.id,'actionitem':object_id }) return render(request,'action_detail.html',{ 'object':p, 'notes': p.actionnote_set.order_by("-set_date", "-id"), "add_note_form":add_note_form })
def public_curriculum(request, username): # This page will be visible to the outside world (logged out users) u = get_object_or_404(User, username = username) c = get_object_or_404(CurriculumVitae, owner = u) try: profile = u.get_profile() except UserProfile.DoesNotExist: profile = None return render(request,'public_curriculum.html',{'u':u, 'cv':c, 'profile':profile})
def project_fastedit(request, object_id): if request.method == 'POST': p = get_object_or_404(Project,id=object_id) if not p.check_manager(request.user, 'change'): return PermissionDenied(request) p.description = request.POST['content'] p.save() return render(request,'project_description.html',{'object':p})
def test_queue_assigned_without_project_name(self, mock_object): mock_object.return_value = self.assigned_message_without_project mock_object.body = self.assigned_msg_without_project queue_assigned(mock_object) error = 'Howdy, please enter a project name' self.assertTrue(mock_object.reply.called) mock_object.reply.assert_called_with( render('mr_queue_response.j2', error=error))
def curriculum(request,username): if (request.user.has_perm('cvmanager.can_view_cv') and request.user.has_perm('cvmanager.can_view_cv_details')) or request.user.username == username: u = get_object_or_404(User, username = username) c = get_object_or_404(CurriculumVitae, owner = u) try: profile = u.get_profile() except UserProfile.DoesNotExist: profile = None return render(request,'curriculum.html',{'u':u, 'cv':c, 'profile':profile}) else: return PermissionDenied(request)
def test_queue_assigned_with_wrong_project_name(self, msg_mock, mr_mock): msg_mock.return_value = self.assigned_message_with_project msg_mock.body = self.assigned_msg_with_project mr_mock.return_value = [] error = ("Hey there, it is either there is no assigned " "merge requests or you entered a wrong project name.") queue_assigned(msg_mock) self.assertTrue(msg_mock.reply.called) msg_mock.reply.assert_called_with( render('mr_queue_response.j2', error=error))
def surveys(request, object_id, register): event = get_object_or_404(Event, id=object_id) if (not event.is_published or event.is_short_preview) and not request.user.has_perms('formacao.event.can_change'): raise Http404 else: if register: url = event.iframeUrl_registration else: url = event.iframeUrl_surveys if url and not (register and event.close_registration): return render(request,'survey_template.html',{'iFrameUrl':url, 'event':event}) else: raise Http404
def profile(request,username): u = get_object_or_404(User, username = username) if not (request.user == u or request.user.has_perm('accounts.view_profiles') or work_together(u, request.user)): return PermissionDenied(request) current_projects = mergeLists(u.projects_working.filter(active=True), u.projects_managed.filter(active=True)) past_projects = mergeLists(u.projects_working.filter(active=False), u.projects_managed.filter(active=False)) todo_list = [ [item, dist(item.due_date)] for item in u.actionitem_todo.filter(done=False) ] try: profile = u.get_profile() except UserProfile.DoesNotExist: profile = None return render(request,'user_profile.html',{'u':u,'current_projects':current_projects,'past_projects':past_projects,'todo_list':todo_list, 'profile':profile})
def view_private_content(request, object_id): event = get_object_or_404(Event, id=object_id) if (not event.is_published or event.is_short_preview) and not request.user.has_perms('formacao.event.can_change'): raise Http404 else: if request.user.has_perms('formacao.event.can_change') or \ (request.method == 'POST' and request.POST['pwd'] == event.password) or \ (event.privateContent and not event.password): return render(request,'event_detail.html',{'event':event, 'private':True}) else: return HttpResponseRedirect(event.get_absolute_url())
def create_conf(): ''' 根据settings配置成配置文件们, ex.metrics ''' Nebula_Config = dict( (k, v) for k, v in settings.iteritems() if k[0].isupper()) # 生成metrics配置 metrics_conf = utils.render(settings.Metrics_Conf_Tem, Nebula_Config) with open(settings.Metrics_Conf_FN, 'w') as f: f.write(metrics_conf) click.echo(u'生成配置成功完成') click.echo('')
def test_queue_unassigned_with_project_name(self, msg_mock, mr_mock): msg_mock.return_value = self.unassigned_message_with_project msg_mock.body = self.unassigned_msg_with_project mr = self.mr_api[0] mr_mock.return_value = { 'title': mr['title'], 'url': mr['web_url'], 'user': mr['author']['name'] } context = {'mrs': mr_mock.return_value} queue_unassigned(msg_mock) self.assertTrue(msg_mock.reply.called) msg_mock.reply.assert_called_with( render('mr_queue_response.j2', context))
def edit_view(request,object_id,form_class,template_name): model = form_class.Meta.model obj = get_object_or_404(model,id=object_id) if model is ActionNote: proj = obj.actionitem.project else: proj = obj.project if not (request.user.has_perm('gestor.change_' + model._meta.module_name) or proj.has_user(request.user)): return PermissionDenied(request) if request.method == 'POST': form = form_class(request.POST, request.FILES, instance=obj) if form.is_valid(): obj = form.save() if request.user.is_authenticated(): request.user.message_set.create(message="The %s was updated" % model._meta.verbose_name ) if form.cleaned_data['notification']: if model == ActionItem: full_desc = "For: %s\n\n%s" % (", ".join([ e.get_full_name() for e in obj.targets.all() ]), obj.description) elif model == Note: full_desc = "%s" % obj.description else: full_desc = "" if model is ActionNote: send_mail( "[%s] %s edited by %s" % (obj.actionitem.title,model._meta.verbose_name,request.user.get_full_name()), '%s edited a %s in action item %s whith the following description:\n\n "%s" \n\nLink: %s\n\n %s' % (request.user.get_full_name(), model._meta.verbose_name, obj.actionitem.title,obj.description,BASE_DOMAIN + obj.actionitem.get_absolute_url(),full_desc), DEFAULT_FROM_EMAIL, [ user.email for user in obj.actionitem.targets.all() if not user == request.user ]) else: send_mail( "[%s] %s edited: %s" % (obj.project.name,model._meta.verbose_name,obj.title), '%s edited a %s in project %s entitled "%s" \n\n Link: %s\n\n %s' % (request.user.get_full_name(), model._meta.verbose_name, obj.project.name,obj.title,BASE_DOMAIN + obj.get_absolute_url(),full_desc), DEFAULT_FROM_EMAIL, [ user.email for user in obj.project.team.all() if not user == request.user ]) if model is ActionNote: return HttpResponseRedirect(obj.actionitem.get_absolute_url()) else: return HttpResponseRedirect(obj.get_absolute_url()) else: form = form_class(instance=obj) if model == ActionItem: form.fields['targets'] = ModelMultipleChoiceField(queryset=obj.project.team.all()) return render(request,template_name,{'form':form})
def project_edit(request, object_id): p = get_object_or_404(Project,id=object_id) if not p.check_manager(request.user, 'change'): return PermissionDenied(request) if request.method == 'POST': form = ProjectForm(request.POST, instance = p) if form.is_valid(): p = form.save() request.user.message_set.create(message="The Project was updated") return HttpResponseRedirect(p.get_absolute_url()) else: form = ProjectForm(instance = p) return render(request,"project_edit.html",{'form':form})
def create_news(request): if request.user.has_perm('mainsite.add_news'): if request.method == 'POST': form = NewsForm(request.POST) if form.is_valid(): obj = form.save() request.user.message_set.create(message="News Created") return HttpResponseRedirect(obj.get_absolute_url()) else: form = NewsForm() return render(request,"news_edit.html",{'form':form}) else: return PermissionDenied(request)
def project_create(request): if request.user.has_perm('gestor.add_project'): p = Project() if request.method == 'POST': form = ProjectForm(request.POST, instance = p) if form.is_valid(): p = form.save() request.user.message_set.create(message="Project Created") return HttpResponseRedirect(p.get_absolute_url()) else: form = ProjectForm(instance = p) return render(request,"project_edit.html",{'form':form}) else: return PermissionDenied(request)
def create_view(request,object_id,form_class,template_name): model = form_class.Meta.model # Never a Project if model is ActionNote: obj = get_object_or_404(ActionItem,id=object_id).project else: obj = get_object_or_404(Project,id=object_id) if not (request.user.has_perm('gestor.add_' + model._meta.module_name) or obj.has_user(request.user)): return PermissionDenied(request) if request.method == 'POST': form = form_class(request.POST, request.FILES) if form.is_valid(): obj = form.save() if request.user.is_authenticated(): request.user.message_set.create(message="The %s was created" % model._meta.verbose_name ) if form.cleaned_data['notification']: if model is ActionNote: send_mail( "[%s] New %s by %s" % (obj.actionitem.title,model._meta.verbose_name,request.user.get_full_name()), '%s created a new %s in action item %s whith the following description:\n\n "%s" \n\nLink: %s' % (request.user.get_full_name(), model._meta.verbose_name, obj.actionitem.title,obj.description,BASE_DOMAIN + obj.actionitem.get_absolute_url()), DEFAULT_FROM_EMAIL, [ user.email for user in obj.actionitem.targets.all() if not user == request.user ]) else: send_mail( "[%s] New %s: %s" % (obj.project.name,model._meta.verbose_name,obj.title), '%s created a new %s in project %s entitled "%s" \n\n Link: %s' % (request.user.get_full_name(), model._meta.verbose_name, obj.project.name,obj.title,BASE_DOMAIN + obj.get_absolute_url()), DEFAULT_FROM_EMAIL, [ user.email for user in obj.project.team.all() if not user == request.user ]) if model is ActionNote: return HttpResponseRedirect(obj.actionitem.get_absolute_url()) else: return HttpResponseRedirect(obj.get_absolute_url()) else: if model is ActionNote: form = form_class(initial={'author':request.user.id,'actionitem':object_id }) else: form = form_class(initial={'author':request.user.id,'project':object_id }) if model == ActionItem: form.fields['targets'] = ModelMultipleChoiceField(queryset=Project.objects.get(id=object_id).team.all()) return render(request,template_name,{'form':form})
def activity_stream(request, all): if not request.user.has_perm('activitystream.view_everything'): return PermissionDenied(request) NUM_OF_ACTIVITIES = 20 if request.method == 'POST' and request.user.has_perm('activitystream.add_activity'): form = ActivityMessageForm(request.POST) if form.is_valid(): message = form.cleaned_data['message'] activity = Activity(message_type = Activity.MSG_USER, message = message) activity.save() else: form = ActivityMessageForm() if all: activities = Activity.objects.all() else: activities = Activity.objects.all()[:NUM_OF_ACTIVITIES] return render(request,'activity_stream.html',{'activities':activities, 'form':form, 'all':all})
def edit_my_curriculum(request): u = request.user if u.curriculumvitae_set.count(): cv = CurriculumVitae.objects.get(owner=u) else: cv = None if request.method == 'POST': POST = request.POST.copy() POST['owner'] = u.id form = CvForm(POST, request.FILES, instance=cv) if form.is_valid(): cv = form.save() request.user.message_set.create(message="Your Curriculum was updated") return HttpResponseRedirect(cv.get_absolute_url()) else: if cv: form = CvForm(instance=cv) else: form = CvForm(initial={'user':request.user}) return render(request, 'curriculum_edit.html', {'form':form})
def activity_stream_detail(request, stream_id): activity = get_object_or_404(Activity, id=stream_id) return render(request,'activity_stream_detail.html',{'object':activity})
import dotenv from common.utils import render dotenv.load() API_TOKEN = dotenv.get('SLACKBOT_API_TOKEN') DEFAULT_REPLY = render('help_response.j2') ERRORS_TO = dotenv.get('ERRORS_CHANNEL') PLUGINS = ['plugins']
def menu(message): menu_helper = MenuHelper() date_helper = DateHelper() days = date_helper.get_days() # Convert message text to list to remove multiple spaces that may have # been mistakenly added by the user and convert the list back to string message_text_list = message.body['text'].lower().split() len_msg_text_list = len(message_text_list) message_text = ' '.join(message_text_list) timetable_res = TimetableAPIUtils() timetable_names = timetable_res.list_timetable_names() num_timetables = len(timetable_names) if len_msg_text_list > 1: timetable_name = message_text_list[1] if len_msg_text_list > 2: day_of_week = message_text_list[2] context = {'timetable_names': timetable_names, 'day_of_week': 'today'} if message_text == 'menu': if num_timetables < 1: context.update({'no_timetable': True}) elif num_timetables == 1: meals = menu_helper.get_meals(timetable_names[0], 'today') menu_helper.meals_check_context_update(meals, context, 'today') else: context.update({'multiple_timetables': True}) response = render('menu_response.j2', context) elif len_msg_text_list == 2: if timetable_name in timetable_names: # User entered "menu TIMETABLE_NAME" meals = menu_helper.get_meals(timetable_name, 'today') menu_helper.meals_check_context_update(meals, context, 'today') response = render('menu_response.j2', context) else: response = menu_helper.timetable_check_context_update( num_timetables, timetable_name, context) elif len_msg_text_list == 3: if timetable_name in timetable_names and day_of_week in days: # User entered "menu TIMETABLE_NAME day" meals = menu_helper.get_meals(timetable_name, day_of_week) context.update({'day_of_week': day_of_week}) menu_helper.meals_check_context_update(meals, context, day_of_week) response = render('menu_response.j2', context) elif timetable_name not in timetable_names: response = menu_helper.timetable_check_context_update( num_timetables, timetable_name, context) elif day_of_week not in days: error = 'You did not enter a valid day.' context.update({'invalid_day': True, 'days': days}) response = render('menu_response.j2', context, error) else: error = 'I think you entered a wrong menu command.' response = render('help_response.j2', error=error) message.reply(response)
def actionnote_detail(request,object_id): p = get_object_or_404(ActionNote,id=object_id) if not p.actionitem.project.check_user(request.user): return PermissionDenied(request) return render(request,'actionnote_detail.html',{'object':p})
def render_template_with_error(message, error): response = render('review_response.j2', error=error) message.reply(response)
def project_list(request): if request.user.is_staff: p = Project.objects.filter(active=True) else: p = request.user.projects_working.filter(active=True) return render(request,'project_list.html',{'object_list':p})
def render_template_with_context(message, context): response = render('review_response.j2', context=context) message.reply(response)
def menu(message): menu_helper = MenuHelper() date_helper = DateHelper() days = date_helper.get_days() # Convert message text to list to remove multiple spaces that may have # been mistakenly added by the user and convert the list back to string message_text_list = message.body['text'].lower().split() len_msg_text_list = len(message_text_list) message_text = ' '.join(message_text_list) timetable_res = TimetableAPIUtils() timetable_names = timetable_res.list_timetable_names() num_timetables = len(timetable_names) if len_msg_text_list > 1: timetable_name = message_text_list[1] if len_msg_text_list > 2: day_of_week = message_text_list[2] context = { 'timetable_names': timetable_names, 'day_of_week': 'today' } if message_text == 'menu': if num_timetables < 1: context.update({'no_timetable': True}) elif num_timetables == 1: meals = menu_helper.get_meals(timetable_names[0], 'today') menu_helper.meals_check_context_update(meals, context, 'today') else: context.update({'multiple_timetables': True}) response = render('menu_response.j2', context) elif len_msg_text_list == 2: if timetable_name in timetable_names: # User entered "menu TIMETABLE_NAME" meals = menu_helper.get_meals(timetable_name, 'today') menu_helper.meals_check_context_update(meals, context, 'today') response = render('menu_response.j2', context) else: response = menu_helper.timetable_check_context_update( num_timetables, timetable_name, context ) elif len_msg_text_list == 3: if timetable_name in timetable_names and day_of_week in days: # User entered "menu TIMETABLE_NAME day" meals = menu_helper.get_meals(timetable_name, day_of_week) context.update({'day_of_week': day_of_week}) menu_helper.meals_check_context_update(meals, context, day_of_week) response = render('menu_response.j2', context) elif timetable_name not in timetable_names: response = menu_helper.timetable_check_context_update( num_timetables, timetable_name, context ) elif day_of_week not in days: error = 'You did not enter a valid day.' context.update({ 'invalid_day': True, 'days': days }) response = render('menu_response.j2', context, error) else: error = 'I think you entered a wrong menu command.' response = render('help_response.j2', error=error) message.reply(response)
def help(message): response = render('help_response.j2') message.reply(response)
def archive(request): date_list = News.objects.filter(is_published=True).dates('date', 'year')[::-1] return render(request,'news_archive.html', { 'date_list': date_list })