Пример #1
0
 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)
Пример #2
0
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())
Пример #3
0
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())
Пример #4
0
    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()
Пример #5
0
    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
Пример #6
0
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()
Пример #7
0
    def get_context_data(self, **kwargs):
        context = super(TaskDetailView, self).get_context_data(**kwargs)
        task = self.object

        # merge actions and comments
        actions = task.action_object_actions.all()
        task_content_type = ContentType.objects.get_for_model(self.model)
        comments = list(Comment.objects\
            .filter(content_type=task_content_type, object_pk=task.id)\
            .select_related('user'))

        # get the annotation for the particular task
        try:
            task_annotation = task.annotation
        except Annotation.DoesNotExist:
            task_annotation = None

        # for the annotation that is linked to the task, get all the replies
        if task_annotation:
            # get the replies to the annotation
            annotation_replies = Annotation.objects.filter(in_reply_to=task_annotation)\
                    .select_related('created_by_user')

            comments.extend([
                Comment(user=a.created_by_user,
                        comment=a.text,
                        submit_date=a.created_at) for a in annotation_replies
            ])

        context['task_timeline'] = sorted(
            chain(comments, actions),
            key=lambda x: x.submit_date
            if hasattr(x, 'comment') else x.timestamp)

        context['possible_workflows'] = Workflow.objects.unclosed().filter(
            country=task.country, locality=task.locality).all()

        # warn when submitting task on behalf of another user
        Task.decorate_submission_message([task], self)

        Task.decorate_potential_assignees([task], self.country)
        Task.decorate_permissions([task], self)

        # add work to context
        if task.work:
            context['work'] = task.work
            context['work_json'] = json.dumps(
                WorkSerializer(instance=task.work,
                               context={
                                   'request': self.request
                               }).data)

        return context
Пример #8
0
 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)
Пример #9
0
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
Пример #10
0
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
Пример #11
0
    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())
Пример #12
0
    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
Пример #13
0
    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()
Пример #14
0
    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)
Пример #15
0
    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('')
Пример #16
0
 def getGetApproveURL(self):
     c = Comment(id=12345)
     self.assertEqual(django_comments.get_approve_url(c), "/approve/12345/")
Пример #17
0
 def getGetDeleteURL(self):
     c = Comment(id=12345)
     self.assertEqual(django_comments.get_delete_url(c), "/delete/12345/")
Пример #18
0
 def testGetFlagURL(self):
     c = Comment(id=12345)
     self.assertEqual(django_comments.get_flag_url(c), "/flag/12345/")
Пример #19
0
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))
Пример #20
0
    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
Пример #21
0
    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)
Пример #22
0
    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