def post(request): """Returns a serialized object""" data = json.loads(request.body)["body"] guid = data.get("guid", None) res = Result() if guid: obj = getObjectsFromGuids([guid])[0] comment = Comment() comment.comment = data.get("comment", "No comment") comment.user = request.user comment.user_name = request.user.get_full_name() comment.user_email = request.user.email comment.content_object = obj # For our purposes, we never have more than one site comment.site_id = 1 comment.save() obj.comment_count += 1 obj.save() emailComment(comment, obj, request) res.append(commentToJson(comment)) return JsonResponse(res.asDict())
def object_detail(request, object_slug): trans_obj = TransientCandidate.objects.get(slug = object_slug) trans_candidate_id = trans_obj.pk ranked_interesting = Ranking.objects.filter(trans_candidate = trans_obj).filter(isInteresting = True) int_users_list = UserProfile.objects.filter(ranking = ranked_interesting) int_counts = len(int_users_list) if request.method == "POST": if request.user.is_authenticated(): #Save the comment if there is one. comment_text = request.POST.get('comment') if len(comment_text) > 0: #save the comment newComment = Comment() newComment.user = request.user newComment.user_name = request.user.username newComment.user_email = request.user.email newComment.user_url = UserProfile.objects.get(user=request.user).website newComment.comment = comment_text newComment.site = get_current_site(request) newComment.content_object = trans_obj newComment.save() return render(request, 'winnow/trans_detail.html', {'object' : trans_obj, 'interesting_count': str(int_counts), 'interesting_user_list': int_users_list})
def create(self, request, *args, **kwargs): if request.user.is_authenticated(): obj_content_type = ContentType.objects.get_for_model(BillSegment) obj = Comment() obj.content_type = obj_content_type obj.user = request.user obj.comment = request.data['comment'] obj.object_pk = request.data['object_pk'] obj.site_id = settings.SITE_ID obj.save() return Response(status=201) elif request.data['token']: token = Token.objects.get(key=request.data['token']) obj_content_type = ContentType.objects.get_for_model(BillSegment) obj = Comment() obj.content_type = obj_content_type obj.user = token.user obj.comment = request.data['comment'] obj.object_pk = request.data['object_id'] obj.site_id = settings.SITE_ID obj.save() serializer = CommentsSerializer(obj) return JSONResponse(serializer.data, status=201) else: return Response(serializer._errors, status=status.HTTP_400_BAD_REQUEST)
def post(request): """Returns a serialized object""" data = request.POST or json.loads(request.body)['body'] guid = data.get('guid', None) res = Result() if guid: obj = getObjectsFromGuids([guid,])[0] comment = Comment() comment.comment = data.get('comment', 'No comment') comment.user = request.user comment.user_name = request.user.get_full_name() comment.user_email = request.user.email comment.content_object = obj comment.site_id = get_current_site(request).id comment.save() obj.comment_count += 1 obj.save() emailComment(comment, obj, request) res.append(commentToJson(comment)) return JsonResponse(res.asDict())
def handle(self, *args, **options): # Get apps with a status of PENDING or QUESTION for partners with a status of AVAILABLE # where the editor has not agreed to the terms of use. pending_apps = (Application.objects.filter( status__in=[Application.PENDING, Application.QUESTION], partner__status__in=[Partner.AVAILABLE], editor__isnull=False, editor__user__userprofile__terms_of_use=False, ).exclude(editor__user__groups__name="restricted").order_by( "status", "partner", "date_created")) # Loop through the apps and add a comment if twl_team hasn't commented already or if the app hasn't had comments # in 8 days or more. for app in pending_apps: if (Comment.objects.filter( Q(object_pk=str(app.pk), site_id=settings.SITE_ID), (Q(user=twl_team) | Q(submit_date__gte=(timezone.now() - timedelta(days=8)))), ).count() == 0): comment = Comment( content_object=app, site_id=settings.SITE_ID, user=twl_team, # Translators: This comment is added to pending applications when our terms of use change. comment=_( "Our terms of use have changed. " "Your applications will not be processed until you log in and agree to our updated terms." ), ) comment.save()
def test_to_table_reviewed(self): site = USite.objects.create(name="site") unit1 = utils.create_unit(site=site) fault_utils.create_fault(unit=unit1) unit2 = utils.create_unit() fault2 = fault_utils.create_fault(unit=unit2) fault_rev = fault_utils.create_fault_review(fault=fault2) comment = Comment(submit_date=timezone.now(), user=fault_rev.reviewed_by, content_object=fault_rev.fault, comment="comment", site=get_current_site(None)) comment.save() rep = faults.FaultSummaryReport( report_opts={'review_status': "reviewed"}) rep.report_format = "csv" context = rep.get_context() table = rep.to_table(context) header_row = table.index([ _("Fault ID"), _("Occurred"), _("Site"), _("Unit"), _("Fault Type"), _("Modality"), _("Link"), ]) # should be one fault after header assert len(table[header_row + 1:]) == 1
def post(request): """Returns a serialized object""" data = request.POST or json.loads(request.body)['body'] guid = data.get('guid', None) res = Result() if guid: obj = getObjectsFromGuids([guid,])[0] comment = Comment() comment.comment = data.get('comment', 'No comment') comment.user = request.user comment.user_name = request.user.get_full_name() comment.user_email = request.user.email comment.content_object = obj comment.site_id = get_current_site(request).id comment.save() obj.comment_count += 1 obj.save() emailComment(comment, obj, request) res.append(commentToJson(comment)) else: res.isError = True res.message = "No guid provided" return JsonResponse(res.asDict())
def set_comment(node_handle, comment): """ Sets the comment string as a comment for the provided node_handle. """ content_type = ContentType.objects.get_for_model(NodeHandle) object_pk = node_handle.pk user = get_user() site_id = django_settings.SITE_ID c = Comment(content_type=content_type, object_pk=object_pk, user=user, site_id=site_id, comment=comment) c.save()
def rank(request): if request.method == "POST": form = RankingForm(request.POST) if form.is_valid(): if request.user.is_authenticated(): r = form.save(commit=False) r.ranker = UserProfile.objects.get(user=request.user) tc_id = int(request.POST.get("tc_id")) tc = TransientCandidate.objects.get(pk=tc_id) r.trans_candidate = tc r.save() # Now save the comment if there is one. comment_text = request.POST.get("comment") if len(comment_text) > 0: # save the comment newComment = Comment() newComment.user = request.user newComment.user_name = request.user.username newComment.user_email = request.user.email newComment.user_url = UserProfile.objects.get(user=request.user).website newComment.comment = comment_text newComment.site = get_current_site(request) newComment.content_object = tc newComment.save() return HttpResponseRedirect(reverse("rank")) else: print form.errors tc_id = int(request.POST.get("tc_id")) tc = TransientCandidate.objects.get(pk=tc_id) else: try: # Fetch any tc not ranked yet tc = TransientCandidate.objects.filter(dataset_id="cstar_june02_predictedreals").exclude( ranking=Ranking.objects.all() )[0] except IndexError: # Fetch any tc not ranked by the current user try: currentUser = UserProfile.objects.get(user=request.user) tc = TransientCandidate.objects.filter(dataset_id="cstar_june02_predictedreals").exclude( ranking=Ranking.objects.filter(ranker=currentUser) )[0] except IndexError: tc = None if tc is None: tc_id = -1 else: tc_id = tc.id form = RankingForm() return render(request, "winnow/rank.html", {"form": form, "page_rank": "selected", "tc_id": tc_id, "object": tc})
def create(self, request, *args, **kwargs): if request.user.is_authenticated(): obj_content_type = ContentType.objects.get_for_model(BillSegment) obj = Comment() obj.content_type = obj_content_type obj.user = request.user obj.comment = request.data['comment'] obj.object_pk = request.data['object_pk'] obj.site_id = settings.SITE_ID obj.save() return Response(status=201) else: return Response(status=403)
def create_new_sequence(requestsid, created, modified, snex2_param, users, notes, db_session, active=True): newobsgroup = ObservationGroup(name=str(requestsid), created=created, modified=modified) newobsgroup.save() cadence_strategy = snex2_param['cadence_strategy'] cadence_params = {'cadence_frequency': snex2_param['cadence_frequency']} newcadence = DynamicCadence(cadence_strategy=cadence_strategy, cadence_parameters=cadence_params, active=active, created=created, modified=modified, observation_group_id=newobsgroup.id) newcadence.save() ### Check if there are any SNEx1 comments associated with this ### observation request, and if so, save them in SNEx2 comment = db_session.query(notes).filter( and_(notes.tablename == 'obsrequests', notes.tableid == requestsid)).first() if comment: usr = db_session.query(users).filter( users.id == comment.userid).first() snex2_user = User.objects.get(username=usr.name) content_type_id = ContentType.objects.get(model='observationgroup').id newcomment = Comment(object_pk=newobsgroup.id, user_name=snex2_user.username, user_email=snex2_user.email, comment=comment.note, submit_date=comment.posttime, is_public=True, is_removed=False, content_type_id=content_type_id, site_id=2, user_id=snex2_user.id) newcomment.save() return newobsgroup
def test_to_table(self): site = USite.objects.create(name="site") unit1 = utils.create_unit(site=site) fault_utils.create_fault(unit=unit1) unit2 = utils.create_unit() fault2 = fault_utils.create_fault(unit=unit2) usa2 = sl_utils.create_unit_service_area(unit=unit2) sl_utils.create_service_event(unit_service_area=usa2) se2 = sl_utils.create_service_event(unit_service_area=usa2) fault2.related_service_events.add(se2) user = sl_utils.create_user() comment = Comment(submit_date=timezone.now(), user=user, content_object=fault2, comment="comment", site=get_current_site(None)) comment.save() rep = faults.FaultDetailsReport( report_opts={'review_status': 'unreviewed'}) rep.report_format = "csv" context = rep.get_context() table = rep.to_table(context) header_row = table.index([ _("Fault ID"), _("Occurred"), _("Site"), _("Unit"), _("Fault Type"), _("Modality"), _("Created By"), _("Created Date"), _("Modified By"), _("Modified Date"), _("Reviewed By"), _("Reviewed Date"), _("Related Service Events"), _("Link"), ]) # should be three faults after header assert len(table[header_row + 1:]) == 2
def post(self, request, *args, **kwargs): task = self.get_object() user = self.request.user task.updated_by_user = user task_content_type = ContentType.objects.get_for_model(task) comment_text = request.POST.get('comment', None) if task.customised: # redirect to custom close url, if necessary if self.change == 'close' and task.customised.close_url(): return redirect(task.customised.close_url()) for change, verb in Task.VERBS.items(): if self.change == change: state_change = getattr(task, change) if not has_transition_perm(state_change, self): raise PermissionDenied if comment_text: comment = Comment(user=user, object_pk=task.id, user_name=user.get_full_name() or user.username, user_email=user.email, comment=comment_text, content_type=task_content_type, site_id=get_current_site(request).id) state_change(user, comment=comment.comment) # save the comment here so that it appears after the action comment.submit_date = now() comment.save() else: state_change(user) if change == 'submit': verb = 'submitted for review' if change == 'unsubmit': verb = 'returned with changes requested' messages.success(request, "Task '%s' has been %s" % (task.title, verb)) task.save() return redirect(self.get_redirect_url())
def save_valid_fault_form(form, request): """helper for EditFault, CreateFault, and create_ajax for processing FaultForm""" fault = form.save(commit=False) if not fault.id: fault.created_by = request.user fault.modified_by = request.user fault.save() new_faults = set(models.FaultType.objects.filter(code__in=form.cleaned_data['fault_types_field'])) cur_faults = set(fault.fault_types.all()) to_remove = cur_faults - new_faults to_add = new_faults - cur_faults fault.fault_types.remove(*to_remove) fault.fault_types.add(*to_add) related_service_events = form.cleaned_data.get('related_service_events', []) sers = sl_models.ServiceEvent.objects.filter(pk__in=related_service_events) fault.related_service_events.set(sers) comment = form.cleaned_data.get('comment', '') if comment: comment = Comment( submit_date=timezone.now(), user=request.user, content_object=fault, comment=comment, site=get_current_site(request) ) comment.save() for f in request.FILES.getlist('fault-attachments'): Attachment.objects.create( attachment=f, comment="Uploaded %s by %s" % (timezone.now(), request.user.username), label=f.name, fault=fault, created_by=request.user ) a_ids = [a for a in request.POST.getlist('fault-attachments_delete_ids', '') if a] if a_ids: Attachment.objects.filter(id__in=a_ids).delete() return fault
def comment(request, segment_id): if request.user.is_authenticated() and request.method == 'POST': ctype = ContentType.objects.get_for_model(BillSegment) segment = BillSegment.objects.get(pk=segment_id) obj = Comment() obj.content_type = ctype obj.user = request.user obj.comment = request.POST.get('comment') obj.object_pk = segment_id obj.site_id = settings.SITE_ID obj.save() html = render_to_string('widget/_segment_comments.html', {'segment': segment}) return JsonResponse({'html': html, 'count': segment.comments.all().count()}) else: msg = _("You must be logged to comment.") return HttpResponseForbidden(reason=msg)
def test_generate_html(self): site = USite.objects.create(name="site") unit = utils.create_unit(site=site) utc = utils.create_unit_test_collection(unit=unit) utils.create_test_list_instance(unit_test_collection=utc) unit2 = utils.create_unit(site=None) utc2 = utils.create_unit_test_collection(unit=unit2) tli2 = utils.create_test_list_instance(unit_test_collection=utc2) comment = Comment( submit_date=timezone.now(), user=tli2.created_by, content_object=tli2, comment='test comment', site=Site.objects.latest("pk"), ) comment.save() rep = qc.TestListInstanceDetailsReport( report_opts={'unit_test_collection': [utc.pk]}) rep.report_format = "pdf" rep.to_html()
def post(self, request, *args, **kwargs): form = HelpDeskForm(request.POST) if kwargs: pk = kwargs['pk'] requesteduser = HelpDeskModel.objects.get(id=pk).name_Request.id content = ContentType.objects.get_for_model(HelpDeskModel) current_site = get_current_site(request) # Add a comment to the issue thread create_first_note = Comment( content_type=content, object_pk=request.POST['object_pk'], site=current_site, user=request.user, user_name=request.user.get_full_name(), user_email=request.user.email, user_url='http://dummyurl.com', comment=request.POST['comment'], submit_date=datetime.now(), ip_address='127.0.0.1', ) create_first_note.save() return redirect('HelpDesk', pk) else: if form.is_valid(): f = form.save(commit=False) f.name_Request = request.user f.save() message = Mail( from_email='*****@*****.**', to_emails=['*****@*****.**'], subject='HelpDesk Request', html_content="A new helpdesk complaint has been filed by.") sg = SendGridAPIClient(SENDGRID_API_KEY) # sg.send(message) messages.success(request, "Your request has been submitted.") return redirect('HelpDesk', f.id)
def add_new_note(self, request, resource_type, resource_id): resource = request.resource if request.POST: #title = request.REQUEST.get('title'); body = request.REQUEST.get('body') new_comment = Comment(content_object=resource, site=DjangoSite.objects.all()[0], user=request.user, user_name=request.user.username, user_email=request.user.email, user_url='', comment=body, ip_address=None, is_public=True, is_removed=False) new_comment.save() return self.response_success() return HttpResponse('')
def handle(self, *args, **options): ### Define our db tables as Classes obsrequests = load_table('obsrequests', db_address=_SNEX1_DB) obslog = load_table('obslog', db_address=_SNEX1_DB) obstags = load_table('obsrequests_tags', db_address=_SNEX1_DB) tags = load_table('tags', db_address=_SNEX1_DB) Groups = load_table('groups', db_address=_SNEX1_DB) users = load_table('users', db_address=_SNEX1_DB) notes = load_table('notes', db_address=_SNEX1_DB) #print('Made tables') with get_session(db_address=_SNEX1_DB) as db_session: ### Make a dictionary of the groups in the SNex1 db snex1_groups = {} for x in db_session.query(Groups): snex1_groups[x.name] = x.idcode ### Get all the currently active sequences onetime_sequence = db_session.query(obsrequests).filter( and_( obsrequests.autostop == 1, obsrequests.approved == 1, or_(obsrequests.sequenceend == '0000-00-00 00:00:00', obsrequests.sequenceend > datetime.datetime.utcnow()))) onetime_sequence_ids = [int(o.id) for o in onetime_sequence] repeating_sequence = db_session.query(obsrequests).filter( and_( obsrequests.autostop == 0, obsrequests.approved == 1, or_(obsrequests.sequenceend == '0000-00-00 00:00:00', obsrequests.sequenceend > datetime.datetime.utcnow()))) repeating_sequence_ids = [int(o.id) for o in repeating_sequence] ### Get pending sequence ids pending_sequence = db_session.query(obsrequests).filter( and_( obsrequests.approved == 0, or_(obsrequests.sequenceend == '0000-00-00 00:00:00', obsrequests.sequenceend > datetime.datetime.utcnow()))) pending_sequence_ids = [int(o.id) for o in pending_sequence] #print('Got active sequences') ### Cancel the SNEx2 sequences that are no longer active in SNEx1 snex2_active_cadences = DynamicCadence.objects.filter(active=True) for cadence in snex2_active_cadences: obsgroupid = int(cadence.observation_group_id) currentobsgroup = ObservationGroup.objects.filter( id=obsgroupid).first() try: snex1id = int(currentobsgroup.name) except: # Name not an integer, so not an observation group from SNEx1 continue if snex1id not in onetime_sequence_ids and snex1id not in repeating_sequence_ids: cadence.active = False cadence.save() ### Look for comments associated with cancellation by ### checking the number of comments in SNEx2 vs SNEx1 content_type_id = ContentType.objects.get( model='observationgroup').id snex2_comment_count = Comment.objects.filter( object_pk=obsgroupid, content_type_id=content_type_id).count() snex1_comment_query = db_session.query(notes).filter( and_(notes.tableid == snex1id, notes.tablename == 'obsrequests')).order_by( notes.id.desc()) snex1_comment_count = snex1_comment_query.count() if snex2_comment_count < snex1_comment_count: cancel_comment = snex1_comment_query.first() usr = db_session.query(users).filter( users.id == cancel_comment.userid).first() snex2_user = User.objects.get(username=usr.name) # Ingest most recent snex1 comment newcomment = Comment( object_pk=obsgroupid, user_name=snex2_user.username, user_email=snex2_user.email, comment=cancel_comment.note, submit_date=cancel_comment.posttime, is_public=True, is_removed=False, content_type_id=content_type_id, site_id=2, user_id=snex2_user.id) newcomment.save() ### Add the sequence end time to the template observationrecord templaterecord = currentobsgroup.observation_records.filter( observation_id='template').first() if templaterecord: templaterecord.parameters[ 'sequence_end'] = datetime.datetime.utcnow( ).strftime('%Y-%m-%dT%H:%M:%S') templaterecord.save() #print('Canceled inactive sequences') #### Check if any of the pending requests in SNEx2 are no longer pending in SNEx1 snex2_pending_cadences = ObservationRecord.objects.filter( observation_id='template pending') for pendingobs in snex2_pending_cadences: currentobsgroup = pendingobs.observationgroup_set.first() try: snex1id = int(currentobsgroup.name) except: # Name not an integer, so not an observation group from SNEx1 continue if snex1id not in pending_sequence_ids: #Was either accepted or rejected pendingobs.observation_id = 'template' pendingobs.save() # Figure out if the pending sequence was accepted or rejected obsgroupid = currentobsgroup.id if snex1id in onetime_sequence_ids or snex1id in repeating_sequence_ids: cadence = DynamicCadence.objects.get( observation_group_id=obsgroupid) cadence.active = True cadence.save() else: ### Look for comments associated with cancellation by ### checking the number of comments in SNEx2 vs SNEx1 content_type_id = ContentType.objects.get( model='observationgroup').id snex2_comment_count = Comment.objects.filter( object_pk=obsgroupid, content_type_id=content_type_id).count() snex1_comment_query = db_session.query(notes).filter( and_(notes.tableid == snex1id, notes.tablename == 'obsrequests')).order_by( notes.id.desc()) snex1_comment_count = snex1_comment_query.count() if snex2_comment_count < snex1_comment_count: cancel_comment = snex1_comment_query.first() usr = db_session.query(users).filter( users.id == cancel_comment.userid).first() snex2_user = User.objects.get(username=usr.name) # Ingest most recent snex1 comment newcomment = Comment( object_pk=obsgroupid, user_name=snex2_user.username, user_email=snex2_user.email, comment=cancel_comment.note, submit_date=cancel_comment.posttime, is_public=True, is_removed=False, content_type_id=content_type_id, site_id=2, user_id=snex2_user.id) newcomment.save() ### Compare the currently active sequences with the ones already in SNEx2 ### to see which ones need to be added and which ones need the newest obs requests onetime_obs_to_add = [] repeating_obs_to_add = [] existing_onetime_obs = [] existing_repeating_obs = [] pending_obs_to_add = [] # Get the observation groups already in SNEx2 existing_obs = [] for o in ObservationGroup.objects.all(): try: existing_obs.append(int(o.name)) except: # Name not a SNEx1 ID, so not in SNEx1 continue for o in onetime_sequence: if int(o.id) not in existing_obs: onetime_obs_to_add.append(o) else: existing_onetime_obs.append(o) for o in repeating_sequence: if int(o.id) not in existing_obs: repeating_obs_to_add.append(o) else: existing_repeating_obs.append(o) for o in pending_sequence: if int(o.id) not in existing_obs: pending_obs_to_add.append(o) ### Add the pending observations not in SNEx2 for obs in pending_obs_to_add: created = obs.datecreated modified = obs.lastmodified target_id = int(obs.targetid) target_query = Target.objects.filter(id=target_id) if not target_query.exists(): print( 'Observation not ingested because target {} does not exist' .format(target_id)) continue requestsid = int(obs.id) if obs.autostop == 0: snex2_param = get_snex2_params(obs, repeating=True) else: snex2_param = get_snex2_params(obs, repeating=False) newobsgroup = create_new_sequence(requestsid, created, modified, snex2_param, users, notes, db_session, active=False) ### Add "template" record snex2_param['sequence_start'] = str(obs.sequencestart).replace( ' ', 'T') snex2_param['sequence_end'] = str(obs.sequenceend).replace( ' ', 'T') snex2_param['start_user'] = db_session.query(users).filter( users.id == obs.userstart).first().firstname rmndr = snex2_param.pop('reminder') template = ObservationRecord(facility='LCO', observation_id='template pending', status='', created=created, modified=modified, target_id=target_id, user_id=2, parameters=snex2_param) template.save() # Save permissions on template if int(obs.groupidcode) is not None: update_permissions(int(obs.groupidcode), template, snex1_groups) #View obs record newobsgroup.observation_records.add(template) count = 0 #print('Getting parameters for new sequences') for sequencelist in [ onetime_obs_to_add, existing_onetime_obs, repeating_obs_to_add, existing_repeating_obs ]: for obs in sequencelist: facility = 'LCO' created = obs.datecreated modified = obs.lastmodified target_id = int(obs.targetid) user_id = 2 #supernova ### Make sure target is in SNEx2 (to avoid ingesting obs for standards) target_query = Target.objects.filter(id=target_id) if not target_query.exists(): print( 'Observation not ingested because target {} does not exist' .format(target_id)) continue ### Get observation id from observation portal API # Query API requestsid = int(obs.id) #print('Querying API for sequence with SNEx1 ID of {}'.format(requestsid)) # Get observation portal requestgroup id from most recent obslog entry for this observation sequence tracknumber_query = db_session.query(obslog).filter( and_(obslog.requestsid == requestsid, obslog.tracknumber > 0)).order_by( obslog.id.desc()) tracknumber_count = tracknumber_query.count() if tracknumber_count > 0: tracknumber = int( tracknumber_query.first().tracknumber) windowstart = Time( tracknumber_query.first().windowstart, format='jd').to_value('isot') windowend = Time(tracknumber_query.first().windowend, format='jd').to_value('isot') # Get the observation portal observation id using this tracknumber headers = { 'Authorization': 'Token {}'.format(os.environ['LCO_APIKEY']) } response = requests.get( 'https://observe.lco.global/api/requestgroups/{}'. format(tracknumber), headers=headers) if not response.json().get('requests'): continue result = response.json()['requests'][0] observation_id = int(result['id']) status = result['state'] #print('The most recent observation request for this sequence has API id {} with status {}'.format(observation_id, status)) #print('and with parameters {}'.format(snex2_param)) else: #print('No requests have been submitted for this sequence yet') observation_id = 0 if count < 2: snex2_param = get_snex2_params(obs, repeating=False) else: snex2_param = get_snex2_params(obs, repeating=True) if observation_id: in_snex2 = bool( ObservationRecord.objects.filter( observation_id=str(observation_id)).count()) #print('Is this observation request in SNEx2? {}'.format(in_snex2)) else: in_snex2 = False ### Add the new cadence, observation group, and observation record to the SNEx2 db try: ### Create new observation group and dynamic cadence, if it doesn't already exist if count == 0 or count == 2: newobsgroup = create_new_sequence(requestsid, created, modified, snex2_param, users, notes, db_session, active=True) ### Add "template" record snex2_param['sequence_start'] = str( obs.sequencestart).replace(' ', 'T') snex2_param['sequence_end'] = str( obs.sequenceend).replace(' ', 'T') snex2_param['start_user'] = db_session.query( users).filter(users.id == obs.userstart).first().firstname rmndr = snex2_param.pop('reminder') template = ObservationRecord( facility='LCO', observation_id='template', status='', created=created, modified=modified, target_id=target_id, user_id=user_id, parameters=snex2_param) template.save() # Save permissions on template if int(obs.groupidcode) is not None: update_permissions( int(obs.groupidcode), template, snex1_groups) #View obs record newobsgroup.observation_records.add(template) ### Add observation science tag to the target in SNEx2, if it doesn't already exist obstagids = [ t.tagid for t in db_session.query(obstags).filter( obstags.requestsid == requestsid).all() ] taglist = [ x.tag for x in db_session.query(tags).filter( tags.id.in_(obstagids)).all() ] for t in taglist: snex2_tag, _ = ScienceTags.objects.get_or_create( tag=t) newtag, created = TargetTags.objects.get_or_create( target_id=target_id, tag_id=int(snex2_tag.id)) ### Add the new observation record, if it exists in SNEx1 but not SNEx2 if tracknumber_count > 0 and observation_id > 0 and not in_snex2: snex2_param['start'] = windowstart snex2_param['end'] = windowend newobs = ObservationRecord( facility=facility, observation_id=str(observation_id), status=status, created=created, modified=modified, target_id=target_id, user_id=user_id, parameters=snex2_param) newobs.save() obs_groupid = int(obs.groupidcode) if obs_groupid is not None: update_permissions( int(obs_groupid), newobs, snex1_groups) #View obs record ### Add observaton record to existing observation group or the one we just made if count == 0 or count == 2: #print('Adding to new observation group') newobsgroup.observation_records.add(newobs) else: oldobsgroup = ObservationGroup.objects.filter( name=str(requestsid)).first() oldobsgroup.observation_records.add(newobs) #print('Added observation record') if in_snex2: #Update the status and start and end times oldobs = ObservationRecord.objects.filter( observation_id=str(observation_id)).order_by( '-id').first() oldobs.status = status oldobs.parameters['reminder'] = snex2_param[ 'reminder'] oldobs.save() except: raise count += 1
def post_comment(request, next=None, using=None): """ Post a comment. HTTP POST is required. If ``POST['submit'] == "preview"`` or if there are errors a preview template, ``comments/preview.html``, will be rendered. """ # Fill out some initial data fields from an authenticated user, if present data = request.POST.copy() if request.user.is_authenticated(): if not data.get('name', ''): data["name"] = request.user.get_full_name() or request.user.get_username() if not data.get('email', ''): data["email"] = request.user.email # Look up the object we're trying to comment about ctype = data.get("content_type") object_pk = data.get("object_pk") if ctype is None or object_pk is None: return CommentPostBadRequest("Missing content_type or object_pk field.") try: model = apps.get_model("backend",ctype) # model = apps.get_model(*ctype.split(".", 1)) target = model._default_manager.using(using).get(pk=object_pk) except TypeError: return CommentPostBadRequest( "Invalid content_type value: %r" % escape(ctype)) except AttributeError: return CommentPostBadRequest( "The given content-type %r does not resolve to a valid model." % escape(ctype)) except ObjectDoesNotExist: return CommentPostBadRequest( "No object matching content-type %r and object PK %r exists." % ( escape(ctype), escape(object_pk))) except (ValueError, ValidationError) as e: return CommentPostBadRequest( "Attempting go get content-type %r and object PK %r exists raised %s" % ( escape(ctype), escape(object_pk), e.__class__.__name__)) # Create the comment comment = Comment( content_object=target, user_name=data["name"], user_email=data["email"], comment=data["comment"], site_id=settings.SITE_ID ) comment.ip_address = request.META.get("REMOTE_ADDR", None) if request.user.is_authenticated(): comment.user = request.user # Signal that the comment is about to be saved responses = signals.comment_will_be_posted.send( sender=comment.__class__, comment=comment, request=request ) for (receiver, response) in responses: if response is False: return CommentPostBadRequest( "comment_will_be_posted receiver %r killed the comment" % receiver.__name__) # Save the comment and signal that it was saved comment.save() signals.comment_was_posted.send( sender=comment.__class__, comment=comment, request=request ) serializer = CommentSerializer(comment, context={'request': request}) return JsonResponse(serializer.data, safe=False)
def update_todos(self, todo_id): try: todolists = TodoList.objects.filter(basecamp_id=todo_id) except TodoList.DoesNotExist: raise CommandError('Todo list whith given basecamp_id not found') for todolist in todolists: xml = ET.fromstring(self.bc.todo_list(todolist.basecamp_id)) for todoitem in xml.find('todo-items').findall('todo-item'): bc_id = int(todoitem.find('id').text) bc_name = todoitem.find('content').text bc_created_at = parser.parse(todoitem.find('created-at').text) bc_updated_at = parser.parse(todoitem.find('updated-at').text) try: bc_creator = Profile.objects.get(basecamp_id=todoitem.find('creator-id').text).user except Profile.DoesNotExist: bc_creator = None bc_due_at = todoitem.find('due-at').text try: bc_responsible_type = todoitem.find('responsible-party-type').text if (bc_responsible_type == 'Person'): try: bc_responsible = Profile.objects.\ get(basecamp_id=todoitem.find('responsible-party-id').text).user except Profile.DoesNotExist: bc_responsible = None else: bc_responsible = None except: bc_responsible = None if bc_due_at is not None: bc_due_at = parser.parse(bc_due_at) bc_position = todoitem.find('position').text bc_completed = todoitem.find('completed').text if bc_completed != "true": bc_completed = False else: bc_completed = True if bc_completed: bc_completed_at = parser.parse(todoitem.find('completed-at').text) try: bc_completer = Profile.objects.get(basecamp_id=todoitem.find('completer-id').text).user except Profile.DoesNotExist: bc_completer = None need_update_todo_item = False try: d_todoitem = TodoItem.objects.get(basecamp_id=bc_id) if d_todoitem.updated_at < bc_updated_at or self.forced: need_update_todo_item = True d_todoitem.sort = bc_position d_todoitem.name = bc_name d_todoitem.creator = bc_creator d_todoitem.completed = bc_completed d_todoitem.due_at = bc_due_at d_todoitem.todo_list = todolist d_todoitem.project = todolist.project d_todoitem.responsible = bc_responsible d_todoitem.created_at = bc_created_at d_todoitem.updated_at = bc_updated_at if bc_completed: d_todoitem.completed_at = bc_completed_at d_todoitem.completer = bc_completer d_todoitem.save() except: need_update_todo_item = True d_todoitem = TodoItem( basecamp_id = bc_id, sort = bc_position, name = bc_name, creator = bc_creator, completed = bc_completed, responsible = bc_responsible, due_at = bc_due_at, todo_list = todolist, project = todolist.project, created_at = bc_created_at, updated_at = bc_updated_at ) if bc_completed: d_todoitem.completed_at = bc_completed_at d_todoitem.completer = bc_completer d_todoitem.save() try: self.stdout.write(u"TodoItem %s created." % d_todoitem) except UnicodeDecodeError: self.stdout.write(u"TodoItem created.") if (need_update_todo_item): comments_xml = ET.fromstring(self.bc.todo_item_comments(d_todoitem.basecamp_id)) for comment in comments_xml.findall('comment'): bc_comment_id = comment.find('id').text try: Comment.objects.for_model(TodoItem)\ .get(object_pk = d_todoitem.pk, user_url = bc_comment_id) except Comment.DoesNotExist: try: bc_author = Profile.objects.get(basecamp_id=comment.find('author-id').text).user except Profile.DoesNotExist: bc_author = None django_comment = Comment( content_type = ContentType.objects.get_for_model(TodoItem), object_pk = d_todoitem.pk, user = bc_author, user_name = comment.find('author-name').text, user_email = '', user_url = bc_comment_id, comment = comment.find('body').text, submit_date = parser.parse(comment.find('created-at').text), site_id = settings.SITE_ID, is_public = True, is_removed = False, ) django_comment.save()
def test_to_table(self): site = USite.objects.create(name="site") unit = utils.create_unit(site=site) utc = utils.create_unit_test_collection(unit=unit) tli = utils.create_test_list_instance(unit_test_collection=utc) # make this tli autoreviewed tli.all_reviewed = True tli.reviewed_by = None tli.save() unit2 = utils.create_unit(site=None) utc2 = utils.create_unit_test_collection(unit=unit2) tli2 = utils.create_test_list_instance(unit_test_collection=utc2) # give tli2 some history tli3 = utils.create_test_list_instance( unit_test_collection=utc2, work_completed=tli2.work_completed - timezone.timedelta(days=2), ) ti = utils.create_test_instance(test_list_instance=tli3) # tli comment comment = Comment( submit_date=timezone.now(), user=tli2.created_by, content_object=tli2, comment='test comment', site=Site.objects.latest("pk"), ) comment.save() attachment = Attachment( attachment=ContentFile("content", "content.pdf"), created_by=tli.created_by, testlistinstance=tli2, ) attachment.save() attachment = Attachment( attachment=ContentFile("content", "content.pdf"), created_by=tli.created_by, testinstance=ti, ) attachment.save() rep = qc.TestListInstanceDetailsReport( report_opts={'unit_test_collection': [utc.pk, utc2.pk]}) rep.report_format = "csv" context = rep.get_context() table = rep.to_table(context) ntlis = table.count([ _('Test'), _('Value'), _('Reference'), _('Tolerance'), _('Pass/Fail'), _('Review Status'), _('Comment'), _('Attachments'), ]) # should be three tlis assert ntlis == 3
def get_comments(targetid, tablename, notes, users, days_ago): content_dict = { 'targets': ContentType.objects.get(model='target').id, 'obsrequests': ContentType.objects.get(model='observationgroup').id, 'spec': ContentType.objects.get(model='reduceddatum').id } with get_session(db_address=_SNEX1_DB) as db_session: if targetid == 'all': comments = db_session.query(notes).filter( and_(notes.tablename == tablename, notes.datecreated > days_ago)) else: comments = db_session.query(notes).filter( and_(notes.targetid == targetid, notes.tablename == tablename, notes.datecreated > days_ago)) for comment in comments: usr = db_session.query(users).filter( users.id == comment.userid).first() snex2_user = User.objects.get(username=usr.name) target_id = comment.targetid if tablename == 'targets': # Check if it already exists in SNEx2 old_comment = Comment.objects.filter( object_pk=target_id, comment=comment.note, content_type_id=content_dict[tablename]).first() if old_comment: continue newcomment = Comment( object_pk=target_id, user_name=snex2_user.username, user_email=snex2_user.email, comment=comment.note, submit_date=comment.posttime, is_public=True, is_removed=False, content_type_id=content_dict[tablename], site_id=2, #TODO: Why? user_id=snex2_user.id) newcomment.save() elif tablename == 'obsrequests': # Need to get the observationgroup id given its name obsgroup = ObservationGroup.objects.filter( name=str( comment.tableid)) #TODO: Check if should be str or int if obsgroup.count() > 0: newcomment = Comment( object_pk=obsgroup.first().id, user_name=snex2_user.username, user_email=snex2_user.email, comment=comment.note, submit_date=comment.posttime, is_public=True, is_removed=False, content_type_id=content_dict[tablename], site_id=2, #TODO: Why? user_id=snex2_user.id) newcomment.save() elif tablename == 'spec': # Need to get reduceddatum id from the reduceddatumextra table rdes = ReducedDatumExtra.objects.filter( data_type='spectroscopy', key='snex_id', target_id=int(comment.targetid)) snex2_id = False for rde in rdes: if int(comment.tableid) == json.loads( rde.value)['snex_id']: snex2_id = json.loads(rde.value)['snex2_id'] break if snex2_id: # Check if it already exists in SNEx2 old_comment = Comment.objects.filter( object_pk=snex2_id, comment=comment.note, content_type_id=content_dict[tablename]).first() if old_comment: continue newcomment = Comment( object_pk=snex2_id, user_name=snex2_user.username, user_email=snex2_user.email, comment=comment.note, submit_date=comment.posttime, is_public=True, is_removed=False, content_type_id=content_dict[tablename], site_id=2, #TODO: Why? user_id=snex2_user.id) newcomment.save() print('Done ingesting comments for target {} and table {}'.format( targetid, tablename))
def post(self, request, *args, **kwargs): form = HelpDeskForm(request.POST) if kwargs: pk = kwargs['pk'] requesteduser = HelpDeskModel.objects.get(id=pk).name_Request.id content = ContentType.objects.get_for_model(HelpDeskModel) current_site = get_current_site(request) # Add a comment to the issue thread create_first_note = Comment( content_type=content, object_pk=request.POST['object_pk'], site=current_site, user=request.user, user_name=request.user.get_full_name(), user_email=request.user.email, user_url='http://dummyurl.com', comment=request.POST['comment'], submit_date=datetime.now(), ip_address='127.0.0.1', ) create_first_note.save() return redirect('HelpDesk', pk) else: if form.is_valid(): f = form.save(commit=False) f.name_Request = request.user f.save() subject = 'Your email has been logged with HelpDesk' email = str(request.user) try: alumni = Alumni.objects.get(user_id=request.user.id) first_name = alumni.user.first_name context = {'first_name': first_name} admin_context = { 'first_name': first_name, 'protocol': 'https', 'domain': 'murdochdubaicareerportal.com' } htmlText = render_to_string( 'HelpDesk/helpdesk_request_logged.html', context) send_html_mail(subject, htmlText, [email]) subject = 'A new HelpDesk request has been received' htmlText = render_to_string( 'HelpDesk/helpdesk_request_submitted.html', admin_context) send_html_mail(subject, htmlText, [DEFAULT_FROM_EMAIL]) except: pass try: student = Student.objects.get(user_id=request.user.id) first_name = student.user.first_name student_email = str(student.personal_email) context = {'first_name': first_name} admin_context = { 'first_name': first_name, 'protocol': 'https', 'domain': 'murdochdubaicareerportal.com' } htmlText = render_to_string( 'HelpDesk/helpdesk_request_logged.html', context) send_html_mail(subject, htmlText, [email]) send_html_mail(subject, htmlText, [student_email]) subject = 'A new HelpDesk request has been received' htmlText = render_to_string( 'HelpDesk/helpdesk_request_submitted.html', admin_context) send_html_mail(subject, htmlText, [DEFAULT_FROM_EMAIL]) except: pass try: employer = Employer.objects.get(user_id=request.user.id) first_name = employer.company_name context = {'first_name': first_name} admin_context = { 'first_name': first_name, 'protocol': 'https', 'domain': 'murdochdubaicareerportal.com' } htmlText = render_to_string( 'HelpDesk/helpdesk_request_logged.html', context) send_html_mail(subject, htmlText, [email]) subject = 'A new HelpDesk request has been received' htmlText = render_to_string( 'HelpDesk/helpdesk_request_submitted.html', admin_context) send_html_mail(subject, htmlText, [DEFAULT_FROM_EMAIL]) except: pass messages.success(request, "Your request has been submitted.") return redirect('HelpDesk', f.id)